开发者

Use AES to encrypt with Objective-C and decrypt with PHP

开发者 https://www.devze.com 2023-03-29 22:33 出处:网络
I want to use AES to encrypt a password in Objective-C, and then decrypt it in PHP, but I have two problems.

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",


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


fixed use some thing like

$iv2 = '';
    for ($i = 0; $i < 16; $i++) {
        $iv2 .= "\0";
    }
   mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($text), MCRYPT_MODE_CBC, $iv2);
  1. don't change the code from https://gist.github.com/838614
  2. the key should be 32 byte
  3. the results of encryt and decrypt are not the same

I have tried to test with this string:

$plaintex = "fskfladsadsadfsfs dfskl;dfs a jadfsa ds'a' j afdjdfsaadfs' jdfas af 'ksfegfffffffffffffffffffffsdfsfgfsfdsdfddfsg"

and the results are different. Anybody here know what the reason could be?


I suspect that it's the padding routine. I've approached this by making sure that the text to be encrypted is padded to 32 char with spaces, and before the result is returned from teh decryption routine, trim off the extra spaces.

There are official padding algorithms, but I found they didn't work. If you encrypt a string that is a multiple of 32 characters long, then even if the padding routine is wrong it will ignore it.

0

精彩评论

暂无评论...
验证码 换一张
取 消