缓冲太小的SecKeyWrapper,怎么解决呢?

时间:2011-08-02 21:48:06

标签: iphone objective-c security aes

看我的代码:

  NSString *requestString = @"I am an encrypted piece of dataaaaaaaaaaaaaaaaaaaa";
NSData *xmlDaCriptare1 = [NSData dataWithBytes:[requestString UTF8String] length:[requestString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];


xmlDaCriptare1 = [requestString dataUsingEncoding:NSUTF8StringEncoding];

NSString *strcheck = [[NSString alloc] initWithData:xmlDaCriptare1 encoding:NSUTF8StringEncoding];

NSLog(@"%@", strcheck);

char *keya = "a16byteslongkey!";

NSData * symmetricKey = [NSData dataWithBytes:keya length:16];      
CCOptions pad = kCCOptionECBMode;  
NSData *xmlCriptato = [[SecKeyWrapper sharedWrapper] doCipher:xmlDaCriptare1  
                                                key:symmetricKey  
                                            context:kCCEncrypt 
                                            padding:&pad];  

总是回归:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Problem with encipherment ccStatus == -4301'

那是“缓冲太小”。有谁知道如何解决它? 谢谢, 奥蕾莉亚

1 个答案:

答案 0 :(得分:0)

我不确定你要做什么,你有

NSData *xmlDaCriptare1 = [NSData dataWithBytes:[requestString UTF8String] length:[requestString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];

但这样做会在NSData中重新创建完全相同的字节

xmlDaCriptare1 = [requestString dataUsingEncoding:NSUTF8StringEncoding];

来自

strcheck
NSString *strcheck = [[NSString alloc] initWithData:xmlDaCriptare1 encoding:NSUTF8StringEncoding];

永远不会发布,

我假设您正在使用来自apple CryptoExercise的演示示例中的 SecKeyWrapper ,所以您可能在代码中发现了一个错误(毕竟这只是一个演示程序),I建议你找出 SecKeyWrapper 失败的地方,如果你仍然无法找到该部分代码发生的错误。