CCCrypt解密结果为-4304

时间:2012-07-11 17:20:14

标签: objective-c ios encryption commoncrypto

当我加密字符串然后立即解密时,CCCryptorStatus是-4304并且得到的解码字符串不是正确的长度。谁能看到我可能做错了什么?

//Encrypt

NSString *plainText = @"e22ae25e-0b20-433a-8aa5-a5678714f590";
NSData *rawData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
size_t outLength;
NSMutableData *cipherData = [NSMutableData dataWithLength:rawData.length + kCCBlockSizeAES128];
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, [AESKey bytes], [AESKey length],  NULL, [rawData bytes], [rawData length], [cipherData mutableBytes], [cipherData length], &outLength);

//[plainText length] = 36
//[rawData length] = 36
//[cipherData length] = 52
//outLength = 48

//Decrypt

NSMutableData *decodedData = [NSMutableData dataWithLength:cipherData.length + kCCBlockSizeAES128];

//[decodedData length] = 68

CCCryptorStatus cryptStatus2 = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, [AESKey bytes], [AESKey length], NULL, [cipherData bytes], [cipherData length], [decodedData mutableBytes], [decodedData length], &outLength);
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];

//[decodedData length] = 68
//[decodedString length] = 68

/*
    At this point, cryptStatus2 is -4304 and 
    decodedString = "e22ae25e-0b20-433a-8aa5-a5678714f590\f\f\f\f\f\f\f\f\f\f\f\f";

*/

1 个答案:

答案 0 :(得分:1)

根据头文件,这是kCCAlignmentError。尝试使用outlength代替[cipherData length]