2017-06-01 223 views
9

我使用SecKeyGeneratePair創建公鑰和私鑰的密鑰對。如何使用Swift 3生成RSA公鑰和私鑰?

的ViewController

import UIKit 
import Security 

class ViewController: UIViewController { 

    @IBOutlet weak var textFld: UITextField! 
    @IBOutlet weak var encryptedTextFld: UITextView! 
    @IBOutlet weak var decryptedTextFld: UITextView! 

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

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let publicKeyAttr: [NSObject: NSObject] = [kSecAttrIsPermanent:true as NSObject, kSecAttrApplicationTag:"com.xeoscript.app.RsaFromScrach.public".data(using: String.Encoding.utf8)! as NSObject] 
     let privateKeyAttr: [NSObject: NSObject] = [kSecAttrIsPermanent:true as NSObject, kSecAttrApplicationTag:"com.xeoscript.app.RsaFromScrach.private".data(using: String.Encoding.utf8)! 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("Key pair generated OK") 
      print("Public Key: \(publicKey!)") 
      print("Private Key: \(privateKey!)") 
     } else { 
      print("Error generating key pair: \(String(describing: statusCode))") 
     } 

    } 

    @IBAction func encryptPressed(_ sender: Any) { 

    } 

    @IBAction func decryptPressed(_ sender: Any) { 

    } 

然後我拿到了鑰匙像下面

公鑰

<SecKeyRef algorithm id: 1, key type: RSAPublicKey, version: 4, block size: 2048 bits, exponent: {hex: 10001, decimal: 65537}, modulus: B4854E2DA6BA5EBC96C38BD44078D314E4504D130A03018ACD17D0F6679C3B6C9937B5D932A635AEAC32B9245EC400208C1F79932174EF804468D0DCE40DAF5B544CF9E4BCD7C49BA5D0BF3F8246B89B57A3A910CBB5200DCA6145E3EE216CE9C4A3283F1027AA15F7543BD3BEFF35BE24EE709CF8EB12545970AFFDA38CA11410ECA20A8F428D228ED07BF5399A2F55D93D7C143BAFA59A08E4FF932C3A689FA7F3F166B79A43837028319CB383F716B594F317ED6E20D7A8003190A13BC132D5B13708EDAEA3E2012B16CF37437BB617070D9A6DDFE55884A79BD530E4E654B823A8BBBF0AA777C8E46E94BD83E1C59EC6E1D34E69405640C309515243AA8D, addr: 0x608000420e80> 

私鑰

<SecKeyRef algorithm id: 1, key type: RSAPrivateKey, version: 4, block size: 2048 bits, addr: 0x60000003b960> 

但我不希望像生成以上。

我除了喜歡按鍵下方

公鑰

-----BEGIN RSA PUBLIC KEY----- 
MIIBCgKCAQEA0bipoOhkkvPxcsyOzcqsIUeVe0+iwe8W7N4EbHZMgujRERu1TPpy 
UcCO0uuKmm1TU09Kl40rRvDbtgB1YcGV3FPnNp3sOyFVsdyZ5bzxZtyyLrSWtj/n 
bLnGwaG9xJSwd2R/pTQLzOLV5KldwD2eUb3Z4Z4e9Z8II7eWgGaCLLqbrtEAa05N 
EqARckxrzJ1S3j+59h4AQovF72KI90/kRPryT2OGDiVlJ6CTjn2ZnTYcx65X6Rwf 
AeJKHZAGhw96j9tXyS+dJcXy4IBUTi3PXw0aEfhHQr/JsSHuMp/8mrhVJEokXb1C 
gKDZgJXujpGhCBdztHBAJxLBQMlODg7srwIDAQAB 
-----END RSA PUBLIC KEY----- 

私鑰

