Android AES decryption and data from iOS:javax.crypto.BadPaddingException: pad block corrupted -


i tried decrypt backup on android sent ios, , exception javax.crypto.badpaddingexception: pad block corrupted showed @ method dofinal.

public  string decrypt(byte[] ciphertext, secretkey key, byte [] initialvector) throws exception {     cipher cipher = cipher.getinstance("aes/cbc/pkcs7padding");     ivparameterspec ivparameterspec = new ivparameterspec(initialvector);     cipher.init(cipher.decrypt_mode, key, ivparameterspec);     ciphertext = cipher.dofinal(ciphertext);      return new string(ciphertext, "utf-8"); } 

the key , initialvector sent ios in base64 string. related code:

public static byte[] decodewebsafe(string s) throws base64decoderexception {     byte[] bytes = s.getbytes();     return decodewebsafe(bytes, 0, bytes.length); }  byte[] iv = base64.decodewebsafe(enciv); byte[] salt = base64.decodewebsafe(encsalt); byte[] data = base64.decodewebsafe(encdata); secretkey key = security.getexistingkey(password, salt); string original = aes.decrypt(data, key, iv); 

and security.getexistingkey:

public static secretkey getexistingkey(string password, byte[] salt) throws exception{     secretkey key= null;     keyspec keyspec = new pbekeyspec(password.tochararray(), salt, 10000, 256);     secretkeyfactory keyfactory = secretkeyfactory.getinstance("pbkdf2withhmacsha1");      byte[] keybytes=new byte[32];      keybytes = keyfactory.generatesecret(keyspec).getencoded();     key= new secretkeyspec(keybytes, "aes");      return key; } 

thx solutions.

p.s.this how set encryption in ios:

cccryptorstatus cryptstatus = cccrypt(kccencrypt, kccalgorithmaes128, kccoptionpkcs7padding, self.encryptionkey.bytes, kcckeysizeaes128, self.encryptioniv.bytes, [rawdata bytes], datalength,  /* input */buffer, buffersize, /* output */&numbytesencrypted); 

the key , iv derivation method:

(nsdata *)keyforpassword:(nsstring *)password salt:(nsdata *)salt { nsmutabledata * derivedkey = [nsmutabledata datawithlength:kcckeysizeaes128];  int result = cckeyderivationpbkdf(kccpbkdf2,            // algorithm                               password.utf8string,                                password.length,                                 salt.bytes,           // salt                               salt.length,          // saltlen                               kccprfhmacalgsha1,    // prf                               kpbkdfrounds,         // rounds                               derivedkey.mutablebytes, // derivedkey                               derivedkey.length); // derivedkeylen } 

i can see several differences in way generate key:

  • in ios, key of 16 bytes / 128 bit generated; in android it's 256 bits.
  • in ios, password utf-8 encoded while android either takes lower 8 bits or full 16 bits of each characters (i don't know details of specific algorithm).
  • in ios, pass invalid length password (the number of characters instead number of bytes in utf-8 encoding).

you better invest time in better matching key generation , comparing keys before decryption.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -