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?
For ECB:
The java code will be
public static byte[] encryptWithAES(byte[] message, Key AESkey) throws Exception {
byte[] decryptedMessage = null;
try {
Cipher encrypt = Cipher.getInstance("AES/ECB/PKCS5Padding"); // here is the change
encrypt.init(Cipher.ENCRYPT_MODE, AESkey);
decryptedMessage = encrypt.doFinal(message);
} catch (Exception ex) {
System.err.println(ex.getMessage());
throw ex;
}
return decryptedMessage;
}
For CBC:
The java code will be
public static byte[] encryptWithAES(byte[] message, Key AESkey, byte[] iv) throws Exception {
byte[] decryptedMessage = null;
try {
Cipher encrypt = Cipher.getInstance("AES/CBC/PKCS5Padding"); // here is the change
encrypt.init(Cipher.ENCRYPT_MODE, AESkey,new IvParameterSpec(iv)); // and I add the iv for cbc
decryptedMessage = encrypt.doFinal(message);
} catch (Exception ex) {
System.err.println(ex.getMessage());
throw ex;
}
return decryptedMessage;
}
Edit (2):
Depends on @zaph said the previous objective c will take twice process,
I updated the objective c code :
- (NSData *) AESECB : (NSData *) data withKey :(NSData *) key : (CCOperation) operation{
CCCryptorStatus ccStatus = kCCSuccess;
NSUInteger dataLength = data.length;
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );
size_t numBytes = 0;
ccStatus = CCCrypt(operation, // will pass kCCDecrypt or kCCEncrypt
kCCAlgorithmAES,
kCCOptionECBMode | kCCOptionPKCS7Padding,
[key bytes],
[key length],
nil,
[data bytes],
[data length],
buffer,
bufferSize,
&numBytes);
if( ccStatus == kCCSuccess )
{
return [NSData dataWithBytes:buffer length:numBytes];
}
free(buffer);
return nil;
}
- (NSData *) AESCBC : (NSData *) data withKey: (NSData *) key : (CCOperation) operation{
CCCryptorStatus ccStatus = kCCSuccess;
NSUInteger dataLength = data.length;
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );
size_t numBytes = 0;
ccStatus = CCCrypt(operation, // will pass kCCDecrypt or kCCEncrypt
kCCAlgorithmAES,
kCCOptionPKCS7Padding,
[key bytes],
[key length],
self.IV.bytes,
[data bytes],
[data length],
buffer,
bufferSize,
&numBytes);
if( ccStatus == kCCSuccess )
{
return [NSData dataWithBytes:buffer length:numBytes];
}
free(buffer);
return nil;
}
Many thanks @zaph for your advice again.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments