-1

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?

Yasser Ajaj
  • 119
  • 1
  • 10
  • You should *not* use `AES_encrypt` and friends. That's a software-only implementation, so you will not enjoy hardware support, like AES-NI. You should be using `EVP_*` functions. See [EVP Symmetric Encryption and Decryption](http://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption) on the OpenSSL wiki. In fact, you should probably be using authenticated encryption because it provides *both* confidentiality and authenticity. See [EVP Authenticated Encryption and Decryption](http://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption) on the OpenSSL wiki. – jww Mar 19 '17 at 09:17
  • [Java AES 128 encrypting differently to openssl](http://stackoverflow.com/q/21086103/608639), [Java equivalent of an OpenSSL AES CBC encryption](http://stackoverflow.com/q/32508961/608639), [How to decode a string encoded with openssl aes-128-cbc using java?](http://stackoverflow.com/q/31947256/608639), [Using Java to decrypt openssl aes-256-cbc using provided key and iv](http://stackoverflow.com/q/15594518/608639), etc. – jww Mar 19 '17 at 09:18
  • Many thanks @jww, I'll try it. – Yasser Ajaj Mar 19 '17 at 10:02

1 Answers1

1

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.

Yasser Ajaj
  • 119
  • 1
  • 10