[Day]使用AES在php中加密,并用objective-c解密

本文展示了如何在PHP和iOS平台上使用AES算法进行加密和解密操作,并通过Base64编码。提供了PHP和iOS代码示例,包括密钥生成、加密、解密过程以及跨平台验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AES for iphone下载AES库

在不同平台进行AES加密,并使用Base64进行编码,结果有可能不同,但解密之后会一样,所以不要因为在2个平台加密结果不一样而认为自己错了。。。


php加密解密:

<?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);
?> 

iphone加密解密

#import "NSString+AESCrypt.h"
NSString *key = @"a16byteslongkey!a16byteslongkey!";
NSString *plaintext = @"iphone";
NSString *ciphertext = [plaintext AES256EncryptWithKey: key];
NSLog(@"ciphertext: %@", ciphertext);
plaintext = [ciphertext AES256DecryptWithKey: key];
NSLog(@"plaintext: %@", plaintext);


以下是一个在php中加密,然后在iphone中解密的例子
php 代码:

<?php
$key = "test_AES_key";

$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key = md5($key);
$plaintext = "ksfe";

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

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

iphone 代码:

-(NSString *)md5:(NSString *)str { 
    const char *cStr = [str UTF8String]; 
    unsigned char result[32]; 
    CC_MD5( cStr, strlen(cStr), result ); 
    return [NSString stringWithFormat: 
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3], 
            result[4], result[5], result[6], result[7], 
            result[8], result[9], result[10], result[11], 
            result[12], result[13], result[14], result[15] 
            ]; 
}

#import "NSString+AESCrypt.h"

NSString *strKey = @"test_AES_key";
NSData *chipherData = [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://192.168.157.1/index_aes.php"]];
    
NSString *chipherText = [[NSString alloc] initWithData: chipherData encoding: NSUTF8StringEncoding];
NSString *plainText = [chipherText AES256DecryptWithKey: [self md5: strKey]];
NSLog(@"plainText: %@", plainText);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值