arik
arik

Reputation: 29240

PHP iOS AES Encryption Inconsistency

In PHP, I have the following code:

$key = 'HelloKey';
$data = 'This is a secret';
$mdKey = $key;
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mdKey, $data, MCRYPT_MODE_CBC));
echo $encrypted;

It prints: IRIl6K1tAUSwEBNmPXxnzgVobvTfCxwvQJGQmnf63UU=

EDIT: I have modified my code to not use zero-padding:

$key = 'HelloKey';
$data = 'This is a secret';
$mdKey = $key;

$block = mcrypt_get_block_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$pad = $block - (strlen($data) % $block);
$data .= str_repeat(chr($pad), $pad);

$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mdKey, $data, MCRYPT_MODE_CBC));
echo $encrypted;

It now prints: q2THgtCcd+r5kQV/W6gsU56dtfx+IEWPNc2MsjcHCNw=


In iOS, I have the following code:

NSString *key = @"HelloKey";
NSString *mdKey = key;

NSString *data = @"This is a secret";
NSData *plain = [data dataUsingEncoding:NSUTF8StringEncoding];

NSData *cipher = [SCEncryptionAES AES256EncryptData:plain withKey:mdKey];
NSLog(@"%@", [SCBase64 base64Encode:cipher length:cipher.length]);

It prints: f8pFZU7kdJNOriA0EBzFfTHsJFOG1MzCw7xV8ztLYQw=

Here's my AES256EncryptData: withKey-method:

+ (NSData *)AES256EncryptData:(NSData *)data withKey:(NSString *)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSUInteger dataLength = data.length;

    //See the doc: For block ciphers, the output size will always be less than or
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one 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) */,
                                          data.bytes, dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }

    free(buffer); //free the buffer;
    return nil;
}

I am not using initialization vectors or md5-key hashing which might overcomplicate this fairly simple example. Also, I have made sure that the base64-functions work identically, so the error is doubtlessly in the AES256EncryptData: withKey-method. So far, I have only seen this implementation (which I have impudently adopted). Is there something I do wrong?

Upvotes: 1

Views: 719

Answers (1)

vcsjones
vcsjones

Reputation: 141588

There are a few things that come to mind.

mcrypt_encrypt does not use PKCS#7 padding. It uses null padding:

If the size of the data is not n * blocksize, the data will be padded with '\0'.

However your iOS code specifies kCCOptionPKCS7Padding to perform PKCS#7 padding. You want to update either the iOS code or the PHP so they use the same padding technique. I would opt to change the PHP since using PKCS#7 is a better option than null byte padding.

The second thing that comes to mind (not knowing PHP very well I can't be sure) is how is PHP interpreting your string data? As ASCII, or UTF8? Your iOS code is using UTF8.

Upvotes: 1

Related Questions