android - Unable to decrypt AES encrypted string from Objective C -


i want encrypt , decrypt in android , ios , php.

in android , in php using

  • encryption type: aes
  • encryption mode: cbc
  • padding : pkcs7padding
  • hash algorithm: sha-256

when encrypt , decrypt on android works perfectly. when try decrypt ios or php encrypted string in base64 or hex2binary. on android decrypt string first 16 character ios case , 19 character php code doest not decrypt showing other characters. pasting android code

            // ignore line encoding             //string input = "congratulation, you've sucessfully decoded!";              final byte[] iv = new byte[16];             arrays.fill(iv, (byte) 0x00);             ivparameterspec ivparameterspec = new ivparameterspec(iv);              // when tried gives "pad block corrupted" exception else work above told              /*byte[] key = commonutilities.encryptionkey.getbytes("utf-8");             system.out.println(key.length);             messagedigest sha = messagedigest.getinstance("sha-256");             key = sha.digest(key);             key = arrays.copyof(key, 16); // use first 128 bit             system.out.println(key.length);             system.out.println(new string(key,"utf-8"));             secretkeyspec secretkey = new secretkeyspec(key, "aes");*/              // encryptionkey = "12345678901234561234567890123456"; same in ios , php             secretkeyspec skeyspec = new secretkeyspec(commonutilities.encryptionkey.getbytes("utf-8"), "aes");             cipher ecipher = cipher.getinstance("aes/cbc/pkcs7padding");              // ignore these lines these encoding             /*ecipher.init(cipher.encrypt_mode, skeyspec, ivparameterspec);             byte[] dstbuff = ecipher.dofinal(input.getbytes("utf-8"));                           system.out.println("encrypted: " + new string(dstbuff, "utf-8"));              string enbin2hex = com.byte2hex(dstbuff);                 string en = base64.encodetostring(dstbuff, base64.default);*/                   // hex2binay ios gives me decrypt             // original text: "hello shani how doing , stuck in encryption ?"             string strbin2hex = "30bef4ab063d0d72f91d8d11a7adee1b1ec58f67c4d9cc20f59fb56b8b23b7c665198cff805897bd1afb82e578ac82c6c18c0ea909e17540d0b95a81e8446168";                ecipher.init(cipher.decrypt_mode, skeyspec, ivparameterspec);             byte[] de = ecipher.dofinal(com.hex2byte(strbin2hex));                //de = removetrailingnulls(de);             //int bytesdecryptedafter = de.length;              system.out.println("decrypted: " + new string(de, "utf-8"));             // decrypted string "igohj&t`hnh"kkr&are doing , stuck in encryption ?" 

here can see unable decrypt full string "hello shani how " missing characters.

in ios using

  • aes256
  • kccoptionpkcs7padding

    // ios encrypt code - (nsdata *)aes256encryptwithkey:(nsstring *)key { char keyptr[kcckeysizeaes256+1];     bzero(keyptr, sizeof(keyptr)); // fill zeroes (for padding)     // fetch key data [key getcstring:keyptr maxlength:sizeof(keyptr) encoding:nsutf8stringencoding];   nsuinteger datalength = [self length];   //see doc: block ciphers, output size less or  //equal input size plus size of 1 block.  //that's why need add size of 1 block here  size_t buffersize = datalength + kccblocksizeaes128;  void *buffer = malloc(buffersize);   size_t numbytesencrypted = 0;  cccryptorstatus cryptstatus = cccrypt(kccencrypt, kccalgorithmaes128, kccoptionpkcs7padding,                                   keyptr, kcckeysizeaes256,                                   "0000000000000000" /* initialization vector (optional) */,                                   [self bytes], datalength, /* input */                                   buffer, buffersize, /* output */                                   &numbytesencrypted);  if (cryptstatus == kccsuccess) {      //the returned nsdata takes ownership of buffer , free on deallocation      return [nsdata datawithbytesnocopy:buffer length:numbytesencrypted];  }   free(buffer); //free buffer;  return nil;  } 

any idea doing wrong.

thanks time

i see issues:

  • cipher mode (cbc in android code) isn't specified on ios
  • algorithm specified explicitly on ios (aes128), , not on android
  • algorithm not accord key size on ios 128/256.
  • initial vector different

so, instead of

cccryptorstatus cryptstatus = cccrypt(kccencrypt, kccalgorithmaes128, kccoptionpkcs7padding,                                   keyptr, kcckeysizeaes256,                                   "0000000000000000" /* initialization vector (optional) */,                                   [self bytes], datalength, /* input */                                   buffer, buffersize, /* output */                                   &numbytesencrypted);  if (cryptstatus == kccsuccess) {      //the returned nsdata takes ownership of buffer , free on deallocation      return [nsdata datawithbytesnocopy:buffer length:numbytesencrypted];  } 

i try

char iv[kccblocksizeaes128 + 1]; bzero(iv, sizeof(iv)) cccryptorstatus cryptstatus = cccrypt(kccencrypt, kccalgorithmaes, kccoptionpkcs7padding,                                   keyptr, kcckeysizeaes128,                                   iv,                                   [self bytes], datalength, /* input */                                   buffer, buffersize, /* output */                                   &numbytesencrypted);  if (cryptstatus == kccsuccess) {      //the returned nsdata takes ownership of buffer , free on deallocation      return [nsdata datawithbytesnocopy:buffer length:numbytesencrypted];  } 

ensure, android uses aes128


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -