AES/CBC/NoPadding between C# and Java -


i'm using encryption functions in c# , java output doesn't seem match. i'm feeding in same key , iv strings 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"));  // convert encrypted hex building string of encrypted[i] & 0xff 

output:

60f73a575b647263d75011bb974a90e85201b8dfeec6ec8ffba04c75ab5649b3 

c#:

symmetrickeyalgorithmprovider alg = symmetrickeyalgorithmprovider.openalgorithm(symmetricalgorithmnames.aescbc);  binarystringencoding encoding = binarystringencoding.utf8;  // create key , 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); // encryption ibuffer encryptedbuffer = cryptographicengine.encrypt(ckey, inputbuffer, ivbuffer);  // convert encrypted hex string encryptedstr = cryptographicbuffer.encodetohexstring(encryptedbuffer); 

output:

4b6fd83c35565fc30a9ce56134c277cbea74d14886cf99e11f4951075d4f4505 

i using java decrypter check , decrypts java-encrypted string correctly c# string read "&app_version=1q0.0.0" seems close off.

i have checked bytes of key, input, , iv match before encryption step. there other differences cause discrepancy?

edit all-zero key "00000000000000000000000000000000" , iv "0000000000000000" got same output both java , c#:

081821ab6599650b4a31e29994cb130203e0d396a1d375c7d1c05af73b44a86f 

so perhaps there wrong key or iv 1 reading...

i feel fool...my iv contained 0 in 1 , capital o in another!! well, @ least know code equivalent.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -