AES same encryption/decryption in iOS and Android -
i want encrypt same text: "lorem ipsum dolor sit amet" ios , android.
key used is:"a01bd1be-9d28-11e2-a12e-48086188709b"
ios implementation:
- (nsdata *)aes256encryptwithkey:(nsstring *)key{ char keyptr[kcckeysizeaes256 + 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, kccoptionpkcs7padding, keyptr, kcckeysizeaes256, 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;}
android implementation:
byte[] rawkey = new byte[32]; system.arraycopy("a01bd1be-9d28-11e2-a12e-48086188709b".getbytes("utf-8"), 0, rawkey, 0, 32); cipher cipher = cipher.getinstance("aes/cbc/pkcs7padding"); final byte[] iv = new byte[16]; arrays.fill(iv, (byte) 0x00); ivparameterspec ivspec = new ivparameterspec(iv); secretkeyspec keyspec = new secretkeyspec(rawkey, "aes"); cipher.init(cipher.encrypt_mode, keyspec, ivspec); byte[] results = cipher.dofinal(cleartextbyte); string result = base64.encodetostring(results, base64.default);
i different result.
why ?
Comments
Post a Comment