我正在使用 RNCryptor 在 iOS 中加密消息,然后在 PHP 中对其进行解密。但是,特殊字符会从解密的字符串中消失。
我如何在 iOS 中加密
-(NSData *)encryptThis:(NSString *)str
{
NSString *key = @"mysuper32bitkey";
NSError *error = nil;
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [RNEncryptor encryptData:data
withSettings:kRNCryptorAES256Settings
password:key
error:&error];
return encryptedData;
}
我如何在 PHP 中解密
$b64_data = $message;
$password = "mysuper32bitkey"
// back to binary
$bin_data = $message;// message already in binary. No need for -> mb_convert_encoding($b64_data, "UTF-8", "BASE64");
// extract salt
$salt = substr($bin_data, 2, 8);
// extract HMAC salt
$hmac_salt = substr($bin_data, 10, 8);
// extract IV
$iv = substr($bin_data, 18, 16);
// extract data
$data = substr($bin_data, 34, strlen($bin_data) - 34 - 32);
// extract HMAC
$hmac = substr($bin_data, strlen($bin_data) - 32);
// make HMAC key
$hmac_key = pbkdf2('SHA1', $password, $hmac_salt, 10000, 32, true);
// make HMAC hash
$hmac_hash = hash_hmac('sha256', $data , $hmac_key, true);
// check if HMAC hash matches HMAC
if($hmac_hash != $hmac)
echo "false";
// make data key
$key = pbkdf2('SHA1', $password, $salt, 10000, 32, true);
// decrypt
$ret = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);
echo trim(preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\xFF]/u', '', $ret));
问题是,如果我在 iOS 中加密“Agresión”,PHP 只返回“Agresin”。为什么它不解密 "ó" ?我认为dataUsingEncoding:NSUTF8StringEncoding会保留特殊字符...我应该使用另一种类型的编码吗?