如何在核心数据中存储大小为4096的kSecAttrKeyTypeRSA类型的公钥和私钥而不是钥匙串

时间:2019-04-03 14:54:39

标签: ios swift keychain

我已经使用RSA4096生成了公钥和私钥,并且没有通过将kSecAttrIsPermanent设置为false来存储到钥匙串中。

现在,我正在尝试将SecKey存储到核心数据中,但无法将SecKey转换为NSData。请建议

let privateKeyParams: [String: AnyObject] = [
    kSecAttrIsPermanent as String: false as AnyObject,
    kSecAttrApplicationTag as String:"com.RSAKey.MyApp" as AnyObject
]

// public key parameters
let publicKeyParams: [String: AnyObject] = [
    kSecAttrIsPermanent as String: false as AnyObject,
    kSecAttrApplicationTag as String:"com.RSAKey.MyApp" as AnyObject
]

// global parameters for our key generation
let parameters: [String: AnyObject] = [
    kSecAttrKeyType as String:          kSecAttrKeyTypeRSA,
    kSecAttrKeySizeInBits as String:    4096 as AnyObject,
    kSecPublicKeyAttrs as String:       publicKeyParams as AnyObject,
    kSecPrivateKeyAttrs as String:      privateKeyParams as AnyObject,
]

var pubKey, privKey: SecKey?
let status = SecKeyGeneratePair(parameters as CFDictionary, &pubKey, &privKey)

1 个答案:

答案 0 :(得分:0)

您可以使用SecKeyCopyExternalRepresentation()将SecKey转换为数据。