1

I am trying to use RNCryptor to do AES256 encryption at iOS side and AES256 decryption at remote site in PHP. But I am not able to get the correct decrypted data with php. Please help review if anything I am doing wrong.

Below is my code for iOS.

 NSString *key = @"1234567890123456789012";
 NSData *encryptedData = [RNEncryptor encryptData:data
                                    withSettings:kRNCryptorAES256Settings
                                        password:key
                                           error:&error];

Then I post the encryptedData to server which is in PHP. Below is my PHP code.

$key ="1234567890123456789012"  //32-bit key
$username = aes256Decrypt ($key, $username);

function aes256Decrypt($key, $data) {
    if(32 !== strlen($key)) $key = hash('SHA256', $key, true);
    $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, 
                str_repeat("\0", 16));
    $padding = ord($data[strlen($data) - 1]);
    $result = substr($data, 0, -$padding);
    return substr($data, 0, -$padding);
}
jbtule
  • 31,383
  • 12
  • 95
  • 128
jacksun
  • 91
  • 1
  • 7

1 Answers1

1

Based on some quick research, it appears that RNCryptor has its own output format. An example is shown below (taken from the page I linked):

DESCRIPTION: | version/cryptor | options | encryptionSalt | HMACSalt |  IV   | ... ciphertext ... |   HMAC   |
 BYTE INDEX: |        0        |    1    |      2-9       |  10-17   | 18-33 | <-      ...     -> | n-32 - n |

Your PHP code is going to have to extract the data from this format before attempting to decrypt. You will need the IV value and the ciphertext in order to retrieve the original plaintext.

Alternatively, switch to a different method of encryption for iOS that doesn't invent its own data format.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • It sounds right. I used AES256EncryptWithKey as alternative. It works as I expected. But the AES256EncryptWithKey approach has serious security issue as pointed in here (http://stackoverflow.com/questions/9794383/aes256-decryption-issue-in-objective-c). I think I need to figure out how to make RNCryptor with my PHP code. – jacksun Nov 30 '12 at 09:17
  • barduck's answers. Details see [here](https://github.com/rnapier/RNCryptor/issues/39#issuecomment-10952128). 1. Use lower level functions in iOS to create the encryption key and IV yourself to be later used in PHP (but then, why use RNCryptor in the first place) 2. Extract these parts from the RNCryptor format in PHP and call the PHP decrypt functions with the relevant parts of it (note you need to use PBKDF2 to recreate the key from the pass + salt). 3. Or you extract it from the RNCryptor format in iOS and submit it separately (but will still need to create the key from pass + salt). – jacksun Dec 04 '12 at 02:44
  • 1
    Note that any correct use of AES would need an output format something like the RNCryptor format. There is no good standard for encoding the required data. The closest is CMS, which is a very complicated data format that would have added a lot of overhead. Everyone creates their own format. OpenSSL has one. aescrypt has another. Neither of those encode sufficient data to correctly secure AES, so I was forced to create yet another one. If there is a standard format available that includes all the required pieces, I'd be interested in switching to it. – Rob Napier Jan 02 '13 at 17:09
  • Did you end up using one of the solutions [here](https://github.com/rnapier/RNCryptor/issues/39#issuecomment-10952128) or a new encrypt/decrypt API? I was wondering how to do #2. Extract these parts from the RNCryptor format in PHP and call the PHP decrypt functions with the relevant parts of it (note you need to use PBKDF2 to recreate the key from the pass + salt). – GabCas Mar 12 '13 at 17:26