我在 PHP 中加密一个值,而另一个程序员在 iPhone 应用程序中解密它。我们都不能被称为加密知识。从我在本网站和其他地方找到的信息来看,一切看起来都是正确的。iPhone 的人说他从解密过程中获得了成功的返回码,但数据为空值。
这是PHP方面:
$key = getproperty("cryptkey","testkeytobechanged");
srand();
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, md5($key), $iv_base64."::::".$authority->get_mobiuser()->get_message(), MCRYPT_MODE_CBC, $iv));
$rv = $iv_base64.$encrypted;
这是 iPhone 端的内容:
//We have compared the hex values after base64 decoding enough that I am satisfied there is not an issue at that point.
NSString *b = [[dict objectForKey:@"data"]objectForKey:@"nlbal"];
NSData *iv = [NSData dataFromBase64String:[[b substringToIndex:22]stringByAppendingString:@"=="]];
NSLog(@"IV %@", [iv description]);
NSString *e = [b substringFromIndex:22];
NSData *encrypted = [NSData dataFromBase64String:e];
NSLog(@"ENC %@", encrypted);
NSUInteger dataLength = [encrypted length];
uint8_t unencryptedData[dataLength + kCCKeySizeAES128];
size_t unencryptedLength;
//NSData *un = [encrypted AES128DecryptWithKey:[SessionManager md5:@"testkeytobechanged"] iv:iv];
//From my research, I understand this to be the correct form to match the encryption I use on the PHP side
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128, 0,
[[SessionManager md5:@"testkeytobechanged"]UTF8String],
kCCKeySizeAES128, [iv bytes],
[encrypted bytes], dataLength,
unencryptedData, dataLength, &unencryptedLength);
NSString *output = [[NSString alloc] initWithBytes:unencryptedData length:unencryptedLength encoding:NSUTF8StringEncoding];
// output = [[NSString alloc]initWithData:un encoding:NSUTF8StringEncoding];
//NSLog(@"un %@", [un description]);
NSLog(@"OUTPUT %i %@", cryptStatus, output);
正如我上面所说,这段代码显然会产生一个成功代码,但有一个空值。