android - AES CBC PKCS5 Decryption in WIndows 8 Appstore application -


well embarrassing ask question despite having lot of articles related in web, not figure out.i struck one.

i encrypting text in android using aes/cbc/pkcs5 algorithm not decrypt in windows 8 appstore application.here encryption code

public static string encrypt(string plaintext,string password) throws exception {      // convert key bytes     byte[] keybytes = password.getbytes("utf-8");     // use first 16 bytes (or less if key shorter)      byte[] keybytes16 = new byte[16];      system.arraycopy(keybytes, 0, keybytes16, 0,             math.min(keybytes.length, 16));      // convert plain text bytes     byte[] plainbytes = plaintext.getbytes("utf-8");      // setup cipher     secretkeyspec skeyspec = new secretkeyspec(keybytes16, "aes");     cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding");     byte[] iv = new byte[16]; // initialization vector 0     cipher.init(cipher.encrypt_mode, skeyspec, new ivparameterspec(iv));      // encrypt     byte[] encrypted = cipher.dofinal(plainbytes);     string encryptedstring = base64.encodetostring(             cipher.dofinal(plainbytes), base64.no_wrap);     // encryptedstring      return base64.encodetostring(encrypted, base64.no_wrap); } 

i encrypting using following code in windows 8 application

public string aes_encrypt(string input, string pass) {   symmetrickeyalgorithmprovider sap = symmetrickeyalgorithmprovider.openalgorithm(symmetricalgorithmnames.aescbcpkcs7);    string encrypted = "";   try   {     byte[] test1 = system.text.encoding.utf8.getbytes(pass);     byte[] test2 = new byte[16];      (int = 0; < test1.length;i++ )     {       test2[i] = test1[i];     }      cryptographickey key =     sap.createsymmetrickey(cryptographicbuffer.createfrombytearray(test2));      ibuffer buffer = cryptographicbuffer.createfrombytearray(system.text.encoding.utf8.getbytes(input));     encrypted = cryptographicbuffer.encodetobase64string(cryptographicengine.encrypt(key, buffer, null));      return encrypted;   }   catch (exception ex)   {     return null;   } } 

following decryption algo

public string aes_decrypt(string input, string pass) {    symmetrickeyalgorithmprovider sap = symmetrickeyalgorithmprovider.openalgorithm(symmetricalgorithmnames.aescbcpkcs7);    string decrypted = "";    try   {     byte[] test1 = system.text.encoding.utf8.getbytes(pass);     byte[] test2 = new byte[16];      (int = 0; < test1.length;i++ )     {       test2[i] = test1[i];     }      cryptographickey key =          sap.createsymmetrickey(cryptographicbuffer.createfrombytearray(test2));       ibuffer buffer = cryptographicbuffer.decodefrombase64string(input);     byte[] decrypted;      cryptographicbuffer.copytobytearray(cryptographicengine.decrypt(key, buffer, null), out decrypted);     decrypted = system.text.encoding.utf8.getstring(decrypted, 0, decrypted.length);     return decrypted;   }   catch (exception ex)   {     return null;   } } 

i know there wrong iv being generated. if give iv null, decryption algorithm generates result(though wrong), if give values iv, throws exception such "value not fall within expected range."

any appreciated.

i'm not sure transformation android windows noticed in java using pkcs5 , in c# pkcs7.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -