看来你正在使用这段代码
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
tempkey, kCCKeySizeAES256,
(void*)IV /* initialization vector (optional) */,
input_raw_data, data_length, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted );
我也遇到了同样的问题,我找到了不使用上述函数的解决方案,它会在加密时添加额外的字节。只需使用这两个函数而不是这个函数。这是我的解决方案
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc( bufferSize );
size_t numBytesEncrypted = 0;
CCCryptorRef ccRef;
CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES128, 0, tempkey, kCCKeySizeAES256, IV, &ccRef);
CCCryptorStatus cryptStatus = CCCryptorUpdate(ccRef, input_raw_data, data_length, buffer, bufferSize, &numBytesEncrypted);
CCCryptorRelease(ccRef);
if( cryptStatus == kCCSuccess )
{
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}