我使用以下代码使用AES128 CBC加密来加密NSData 没有带键和IV的填充,它没有给出相同的输出 android而android的输出是正确的。
- (NSData*)AES128Encrypt:(NSData *)dataToEncrypt iv:(NSString *)iv key:(NSString *)key {
char keyPtr[kCCKeySizeAES128 + 1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
char ivPtr[kCCBlockSizeAES128 + 1];
bzero(ivPtr, sizeof(ivPtr));
if (iv) {
[iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
}
NSUInteger dataLength = [dataToEncrypt length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
ccNoPadding,
keyPtr,
kCCBlockSizeAES128,
ivPtr,
[dataToEncrypt bytes],
dataLength,
buffer,
bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}
答案 0 :(得分:0)
您需要为Key和IV执行Base64解码。