所以这是我关于Stack Overflow的第一个问题,它与钥匙串有关,因此,我阅读了有关钥匙串的苹果文档,他们提到可以通过“这是我的情况”来生成或获取钥匙,然后创建了“查询字典”就像他们说的那样,但是当我尝试添加它时,我得到一个错误,并且我的状态等于-50 。我不知道那是什么意思,也意味着我试图存储密钥的原因因此,我可以将其与“ SecKeyDecrypt” 一起使用,以解密需要将SecKey作为参数的邮件
更新:我发现返回代码的含义是-50 errSecParam,这意味着 “传递给该函数的一个或多个参数无效。” 然后我尝试删除 kSecValueRef作为字符串:privateKey 它可以正常工作,但是我仍然希望存储该密钥??
let privateKey = "myKey"
let tag = "mybunlde.com".data(using: .utf8)!
let addPrivateKey: [String: Any] = [kSecClass as String: kSecClassKey,
kSecAttrApplicationTag as String: tag,
kSecValueRef as String: privateKey]
let status = SecItemAdd(addPrivateKey as CFDictionary, nil)
guard status == errSecSuccess else { print("error while creating the key")
return
}
let getPrivateKey: [String: Any] = [kSecClass as String: kSecClassKey,
kSecAttrApplicationTag as String: tag,
kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
kSecReturnRef as String: true]
print("getPrivateKey \(getPrivateKey)")
} else {
print("no key found ")
}
答案 0 :(得分:0)
https://apple.stackexchange.com/questions/8993/how-can-i-add-a-private-key-to-my-keychain Adding private key into iOS Keychain
使用上面的链接.....
答案 1 :(得分:0)
您可以使用此库来存储和检索钥匙串中的数据 https://github.com/jrendel/SwiftKeychainWrapper
例如
let save: Bool = KeychainWrapper.standard.set("hello keychain", forKey: "key_name")
let get: String? = KeychainWrapper.standard.string(forKey: "key_name")