I am currently playing around with encryption in the Windows Runtime. When using certain encryption algorithms I get either a NotImplementedException (AesCcm, AesGcm) or an ArgumentException (AesEcb, AesEcbPkcs7, DesEcb, DesEcbPkcs7, Rc2Ecb, Rc2EcbPkcs7, Rc4, TripleDesEcb, TripleDesEcbPkcs7).
I use the correct key length for each algorithm (I figured that a wrong key length triggers an ArgumentException). For RC4 I use a key of size 1024 since the key is variable. When using the version without padding I pad the data myself to the block length. I kind of understand that AES with CCM and GCM is obviously not implemented in Windows 8, 64 Bit. But the ArgumentException of the variants with ECB cipher mode and of RC4 are strange.
Here is a sample code:
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();
By the way: I know that ECB is not considered safe. But Microsoft included ECB for certain algorithms. That must have a reason (parallelization or so).
The very same code works using AesCbcPkcs7 for example. A similar code for .NET using AES with ECB and PKCS7, a key length of 256 and an IV sized equal to the block length works as well on the same machine.
What could be meant by the ArgumentException?
I found the answer to the ArgumentException myself: I passed an initialization vector even for algorithmns that does not make use of it (like ECB cipher modes or RC4). These algorithms require that the initialization vector is passed as null.