【发布时间】:2017-04-27 03:41:08
【问题描述】:
我正在从服务器检索加密数据,加密是使用 PHP 中的以下代码完成的:
$password = '1234567890123456';
$iv_size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$encryptedString = openssl_encrypt('I am a testing string', "AES-256-CBC", $password, 0, $iv);
$json['encrypted_feed'] = base64_encode($iv . $encryptedString);
$json['iv'] = base64_encode($iv);
echo json_encode($json);
然后在以下 JSON 对象中从服务器检索数据后:
{
"encrypted_string":"IRcqgAMvXlEm17wUwrwwmE5NRmVrbUlpSEp4NUpta2JNdmMrdzhFOVVzRFR4bkVXUjluMVJwaXNYYTA9",
"iv":"IRcqgAMvXlEm17wUwrwwmA=="
}
然后在我的 iOS 应用程序中,我尝试通过各种方式对其进行解密:
1.使用CommonCrypto tools here 中的实用程序:
** responseObject 是字典
NSString *password = @"1234567890123456";
NSData *pwData = [password dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64FeedStr = [responseObject objectForKey:@"encrypted_feed"];
NSString *base64IVStr = [responseObject objectForKey:@"iv"];
NSData *base64FeedData = [[NSData alloc] initWithBase64EncodedString:b64FeedStr options:0];
NSData *base64IVData = [[NSData alloc] initWithBase64EncodedString:base64IVStr options:0];
NSData *decryptedFeedData = [b64FeedData decryptedDataUsingAlgorithm:kCCAlgorithmAES key:password initializationVector:b64IVData options:kCCOptionPKCS7Padding error:nil];
NSString *result = [[NSString alloc] initWithData:decryptedFeedData encoding:NSUTF8StringEncoding];
在此之后,我还尝试将开头附加的IV数据砍掉,然后按照上面的解密顺序进行,但结果保持不变。
// According to the header:
// @param iv: the IV used to encrypt the data or nil if the encryption uses the `BBAESEncryptionOptionsIncludeIV` parameter
// i.e. the IV is saved along with the ciphertext (the IV is stored as the first block of the encrypted data).
// I also tried passing the IV into it but no luck.
NSData *resultData = [BBAES decryptedDataFromString:b64FeedStr IV:nil key:pwData];
NSString *result = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
我也尝试在 php 中使用AES-128,但结果保持不变,即在生成的 NSString 中使用nil。
我对密码学的了解很浅。经过所有这些调整并得到相同的结果,我感到很沮丧,不知道哪个部分出了问题。
如果对如何解密和检索原始字符串有任何见解,我将不胜感激。非常感谢!
【问题讨论】:
标签: php ios objective-c encryption cryptography