为什么php是无法加密的,为什么iPad上的AES加密和PHP的解密失败?

我有一个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

@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端解密的数据时,为什么会看到数据损坏?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值