본문 바로가기

iPhone dev.

AES 암호화/복호화 관련

공개된 클래스: https://github.com/rnapier/RNCryptor (기존의 CommonCrypto의 wrapper)

stackoverflow의 질문:

I want to use AES to encrypt a password in Objective-C, and then decrypt it in PHP, but I have two problems.

  1. I encrypt the password, but it's an NSData object, so I encode it with base64, but when I decode in PHP, the result is nil. So I can't decrypt it.
  2. I can encrypt and decrypt the password in Objective-C, so it is the PHP that is the problem, but when I encrypt with AES and then encode with base64, the results are not the same.

Here is my code:

PHP:

    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
    $iv
= mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $key
= "a16byteslongkey!";
    $plaintext
= "iphone";
    $ciphertext
= mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_ECB, $iv);
    $ciphertext
= base64_encode($ciphertext);
    echo
"ciphertext: ".$ciphertext."<br/>";

    $ciphertext
= base64_decode($ciphertext);
    $plaintext
= mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext, MCRYPT_MODE_ECB, $iv);
    echo
"plaintext: ".$plaintext."<br/>";

output:

    ciphertext: SXNepKfh0IrlDDdkq4EdmQ==
    plaintext
: iphone

Objective-C: (Get the full source code here: https://gist.github.com/838614)

    NSString *key = @"a16byteslongkey!";
   
NSString *plaintext = @"iphone";

   
NSString *ciphertext = [plaintext AES256EncryptWithKey: key];
   
NSLog(@"ciphertext: %@", ciphertext);

    plaintext
= [ciphertext AES256DecryptWithKey: key];
   
NSLog(@"plaintext: %@", plaintext);

output:

    ciphertext: D19l3gsgXJlrLl7B2oCT6g==
    plaintext
: iphone

i replace kCCKeySizeAES256 with kCCKeySizeAES128, and replace "kCCOptionPKCS7Padding" with "kCCOptionPKCS7Padding | kCCOptionECBMode",

link|edit
4 
Right off the bat it looks like you're using 128 in PHP and 256 in Obj-C... That'll be a problem. – Corey Ogburn Aug 23 '11 at 15:43
thanks for you reply, i replace kCCKeySizeAES256 with kCCKeySizeAES128, and replace "kCCOptionPKCS7Padding" with "kCCOptionPKCS7Padding & kCCOptionECBMode", but the results are also not the same. is there any detail i should change? – pcrazyc Aug 23 '11 at 16:06
2 
you seem to be base64_encode-ing the resulting string in PHP but only converting it to UTF8 in Obj-C. Obviously, the results will be different. – Aleks G Aug 23 '11 at 17:43
maybe you should have a look at NSString+AESCrypt.m, it encode with base64 in "- (NSString *)AES256EncryptWithKey:(NSString *)key" – pcrazyc Aug 24 '11 at 2:01
feedback

i have slove the problem.

  1. don't change the code from https://gist.github.com/838614
  2. the key should be 32 byte.
  3. the results of encryt are not the same, but they'll be the same if you decrypt.

objective-c:

NSString *key = @"a16byteslongkey!a16byteslongkey!";
NSString *plaintext = @"iphone";

NSString *ciphertext = [plaintext AES256EncryptWithKey: key];
NSLog(@"ciphertext: %@", ciphertext);

plaintext
= [ciphertext AES256DecryptWithKey: key];
NSLog(@"plaintext: %@", plaintext);

output:

ciphertext: I3chV+E2XUHeLCcJAhBaJQ==
plaintext
: iphone

php:

$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv
= mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key
= 'a16byteslongkey!a16byteslongkey!';
$plaintext
= "iphone";

$ciphertext
= mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_ECB);
$base64encoded_ciphertext
= base64_encode($ciphertext);
echo
"ciphertext: ".$base64encoded_ciphertext."<br/>";

$plaintext
= mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($base64encoded_ciphertext), MCRYPT_MODE_ECB);
echo
"plaintext: ".$plaintext."<br/>";

$base64encoded_ciphertext
=  "I3chV+E2XUHeLCcJAhBaJQ==";
$plaintext
= mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($base64encoded_ciphertext), MCRYPT_MODE_ECB);
echo
"plaintext: ".trim($plaintext);

output:

ciphertext: kUr+YsYtb3Uy34li/GPcjg==
plaintext
: iphone
plaintext
: iphone
link|edit