1
我試圖生成RSA
鑰匙並將其保存到鑰匙串中。我生成兩個公鑰和一個私鑰。是否有可能在iOS中保存多個公鑰到鑰匙串,swift
public key one -> generated using SecKeyGeneratePair &
private key one -> generated using SecKeyGeneratePair
public key two -> generated using SecKeyCreateWithData
所有三個按鍵kSecAttrIsPermanent
屬性設置爲true
。
然後我檢索鍵下面的方法
func GetKeysfromKeyChain(tag: String) -> SecKey? {
let query : [String: Any] = [
String(kSecClass) : kSecClassKey,
String(kSecAttrKeyType): kSecAttrKeyTypeRSA,
String(kSecAttrApplicationTag): tag,
String(kSecReturnRef): true
]
var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)
if status == errSecSuccess {
print("key existed :")
return result as! SecKey?
}
return nil
}
用上面的方法,可以順利拿到public key one
和privat key one
。但對於public key two
它總是返回一個零值。不知道,請你幫忙。
我這是怎麼產生的公共密鑰的兩個
let data2 = Data.init(base64Encoded: serverPublicKey)
let keyDict:[NSObject:NSObject] = [
kSecAttrIsPermanent: true as NSObject,
kSecAttrApplicationTag: "com.marlonbrand.serverpublic".data(using: String.Encoding.utf8)! as NSObject,
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeySizeInBits: 2048 as NSObject,
kSecAttrKeyClass: kSecAttrKeyClassPublic
]
let publickeysi = SecKeyCreateWithData(data2! as CFData, keyDict as CFDictionary, nil)