AES / CBC / NoPadding between C # and Java

I use some encryption features in C # and Java, the output of which does not seem to match. I feed on the same key and lines IV as a test.

Input line: "& 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 

FROM#:

 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 use Java decrypter for verification, and it correctly decrypts the string encrypted by Java, but the C # string is read as "& app_version = 1Q0.0.0", so it seems close, but slightly disabled.

I checked that the bytes of the key, input, and IV match before the encryption step. Are there any other differences that may cause a discrepancy?

EDIT With the all-zero key "0000000000000000000000000000000000" and IV "0000000000000000" I got the same output for Java and C #:

 081821ab6599650b4a31e29994cb130203e0d396a1d375c7d1c05af73b44a86f 

So maybe something is wrong with the key or IV that is being read ...

+4
source share
2 answers

I feel like a fool ... my IV contained zero in one and capital O in the other !! Well, at least I know that this code is equivalent.

0
source

I have the same problem. C # and Java both give different results

https://www.jdoodle.com/a/Rjz

0
source

Source: https://habr.com/ru/post/1495876/


All Articles