我想使用AES加密Objective-C中的密码,然后用
PHP解密,但我有两个问题.
>我加密密码,但它是一个NSData对象,所以我使用base64对其进行编码,但是当我在PHP中解码时,结果为零.所以我不能解密它.
>我可以在Objective-C中加密和解密密码,因此问题就是PHP,但是当我用AES加密然后用base64编码时,结果就不一样了.
这是我的代码:
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."
";
$ciphertext = base64_decode($ciphertext);
$plaintext = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext, MCRYPT_MODE_ECB, $iv);
echo "plaintext: ".$plaintext."
";
输出:
ciphertext: SXNepKfh0IrlDDdkq4EdmQ==
plaintext: iphone
NSString *key = @"a16byteslongkey!";
NSString *plaintext = @"iphone";
NSString *ciphertext = [plaintext AES256EncryptWithKey: key];
NSLog(@"ciphertext: %@", ciphertext);
plaintext = [ciphertext AES256DecryptWithKey: key];
NSLog(@"plaintext: %@", plaintext);
输出:
ciphertext: D19l3gsgXJlrLl7B2oCT6g==
plaintext: iphone
我用kCCKeySizeAES128替换kCCKeySizeAES256,并将“kCCOptionPKCS7Padding”替换为“kCCOptionPKCS7Padding | kCCOptionECBMode”,