【发布时间】:2015-04-15 20:25:56
【问题描述】:
我对 iOS 上的加密还很陌生,我遇到了一个我无法找到解决方案的错误:
每当我尝试获取 iOS 钥匙串中公钥的 SecKeyRef 并使用它时,都会出现 EXC_BAD_ACCESS 错误。 SecKeyRef(在我下面的代码中称为“publicKeyReference”,最初设置为NULL,但调用SecItemCopyMatching方法后应该有一个值,从调试器窗口中的内存地址可以看出。
这是我的代码:
SecKeyRef publicKeyReference = NULL;
NSData* publicTag = [publicKeyIdentifier dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *queryPublicKey = [[NSMutableDictionary alloc] init];
// Set the public key query dictionary.
[queryPublicKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
[queryPublicKey setObject:publicTag forKey:(__bridge id)kSecAttrApplicationTag];
[queryPublicKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
[queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnPersistentRef];
// Get the key.
sanityCheck = SecItemCopyMatching((__bridge CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyReference);
// Encrypt using the public.
sanityCheck = SecKeyEncrypt( publicKeyReference,
PADDING,
plainBuffer,
plainBufferSize,
&cipherBuffer[0],
&cipherBufferSize
);
下面是一些错误和调试窗口的截图:
似乎某些东西被分配给了 SecKeyRef,因为地址的值不是“0x0”,但无论我尝试了什么,我都不断收到 EXC_BAD_ACCESS 错误。非常感谢您对此问题的任何和所有帮助。
【问题讨论】:
-
你看过 sanityCheck 的第一个值了吗?它可能会显示来自 SecItemCopy 调用的错误消息。另外,请注意您正在使用 kSecReturnPersistentRef,根据 SecItem 中的注释“表明应返回对项目 (CFDataRef) 的持久引用”。所以响应可能不是 SecKeyRef 类型。
标签: ios null exc-bad-access public-key-encryption seckeyref