1

什么是在 iOS 中生成 RSA 密钥对并将它们作为String获取的最佳方法。我见过几个可以生成的库,但我无法将私钥作为字符串。有谁知道在字符串中获取私钥的库或方法?

目前我以这种方式生成密钥对

var statusCode: OSStatus
var publicKey: SecKey?
var privateKey: SecKey?

let publicKeyAttr: [NSObject: NSObject] = [kSecAttrIsPermanent:true as NSObject, kSecAttrApplicationTag:"publicTag" as NSObject]
let privateKeyAttr: [NSObject: NSObject] = [kSecAttrIsPermanent:true as NSObject, kSecAttrApplicationTag:"privateTag" as NSObject]

var keyPairAttr = [NSObject: NSObject]()
keyPairAttr[kSecAttrKeyType] = kSecAttrKeyTypeRSA
keyPairAttr[kSecAttrKeySizeInBits] = 2048 as NSObject?
keyPairAttr[kSecPublicKeyAttrs] = publicKeyAttr as NSObject?
keyPairAttr[kSecPrivateKeyAttrs] = privateKeyAttr as NSObject?

statusCode = SecKeyGeneratePair(keyPairAttr as CFDictionary, &publicKey, &privateKey)

if statusCode == noErr && publicKey != nil && privateKey != nil {

    print(publicKey!)
    print(privateKey!)

} else {
  print("Error generating key pair: \(statusCode)")
}

它在 SecKey 中。如何将它们转换为String?还是有其他方法可以做到这一点?

4

2 回答 2

1

这是使用的代码SecItemCopyMatching

let PublicKeyTag = "publicTag"
let PrivateKeyTag = "privateTag"

let publicKeyAttr: [NSString: Any] = [
    kSecAttrIsPermanent: NSNumber(value: true),
    kSecAttrApplicationTag: PublicKeyTag
]
let privateKeyAttr: [NSString: Any] = [
    kSecAttrIsPermanent: NSNumber(value: true),
    kSecAttrApplicationTag: PrivateKeyTag
]

let keyPairAttr: [NSString: Any] = [
    kSecAttrKeyType: kSecAttrKeyTypeRSA,
    kSecAttrKeySizeInBits: 2048 as NSObject,
    kSecPublicKeyAttrs: publicKeyAttr,
    kSecPrivateKeyAttrs: privateKeyAttr
]

var publicKey: SecKey?
var privateKey: SecKey?
var statusCode: OSStatus
statusCode = SecKeyGeneratePair(keyPairAttr as CFDictionary, &publicKey, &privateKey)

if statusCode == noErr && publicKey != nil && privateKey != nil {
    print(publicKey!)
    print(privateKey!)
} else {
    print("Error generating key pair: \(statusCode)")
}

var dataPtr: AnyObject?
let query: [NSString: Any] = [
    kSecClass: kSecClassKey,
    kSecAttrApplicationTag: PrivateKeyTag,
    kSecReturnData: NSNumber(value: true)
]
statusCode = SecItemCopyMatching(query as CFDictionary, &dataPtr)

let privateKeyData = dataPtr as! Data
let privateKeyString = privateKeyData.base64EncodedString(options: [])
print(privateKeyString)

privateKeyData似乎包含 ASN.1 编码信息。最终结果是 Base64 编码的。

于 2017-01-17T22:09:13.427 回答
0

如果对任何人有帮助,请尝试以下方法。使用 SecKey 类型的输入参数在代码中添加以下方法。

 func secKeyToString(key:SecKey) {
        var error:Unmanaged<CFError>?
        if let cfData = SecKeyCopyExternalRepresentation(key, &error) {
            let base64KeyString = (cfData as Data).base64EncodedString()
            print("Sec key in string:\(base64KeyString)")
        }
    }

您还可以使用相同的代码库创建 SecKey 类的扩展,并返回 base 64 编码字符串,如下所示: extension SecKey {

    func toString()-> String{
        var error:Unmanaged<CFError>?
        let cfData = SecKeyCopyExternalRepresentation(self, &error)
        let base64KeyString = (cfData as! Data).base64EncodedString()
        return base64KeyString
    }
}

使用方法如下:

//publicSecKey is your public key in SecKey format
let keyInStringFormat = publicSecKey.toString()
print("public key in string:\(keyInStringFormat)")
于 2020-07-24T13:17:41.993 回答