我试图在钥匙串中添加密钥,然后用它加密数据。
NSData* peerTag = [@"Test PublicKey" dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary * peerPublicKeyAttr = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kSecClassKey, kSecClass,
(__bridge id)kSecAttrKeyTypeRSA, kSecAttrKeyType,
(__bridge id)kSecAttrKeyClassPublic, kSecAttrKeyClass,
peerTag, kSecAttrApplicationTag,
testPubKey, kSecValueData,
kCFBooleanTrue, kSecReturnPersistentRef,
nil];
OSStatus sanityCheck = noErr;
sanityCheck = SecItemDelete((__bridge CFDictionaryRef) peerPublicKeyAttr);
SecKeyRef persistPeer = NULL;
sanityCheck = SecItemAdd((__bridge CFDictionaryRef)peerPublicKeyAttr, (CFTypeRef *)&persistPeer);
NSData* passwordData = [@"password" dataUsingEncoding:NSUTF8StringEncoding];
NSData* encryptedPass = [[Methods sharedCenter]encrypt:passwordData usingKey:persistPeer];
size_t cipherBufferSize = SecKeyGetBlockSize(persistPeer);
SecKeyGetBlockSize(key);
会导致EXC_BAD_ACCESS代码= 1。
我之前没有使用过这个,所以我不确定会导致这个错误的原因。我在这做错了什么?
答案 0 :(得分:0)
你在哪里定义了“钥匙”? 检查它是否真的存在:if(key){... 我认为密钥没有定义或是NULL?