如何使用Objective-C在iOS上进行相同的加密/解密PHP功能? [英] How can I do this same encrypt/decrypt PHP function on iOS with Objective-C?
问题描述
函数encrypt_decrypt($ action,$ string)
{
$ output = false;
$ key ='mykeyhereblah';
$ iv = md5(md5($ key));
if($ action =='encrypt'){
$ output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,md5($ key),$ string,MCRYPT_MODE_CBC,$ iv);
$ output = base64_encode($ output);
}
else if($ action =='decrypt'){
$ output = mcrypt_decrypt(MCRYPT_RIJNDAEL_256,md5($ key),base64_decode($ string),MCRYPT_MODE_CBC,$ iv)
$ output = rtrim($ output,);
}
return $ output;
}
我的电话是这样的:
echo encrypt_decrypt('decrypt','2Fa9cICuUFa / UnmAAa5FjXZK4ht9q3cN2qgk1pCvDSs =');
如何在iOS上使用Objective-C作为一个的NSString
?它需要与这个PHP函数兼容。
所以你想使用CBC模式的AES256进行加密。
您正在寻找的库是CommonCrypto,您可以在这里找到一篇很好的文章: http:// robnapier.net/aes-commoncrypto 。
您还需要一个MD5功能,您可以在这里找到: http://www.makebetterthings.com/iphone/how- to-get-md5-and-sha1-in-objective-c-ios-sdk /
你的代码应该是这样的:
NSString * originalString,* keyString;
NSData * key = [[self md5:keyString] dataUsingEncoding:NSUTF8StringEncoding];
NSData * iv = [[self md5:[self md5:key]] dataUsingEncoding:NSUTF8StringEncoding];
NSData * data = [originalString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData * cipherData = [NSMutableData dataWithLength:data.length + kCCBlockSizeAES128]; // MCRYPT_RIJNDAEL_256的块大小就像AES128
size_t outLength;
CCCryptorStatus结果
= CCCrypt(kCCEncrypt,//操作,用kCCDecrypt替换解密
kCCAlgorithmAES,//与MCRYPT_RIJNDAEL_256相同
无,// CBC模式
key.bytes,// key
32,//因为你使用AES256
iv.bytes,// iv
data.bytes,// dataIn
data .length,// dataInLength,
cipherData.mutableBytes,// dataOut
cipherData.length,// dataOutAvailable
& outLength); // dataOutMoved
NSString resultString = [cipherData base64Encoding];
并确保您在这两种情况下使用相同的UTF8编码,并使用此导入: p>
#import< CommonCrypto / CommonCryptor.h>
我很确定这应该可以工作。
编辑:密钥长度应为32,因为您使用AES256 256bit = 32bytes。默认情况下,MD5的输出不符合此长度。
I have a function in PHP that encrypts and decrypts strings:
function encrypt_decrypt($action, $string)
{
$output = false;
$key = 'mykeyhereblah';
$iv = md5(md5($key));
if( $action == 'encrypt' ) {
$output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
$output = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, $iv);
$output = rtrim($output, "");
}
return $output;
}
and I call it like this:
echo encrypt_decrypt('decrypt', '2Fa9cICuUFa/UnmAAa5FjXZK4ht9q3cN2qgk1pCvDSs=');
How can I do this exact same thing on iOS with Objective-C for an NSString
? It needs to be compatible with this PHP function.
So you want to encrypt using AES256 in CBC mode. The library you are looking for is CommonCrypto and you can find a good article about it here:http://robnapier.net/aes-commoncrypto.
You will also need an MD5 function that you can find here: http://www.makebetterthings.com/iphone/how-to-get-md5-and-sha1-in-objective-c-ios-sdk/
Your code should look something like this:
NSString *originalString,*keyString;
NSData *key = [[self md5:keyString] dataUsingEncoding:NSUTF8StringEncoding];
NSData *iv = [[self md5:[self md5:key]] dataUsingEncoding:NSUTF8StringEncoding];
NSData *data = [originalString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *cipherData = [NSMutableData dataWithLength:data.length + kCCBlockSizeAES128]; //The block size of MCRYPT_RIJNDAEL_256 is just like AES128
size_t outLength;
CCCryptorStatus result
= CCCrypt(kCCEncrypt, // operation, replace with kCCDecrypt to decrypt
kCCAlgorithmAES, // Same as MCRYPT_RIJNDAEL_256
nil, // CBC mode
key.bytes, // key
32, // Since you are using AES256
iv.bytes,// iv
data.bytes, // dataIn
data.length, // dataInLength,
cipherData.mutableBytes, // dataOut
cipherData.length, // dataOutAvailable
&outLength); // dataOutMoved
NSString resultString = [cipherData base64Encoding];
And make sure you are using the same UTF8 encoding in both cases, and use this import:
#import <CommonCrypto/CommonCryptor.h>
I am pretty sure this should work.
EDIT: the key length should be 32 since you are using AES256 256bit=32bytes. The MD5 output wouldn't match this length by default I think.
这篇关于如何使用Objective-C在iOS上进行相同的加密/解密PHP功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!