Php 无法在iOS中解密Base64编码的AES-256-CBC加密字符串,始终导致零字符串

Php 无法在iOS中解密Base64编码的AES-256-CBC加密字符串,始终导致零字符串,php,ios,objective-c,encryption,cryptography,Php,Ios,Objective C,Encryption,Cryptography,我正在从服务器检索加密数据,加密是通过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

我正在从服务器检索加密数据,加密是通过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。使用中的实用程序:

**
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数据,然后按照上面的解密顺序进行解密,但结果保持不变

2。使用

我还尝试在php中使用
AES-128
,但结果保持不变,即在生成的NSString中使用
nil

我对密码学的理解很肤浅。经过所有这些相同的结果调整后,我感到沮丧,不知道哪一部分出了问题

如果有任何关于如何解密和检索原始字符串的见解,我将非常感激。非常感谢

  • MCRYPT\u CAST\u 256
    指定不推荐使用的CAST加密算法,而不是AES

  • 不要使用Cast,使用AES

  • AES是MCRYPT_RIJNDAEL_128(MCRYPT没有说清楚)

  • 在加密过程中使用了IV,并在加密数据前加上前缀。解密时必须使用not。Base64解码、分割IV和加密数据

  • 最好使用完整的跨平台库,例如。从加密原语中很难获得正确和安全的加密


  • 最好不要使用mcrypt,它已经被废弃近十年了。因此,它已被弃用,将从核心中删除,并在PHP7.2中放入PECL。它不支持标准的PKCS#7(née PKCS#5)填充,只支持非标准的空填充,甚至不能用于二进制数据。mcrypt在2003年就有很多杰出的作品。相反,考虑使用或提供完整的解决方案,并保持正确。
    // 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];