iOS encryption AES128/CBC/nopadding why is not working? -


i have app needs encode data using aes/cbc/no padding. app ported on android. there encoding done this:

byte[] encodedkey = getkey();     secretkeyspec skeyspec = new secretkeyspec(encodedkey, "aes");     algorithmparameterspec paramspec = new ivparameterspec(initializationvector);      cipher cipher = cipher.getinstance("aes/cbc/nopadding");     cipher.init(cipher.encrypt_mode, skeyspec, paramspec);      int blocksize = cipher.getblocksize();     int diffsize = decrypted.length % blocksize;     system.out.println("cipher size: " + blocksize);     system.out.println("current size: " + decrypted.length);     if (diffsize != 0) {       diffsize = blocksize - diffsize;       byte[] olddecrypted = decrypted;       decrypted = new byte[decrypted.length + diffsize];       system.arraycopy(olddecrypted, 0, decrypted, 0, olddecrypted.length);       (int = 0; < diffsize; i++) {         decrypted[i + olddecrypted.length] = " ".getbytes()[0];       }       system.out.println("new size: " + decrypted.length);      }     return cipher.dofinal(decrypted); 

the initializationvector looks this:

private byte[] initializationvector = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 

on ios have encryption:

- (nsdata *)aes128encryptwithkey:(nsstring *)key  {     // 'key' should 16 bytes aes128, null-padded otherwise     char keyptr[kcckeysizeaes128+1]; // room terminator (unused)     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,                                            0x0000,                                           keyptr,                                            kcckeysizeaes128,                                           null /* 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; } 

the method described above part of category on nsdata. method called this:

nsdata *data = [@"4915200456727" datausingencoding:nsutf8stringencoding];     nsdata *cipher  = [data aes128encryptwithkey:@"@x#zddxekzerbbw6"];     nsstring *ecriptedstring = [nsstring stringwithformat:@"%.*s", [cipher length], [cipher bytes]]; 

the problem have don't receive same encrypted data on ios , android. on ios encrypted data has 0 bytes in length.

could give pointers on how encrypt string using aes128 cbc , no padding , perhaps example?

thank you

i found solution problem. in order make encryption work without padding had add 0x0000 instead of kccoptionpkcs7padding or kccoptionecbmode treated.

also if data needs encoded doesn't have length multiple of kcckeysizeaes128 ( 16 ) vector holds data must resized have length multiple kcckeysizeaes128 , empty values filled something. added spaces.

    - (nsdata *)aes128encryptwithkey:(nsstring *)key {     char keyptr[kcckeysizeaes128+1];     bzero(keyptr, sizeof(keyptr));      [key getcstring:keyptr maxlength:sizeof(keyptr) encoding:nsutf8stringencoding];      int datalength = [self length];     int diff = kcckeysizeaes128 - (datalength % kcckeysizeaes128);     int newsize = 0;      if(diff > 0)     {         newsize = datalength + diff;     }      char dataptr[newsize];     memcpy(dataptr, [self bytes], [self length]);     for(int = 0; < diff; i++)     {         dataptr[i + datalength] = 0x20;     }      size_t buffersize = newsize + kccblocksizeaes128;     void *buffer = malloc(buffersize);      size_t numbytesencrypted = 0;     cccryptorstatus cryptstatus = cccrypt(kccencrypt,                                           kccalgorithmaes128,                                           0x0000, //no padding                                           keyptr,                                           kcckeysizeaes128,                                           null,                                           dataptr,                                           sizeof(dataptr),                                           buffer,                                           buffersize,                                           &numbytesencrypted);      if(cryptstatus == kccsuccess)     {         return [nsdata datawithbytesnocopy:buffer length:numbytesencrypted];     }      return nil; } 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -