AES128加密在iOS 6和6中提供不同的结果。 IOS 7

时间:2013-10-09 15:33:01

标签: ios ios6 ios7 aes commoncrypto

好吧,我正在开发一个从Web服务器发送和接收加密数据的应用程序。 这里的问题是,我的加密结果不同。 现在,比较在iOS6和iOS上运行的结果iOS 7模拟器,我注意到十六进制也不同,我没有碰到代码。

以下是代码:

@implementation NSData (AES_128)
- (NSData *)AES128Encrypt {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES128+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;

char ivPtr[kCCBlockSizeAES128];

bzero(ivPtr, sizeof(ivPtr));

[iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];

CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                      keyPtr, kCCKeySizeAES128,
                                      ivPtr /* 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;
}

密钥和iv是静态的,16字节的char数组。 我在这里错过了什么?提前致谢

0 个答案:

没有答案