2017-08-24 34 views
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 oneprivat 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) 

回答

0

您是否尝试过设置你的keyDict[String : Any]的类型,做相应的蒙上你需要他们?如果我不得不猜测,对NSObject的强制转换可能会搞砸了。