【问题标题】:Seckey from public key string from server in Swift来自 Swift 中服务器的公钥字符串的 Seckey
【发布时间】:2015-05-02 16:55:23
【问题描述】:

我想使用 RSA 加密数据,我试图在我的代码中生成密钥并且它正在工作,但我真正需要的是从服务器获取公钥作为字符串,然后将其用作 Seckey,以便我可以使用它使用 RSA 加密数据, 我试过这段代码:

//KeyString is the string of the key from server
let KeyData = (keyString as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!


    var cert : Unmanaged<SecCertificateRef>!;
    var  policy : Unmanaged<SecPolicy>!;
    cert = SecCertificateCreateWithData(kCFAllocatorDefault, KeyData);
    policy = SecPolicyCreateBasicX509();
    var status : OSStatus = noErr
    var trust: SecTrust?
    var certArray : [Unmanaged<SecCertificateRef>!] = [cert];
    var certArrayPointer = UnsafeMutablePointer<UnsafePointer<Void>>(certArray)
    status = SecTrustCreateWithCertificates(cert, policy, trust);
    let publicKey: SecKeyRef = SecTrustCopyPublicKey(trust!).takeUnretainedValue()

我无法运行此代码,因为 SecTrustCreateWithCertificates 方法期望证书为 anyObject! ,我不知道如何解决这个问题,如果解决这个问题会让我得到 SecKey。

我从this answer in objective-c 得到了上面的代码

所以如果有人可以帮助我获得正确的代码来解决这个问题,我将非常感激:)

【问题讨论】:

    标签: ios swift public-key-encryption encryption-asymmetric seckeyref


    【解决方案1】:

    Mac 版:

    let pubKey = "-----BEGIN PUBLIC KEY-----MIICIjANBgAgK.......InbFk1FkucQqruMyUCAwEAAQ==-----END PUBLIC KEY-----"
    let pubKeyData = pubKey.dataUsingEncoding(NSASCIIStringEncoding)
    var error: Unmanaged<CFErrorRef>?
    let secKey = SecKeyCreateFromData(NSDictionary(), pubKeyData!, &error)
    

    其中 pubKey 是您的公钥的字符串表示形式。 如果您不知道您的公钥,您可以使用以下命令从您的私钥中推断出来:

    openssl rsa -in server.key -pubout  > mykey.pub
    

    其中 server.key 是包含 -----BEGIN RSA PRIVATE KEY----- 的文件 作为第一行。

    对于 iOS:

    这有点复杂。 您需要一个der 文件。它是您的证书的二进制表示。 如果需要转换现有证书,可以使用以下命令:

     openssl x509 -outform der -in file.crt|pem -out mycert.der
    

    .crt.pem 文件包含 -----BEGIN CERTIFICATE----- 作为第一行。

    der 文件放入您的包中并执行以下操作:

    let certificateData = NSData(contentsOfURL:NSBundle.mainBundle().URLForResource("mycert", withExtension: "der")!)
    
    let certificate = SecCertificateCreateWithData(nil, certificateData!)
    
    var trust: SecTrustRef?
    
    let policy = SecPolicyCreateBasicX509()
    let status = SecTrustCreateWithCertificates(certificate!, policy, &trust)
    
    if status == errSecSuccess {
        let key = SecTrustCopyPublicKey(trust!)!;
    }
    

    亚塔!密钥现在包含您的公钥的SecKey 表示。快乐固定。

    【讨论】:

    • pubKey 的解释将使答案更加有用。
    • @zaph 我编辑了答案并添加了获得 SecKey 所需的所有过程;我不确定在不解释整个证书/RSA 的情况下我还能说什么关于什么是公钥......
    • 这是否意味着我们只能使用der文件,不能在iOS中使用pem key?
    • 这没有回答“服务器发送的公钥字符串中的 SecKey”问题
    • 您可以使用 PEM 密钥,但您必须去除页眉和页脚,并使用 Data(base64Encoded: base64string) 解码 base64 字符串,这将为您提供可以传递给 SecCertificateCreateWithData 的数据。见:stackoverflow.com/questions/10579985/…
    【解决方案2】:

    我是用 Alamofire 做的:

    private static func publicKeyForCertificate(certificate: SecCertificate) -> SecKey? {
        var publicKey: SecKey?
        var trust: Unmanaged<SecTrust>?
    
        let policy = SecPolicyCreateBasicX509().takeRetainedValue()
        let status = SecTrustCreateWithCertificates(certificate, policy, &trust)
    
        if status == errSecSuccess {
            let trustRef = trust!.takeRetainedValue()
            publicKey = SecTrustCopyPublicKey(trustRef)!.takeRetainedValue()
    
        }
        return publicKey
    
    }
    

    【讨论】:

      【解决方案3】:

      我是这样做的:

      let cert = SecCertificateCreateWithData(kCFAllocatorDefault, certData)?.takeRetainedValue()
      
      if cert != nil {
          var trust: Unmanaged<SecTrust>?
      
          let policy = SecPolicyCreateBasicX509().takeRetainedValue()
          let status = SecTrustCreateWithCertificates(cert, policy, &trust)
      
          if status == errSecSuccess {
              let trustRef = trust!.takeRetainedValue()
              let key = SecTrustCopyPublicKey(trustRef)!.takeRetainedValue();
          }
      }
      

      这可行,但您需要确保传递给 SecCertificateCreateWithData() 的是 DER 编码的证书,而不仅仅是 DER 编码的密钥。您需要一个由您的服务器私钥签名的证书才能获取关联的公钥。

      【讨论】:

        猜你喜欢
        • 2019-06-09
        • 2019-11-10
        • 1970-01-01
        • 1970-01-01
        • 2017-09-25
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 2021-07-22
        相关资源
        最近更新 更多