Reputation: 1387
I'm using some encryption functions in C# and Java whose output doesn't seem to match. I'm feeding in the same key and IV strings as a test.
Input string: "&app_version=1.0.0.0"
Java:
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes("UTF-8"));
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8"));
// Then I convert encrypted to hex by building a string of encrypted[i] & 0xFF
Output:
60f73a575b647263d75011bb974a90e85201b8dfeec6ec8ffba04c75ab5649b3
C#:
SymmetricKeyAlgorithmProvider alg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbc);
BinaryStringEncoding encoding = BinaryStringEncoding.Utf8;
// Create key and IV buffers
IBuffer keyBuffer = CryptographicBuffer.ConvertStringToBinary(key, encoding);
CryptographicKey cKey = alg.CreateSymmetricKey(keyBuffer);
IBuffer ivBuffer = CryptographicBuffer.ConvertStringToBinary(iv, encoding);
// Create input text buffer
IBuffer inputBuffer = CryptographicBuffer.ConvertStringToBinary(input, encoding);
// Do the encryption
IBuffer encryptedBuffer = CryptographicEngine.Encrypt(cKey, inputBuffer, ivBuffer);
// Convert encrypted back to hex
string encryptedStr = CryptographicBuffer.EncodeToHexString(encryptedBuffer);
Output:
4b6fd83c35565fc30a9ce56134c277cbea74d14886cf99e11f4951075d4f4505
I am using a Java decrypter to check and it decrypts the Java-encrypted string correctly but the C# string is read as "&app_version=1Q0.0.0" so it seems close but slightly off.
I have checked that the bytes of the key, input, and IV match before the encryption step. Are there any other differences that would cause a discrepancy?
EDIT With all-zero key "00000000000000000000000000000000" and IV "0000000000000000" I got the same output for both Java and C#:
081821ab6599650b4a31e29994cb130203e0d396a1d375c7d1c05af73b44a86f
So perhaps there is something wrong with the key or IV that one is reading...
Upvotes: 5
Views: 5167
Reputation: 1387
I feel like a fool...my IV contained a zero in one and a capital O in another!! Well, at least I know this code is equivalent.
Upvotes: 3