在iOS(Swift)中生成后获取RSA密钥对作为String?

时间:2017-01-17 09:11:27

标签: ios swift security cryptography security-framework

在iOS中生成RSA密钥对的最佳方法是什么,并将其作为 String 。我已经看到了几个可以生成的lib但我无法将私钥作为字符串。有没有人知道lib或方法来获取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中。如何将它们转换为字符串?还是有其他办法吗?

2 个答案:

答案 0 :(得分: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编码。

答案 1 :(得分: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编码的字符串,如下所示: 扩展名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)")