-----BEGIN RSA PRIVATE KEY----- 
MIIEowIBAAKCAQEAsfxMagVKY5++61Kot0esyhEOesqyQlZNvWbqMBcOoaOAb3pk 
LvwaGJ2YtD12u4yDEKcY5rpX7B/2t8GBHf+74NG47zAutf4Gf6qgQRUmIx2b7i4k 
WBt3KIifb/Zfs9KVJLhD4007bg1OtXA4kIhhXiuvhajDjDLOEthogF45CkJe+N67 
JnH5hVW5CqBxPyRCrWCFbEHcXs5H515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jagToopxujAHBYpUZT 
qwbMpWzbvl89ZM8VLrdY/V7en+89P/+OnRJvjgUTiRrQ4npmVs59rgLvPRamXzGJ 
A1u4MFTuoZNnxgMqOaQprzlfv6lBSHpxlOl/HpByfcJAENBd2LtgRZv4r6+JY9hD 
M8bgvQKBgCDTSCLj5c1CYyuJMdrz9L5+xLFmrmL48djhK460ZcmcZ/gP808CyXx/ 
sDneow+JWt7Jb3p5zyUvvq1aDGNSsn4plB2rg7AqtoHcZYyFFZGI/K/b6JZna1yu 
FUYOfcanunabxY1wPQxuvR+AEuufBjB0aKg+qkLCCN1HYQtLs+N8 
-----END RSA PRIVATE KEY----- 
  1. 如何通過SecKeyGeneratePair生成的密鑰轉換爲-----BEGIN RSA PUBLIC KEY----- ................ -----END RSA PUBLIC KEY----- 格式
+0

可以試試這個鏈接https://github.com/trailofbits/SecureEnclaveCrypto/tree/master/SecureEnclaveSwift –

回答

0

首先需要獲取原始比特流(數據)表示密鑰對see how to do it here

無論需要什麼,是表示所獲得的數據,與頁眉和頁腳如在您的示例,其通常被稱爲只是base64編碼串PEM格式

如何?檢查data.base64EncodedString()

警告!導出私鑰是...很抱歉的話...愚蠢的想法

2

請試試這個:

let publicKeyAttr: [NSObject: NSObject] = [ 
      kSecAttrIsPermanent:true as NSObject, 
      kSecAttrApplicationTag:"com.xeoscript.app.RsaFromScrach.public".data(using: String.Encoding.utf8)! as NSObject, 
      kSecClass: kSecClassKey, // added this value 
      kSecReturnData: kCFBooleanTrue] // added this value 
let privateKeyAttr: [NSObject: NSObject] = [ 
      kSecAttrIsPermanent:true as NSObject, 
      kSecAttrApplicationTag:"com.xeoscript.app.RsaFromScrach.private".data(using: String.Encoding.utf8)! as NSObject, 
      kSecClass: kSecClassKey, // added this value 
      kSecReturnData: kCFBooleanTrue] // added this value 

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("Key pair generated OK") 
    var resultPublicKey: AnyObject? 
    var resultPrivateKey: AnyObject? 
    let statusPublicKey = SecItemCopyMatching(publicKeyAttr as CFDictionary, &resultPublicKey) 
    let statusPrivateKey = SecItemCopyMatching(privateKeyAttr as CFDictionary, &resultPrivateKey) 

    if statusPublicKey == noErr { 
     if let publicKey = resultPublicKey as? Data { 
      print("Public Key: \((publicKey.base64EncodedString()))") 
     } 
    } 

    if statusPrivateKey == noErr { 
     if let privateKey = resultPrivateKey as? Data { 
      print("Private Key: \((privateKey.base64EncodedString()))") 
     } 
    } 
} else { 
    print("Error generating key pair: \(String(describing: statusCode))") 
} 
0

你需要使用SecKeyCopyExternalRepresentation方法公鑰/私鑰對轉換爲外部表現形式,然後轉換您的數據轉換爲base64編碼的字符串,它只是PEM格式,使用base64EncodedString(options:)方法。 Refer the code here