0

我目前正在玩 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 可能意味着什么?

4

1 回答 1

0

我自己找到了 ArgumentException 的答案:即使对于不使用它的算法(如 ECB 密码模式或 RC4),我也传递了一个初始化向量。这些算法要求初始化向量作为空值传递。

于 2013-01-05T09:31:28.897 回答