I've a Java web service returns AES byte array, and other function returns encrypted data (byte array) with this key,
From the other side, I've ios app that calls get AES key, then calls get encrypted data, I included OpenSSL lib on ios app, and it will decrypt encrypted data with AES key that returned before,
My code in Java:
public static byte[] encryptWithAES(byte[] message, Key AESkey) throws Exception {
byte[] decryptedMessage = null;
try {
Cipher encrypt = Cipher.getInstance("AES");
encrypt.init(Cipher.ENCRYPT_MODE, AESkey);
decryptedMessage = encrypt.doFinal(message);
} catch (Exception ex) {
System.err.println(ex.getMessage());
throw ex;
}
return decryptedMessage;
}
The code in ios:
unsigned char* ucKey = (unsigned char*)[self.AESKeyByte bytes];
AES_KEY wctx;
AES_set_encrypt_key(ucKey, 256, &wctx);
unsigned char* dataDec = (unsigned char *)calloc(size, sizeof(unsigned char*));
unsigned char* encryptedData = (unsigned char*)[value bytes];
AES_decrypt(encryptedData, dataDec, &wctx);
int sizeoddatadec = (int)strlen((char*)dataDec);
NSData* decryptedData = [NSData dataWithBytes:(const void *)dataDec length:sizeof(unsigned char)*sizeoddatadec];
NSString* stringDec = [NSString stringWithUTF8String:[decryptedData bytes]];
The AES key on java is:
ec d6 55 8b 43 70 e9 e4 9f 8c 62 d4 42 b2 c7 46 19 ff 13 c8 10 bb d0 04 ed e4 5b 78 11 1d c7 5d
plain text is: "AChamieh"
And the self.AESKeyByte on ios is:
ec d6 55 8b 43 70 e9 e4 9f 8c 62 d4 42 b2 c7 46 19 ff 13 c8 10 bb d0 04 ed e4 5b 78 11 1d c7 5d
Decrypted data: 64 ee 6a b8 2c 29 16 17 9f 78 cf cb b6 ad e5 cd
Any help for this issue please?