Andrew Ho
Andrew Ho

Reputation: 638

Decrypting AES-256-CBC in Objective C

I am building an iPhone app which gets a decrypted string via JSON from a PHP backend.

In PHP I am encrypting the string like this:

$encrypt_method = "AES-256-CBC";
    $secret_key = 'This is my secret key';
    $secret_iv = 'This is my secret iv';

    // hash
    $key = hash('sha256', $secret_key);

    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);

    if( $action == 'encrypt' ) {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    }

In Objective C I tried to decrypt this string with BBEAS: https://github.com/benoitsan/BBAES

This is the code I have got in Objective C:

   NSData* salt = [BBAES IVFromString:@"This is my secret iv"];

    NSData *key = [BBAES keyBySaltingPassword:@"This is my secret key" salt:salt keySize:BBAESKeySize256 numberOfIterations:BBAESPBKDF2DefaultIterationsCount];
 NSData *decryptedMessage = [BBAES decryptedDataFromString:@"RlVnd01XOE5teTNseDFGQ3JScVhkQT09" IV:salt key:key];
    NSLog(@"Decrypted message: %@", decryptedMessage);

The log only shows a null object now.

I have found a duplicate post for C#: How to decrypt an AES-256-CBC encrypted string

EDIT: Lets say that i can adjust the encoding in PHP. How should I encrypt the string in PHP to be decrypted in Objective C?

Upvotes: 2

Views: 4446

Answers (2)

Levi
Levi

Reputation: 7343

You are not doing the same thing in PHP as in iOS. I am not familiar with this BBAES framework, but what you seem to have is a password from which you are generating a 256 bit AES key using PBKDF key derivation, and using that to decrypt the data. However, in PHP you are hashing your password and using it to encrypt your data, so you are probably using different AES keys for encryption and decryption. And I am not sure that IVs match either.

What you should do is:

In PHP, generate a random 16 byte IV for every encryption you do and use PBKDF key derivation to generate the 256 bit AES key from your password. Keep in mind that the salt and the number of iterations have to be the same in both PHP and iOS. After the encryption, append the IV to the encrypted data and send it.

In iOS, extract the IV from the received ciphertext (the last 16 bytes), generate the AES key from your password the same way you did before using the same salt and number of iterations, and decrypt the data (without the 16 byte IV at the end)

Edit:

As @Zaph pointed out, I forgot to mention that you should use also the same type of padding. BBAES seem to use PKCS7 padding.

Upvotes: 3

WaterNotWords
WaterNotWords

Reputation: 1007

To decrypt in Objective C you can use Apples's version of the CommonCrypto C library. It has a man page and there are already several posts that show decryption examples on Stack Overflow for example:

Determine if key is incorrect with CCCrypt kCCOptionPKCS7Padding-Objective C

which comes from the tutorial here:

http://robnapier.net/aes-commoncrypto

This also really helped me:

CCCrypt decrypting in AES CBC works even without IV

If you have trouble getting it working post some code.

Upvotes: 1

Related Questions