我目前正在玩 Windows 运行时中的加密。使用某些加密算法时,我会得到 NotImplementedException(AesCcm,AesGcm)或 ArgumentException(AesEcb,AesEcbPkcs7,DesEcb,DesEcbPkcs7,Rc2Ecb,Rc2EcbPkcs7,Rc4,TripleDesEcb,TripleDesEcbPkcs7)。
我为每个算法使用正确的密钥长度(我认为错误的密钥长度会触发 ArgumentException)。对于 RC4,我使用大小为 1024 的密钥,因为密钥是可变的。当使用没有填充的版本时,我自己将数据填充到块长度。我有点理解带有 CCM 和 GCM 的 AES 显然没有在 Windows 8、64 位中实现。但是具有 ECB 密码模式和 RC4 的变体的 ArgumentException 很奇怪。
这是一个示例代码:
SymmetricKeyAlgorithmProvider symmetricKeyAlgorithmProvider =
SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
byte[] plainText = {1, 2, 3, 4, 5, 6, 7, 9, 9, 0};
const uint keySize = 256;
byte[] key = CryptographicBuffer.GenerateRandom(keySize).ToArray();
uint blockLength = symmetricKeyAlgorithmProvider.BlockLength;
byte[] initializationVector =
CryptographicBuffer.GenerateRandom(blockLength).ToArray();
CryptographicKey cryptographicKey =
symmetricKeyAlgorithmProvider.CreateSymmetricKey(key.AsBuffer());
// This line throws an ArgumentException. The exception gives no hint what
// argument is meant and why the value is invalid.
byte[] cipherText = CryptographicEngine.Encrypt(cryptographicKey,
plainText.AsBuffer(), initializationVector.AsBuffer()).ToArray();
顺便说一句:我知道欧洲央行不被认为是安全的。但微软在某些算法中包含了 ECB。那一定是有原因的(并行化左右)。
例如,相同的代码使用 AesCbcPkcs7 工作。使用 AES 和 ECB 和 PKCS7 的 .NET 类似代码,密钥长度为 256,IV 大小等于块长度,在同一台机器上也能正常工作。
ArgumentException 可能意味着什么?