为什么iPad上的AES加密和PHP的解密失败?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了为什么iPad上的AES加密和PHP的解密失败?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3693字,纯文字阅读大概需要6分钟。
内容图文
![为什么iPad上的AES加密和PHP的解密失败?](/upload/InfoBanner/zyjiaocheng/654/3fe02e0d881b4f63a46d938e4bb59bf3.jpg)
我有一个iPad应用程序,可以将加密的信息传输到基于PHP的网站,但是在正确解密此信息时遇到了困难.我将以下代码用于PHP端解密:
//Decryption function
function mc_decrypt($decrypt, $key, $iv)
{
$decoded = base64_decode($decrypt);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
mcrypt_generic_init($td, $key, $iv);
$decrypted = mdecrypt_generic($td, $decoded);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return trim($decrypted);
}
以及我的iPad应用程序中的这个Objective-C代码:
#import <CommonCrypto/CommonCryptor.h>
@implementation NSData (AES256)
- (NSData *)AES256EncryptWithKey:(NSString *)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer); //free the buffer;
return nil;
}
- (NSData *)AES256DecryptWithKey:(NSString *)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer); //free the buffer;
return nil;
}
@end
尝试解密在iPad上编码和在PHP端解密的数据时,为什么会看到数据损坏?
解决方法:
检查您正在使用的密钥.在PHP中,MCRYPT_RIJNDAEL_128 _256等常量不代表键强度,而是代表所使用的块大小.
要使用PHP获得128位加密,您需要使用16个字节长的密钥.对于256,您需要32个字节,依此类推.
您的PHP和C代码对我来说都是正确的.确保在两种情况下都正确使用了密钥.
换个想法.看来您在C中使用的是PKCS#7填充.我不认为PHP默认情况下是设计用于该填充的.如果内存对我有用,我相信Mcrypt函数使用空填充.
最后,检查PHP中的初始化向量.我注意到您没有在C代码中使用一个,这意味着您不应该在PHP中接受$iv变量.应该将它作为NULL传递给mcrypt函数(但强烈建议不要这样做).
相反,您应该做的是在C代码中随机化IV(正在加密数据),然后将IV与加密的数据一起发送.您可以检测正在使用的算法的IV大小,并将其从加密数据的前面拆分出来,然后用于填充PHP方面的内容.这样可以进一步保护您的加密.
内容总结
以上是互联网集市为您收集整理的为什么iPad上的AES加密和PHP的解密失败?全部内容,希望文章能够帮你解决为什么iPad上的AES加密和PHP的解密失败?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。