[英]Objective C - SecItemAdd has error:EXC_BAD_ACCESS (first time) and errSecDuplicateItem (second time)
I use this code http://wiki.effectiveprogramming.com/index.php?title=CocoaEncryption&redirect=no in my application to implement Login function. But I have a error method saveRSAPublicKey:我在我的应用程序中使用此代码http://wiki.effectiveprogramming.com/index.php?title=CocoaEncryption&redirect=no来实现登录 function。但是我有一个错误方法 saveRSAPublicKey:
+ (BOOL)saveRSAPublicKey:(NSData*)publicKey appTag:(NSString*)appTag overwrite:(BOOL)overwrite {
//Error here (when first call) - Program received signal: "EXC_BAD_ACCESS" -> crash
OSStatus status = SecItemAdd((CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
(id)kSecClassKey, kSecClass,
(id)kSecAttrKeyTypeRSA, kSecAttrKeyType,
(id)kSecAttrKeyClassPublic, kSecAttrKeyClass,
kCFBooleanTrue, kSecAttrIsPermanent,
[appTag dataUsingEncoding:NSUTF8StringEncoding], kSecAttrApplicationTag,
publicKey, kSecValueData,
kCFBooleanTrue, kSecReturnPersistentRef,
nil],
NULL); //don't need public key ref
DebugLog(@"result = %@", [KeychainUtil fetchStatus:status]);
if(status == noErr)
return YES;
else if(status == errSecDuplicateItem && overwrite == YES)
return [CryptoUtil updateRSAPublicKey:publicKey appTag:appTag];
return NO;
}
When I call saveRSAPublickey
for first time, I can't create OSStatus and my application is crashed.当我第一次调用saveRSAPublickey
时,我无法创建 OSStatus 并且我的应用程序崩溃了。 When I call saveRSAPublickey
for second time: status == errSecDuplicateItem
and run [CryptoUtil updateRSAPublicKey:publicKey appTag:appTag]
-> success.当我第二次调用saveRSAPublickey
时:status == errSecDuplicateItem
并运行[CryptoUtil updateRSAPublicKey:publicKey appTag:appTag]
-> 成功。
It's difficult to find this problem when debugging because it just only appear in new device ( have never been installed my app).调试时很难发现这个问题,因为它只出现在新设备上(从未安装过我的应用程序)。 Finally, I have found the error by the way: set new appTag before call saveRSAPublicKey.最后顺便发现错误:set new appTag before call saveRSAPublicKey。 I see in system.log:我在 system.log 中看到:
Apr 6 12:30:29 MACs-MacBook-Pro securityd[4372]: unable to access hwaes key 4 月 6 日 12:30:29 MACs-MacBook-Pro securityd[4372]:无法访问 hwaes 密钥
Please help me.!!请帮我。!! Thanks all.谢谢大家。
If you pass NULL
for the 2nd arg (where your result would come out), then you're not allowed to have a Return Type Key
set when calling SecItemAdd()
(change kCFBooleanTrue, kSecReturnPersistentRef
to kCFBooleanFalse, kSecReturnPersistentRef
or just delete it).如果您为第二个 arg 传递NULL
(您的结果将在此处出现),则不允许在调用SecItemAdd()
时设置Return Type Key
(将kCFBooleanTrue, kSecReturnPersistentRef
更改为kCFBooleanFalse, kSecReturnPersistentRef
或将其删除)。
I submitted a bug report on this just now.我刚才提交了一份错误报告。
I would guess that publicKey or, more likely, appTag don't contain what you think they do.我猜想 publicKey 或者更可能是 appTag 不包含您认为的内容。 Try logging them out.尝试将它们注销。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.