我正在编写一个将加密数据发送到外部服务器的应用程序。为此,我编写了加密/解密NSData缓冲区的函数。这些功能与iOS5完美配合,但不适用于iOS6。我没有修改代码。有人有解决方案吗?这是代码:
- (NSData*) AES256EncryptWithKey :(NSString*)pKey :(NSString*)pIV
{
// key length is incorrect?
if ([pKey length] != kCCKeySizeAES256)
{
M_LogErrorT("AE - incorrect value - " << [pKey length]);
return nil;
}
// is initialization vector used?
bool useIV = (pIV && [pIV length]);
// initialization vector length is incorrect?
if (useIV && [pIV length] != kCCBlockSizeAES128)
{
M_LogErrorT("AE - incorrect IV - " << [pIV length]);
return nil;
}
// key should be 32 bytes for AES256, will be null-padded otherwise
char pKeyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
bzero(pKeyPtr, sizeof(pKeyPtr)); // fill with zeroes (for padding)
// fetch key data
[pKey getCString:pKeyPtr maxLength:sizeof(pKeyPtr) encoding:NSUTF8StringEncoding];
// initialization vector should be 16 bytes for AES256
char pIVPtr[kCCBlockSizeAES128 + 1]; // room for terminator (unused)
bzero(pIVPtr, sizeof(pIVPtr)); // fill with zeroes (for padding)
// initialization vector is used?
if (useIV)
// fetch initialization vector data
[pIV getCString:pIVPtr maxLength:sizeof(pIVPtr) 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* pBuffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
pKeyPtr,
kCCKeySizeAES256,
useIV ? pIVPtr : NULL, /* initialization vector (optional) */
[self bytes],
dataLength, /* input */
pBuffer,
bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess)
// the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:pBuffer length:numBytesEncrypted];
// free the buffer
free(pBuffer);
return nil;
}
- (NSData*) AES256DecryptWithKey :(NSString*)pKey :(NSString*)pIV
{
// key length is incorrect?
if ([pKey length] != kCCKeySizeAES256)
{
M_LogErrorT("AE - incorrect value - " << [pKey length]);
return nil;
}
// is initialization vector used?
bool useIV = (pIV && [pIV length]);
// initialization vector length is incorrect?
if (useIV && [pIV length] != kCCBlockSizeAES128)
{
M_LogErrorT("AE - incorrect IV - " << [pIV length]);
return nil;
}
// key should be 32 bytes for AES256, will be null-padded otherwise
char pKeyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
bzero(pKeyPtr, sizeof(pKeyPtr)); // fill with zeroes (for padding)
// fetch key data
[pKey getCString:pKeyPtr maxLength:sizeof(pKeyPtr) encoding:NSUTF8StringEncoding];
// initialization vector should be 16 bytes for AES256
char pIVPtr[kCCBlockSizeAES128 + 1]; // room for terminator (unused)
bzero(pIVPtr, sizeof(pIVPtr)); // fill with zeroes (for padding)
// initialization vector is used?
if (useIV)
// fetch initialization vector data
[pIV getCString:pIVPtr maxLength:sizeof(pIVPtr) 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* pBuffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
pKeyPtr,
kCCKeySizeAES256,
useIV ? pIVPtr : NULL, /* initialization vector (optional) */
[self bytes],
dataLength, /* input */
pBuffer,
bufferSize, /* output */
&numBytesDecrypted);
if (cryptStatus == kCCSuccess)
// the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:pBuffer length:numBytesDecrypted];
// free the buffer
free(pBuffer);
return nil;
}
答案 0 :(得分:0)
我使用相同的代码,但没有初始化向量,这是完美的。 也许试着朝这个方向看。