0

使用 Swift 2.2;在 El Capitan 下运行的 Xcode 7.3.1

使用 SecKeyGeneratePair 创建了一个私钥/公钥对 使用此代码获得了我的公钥的副本......然后我对其进行编码,以便我可以通过蓝牙链接发送它。

internal func generateKeyPair(publicKeyTag: String, privateKeyTag:String, keySize: Int)   {

    let privateKeyAttr: [NSString: AnyObject] = [
        kSecAttrIsPermanent: true,
        kSecAttrApplicationTag: privateKeyTag.dataUsingEncoding(NSUTF8StringEncoding)!
    ]
    let publicKeyAttr: [NSString: AnyObject] = [
        kSecAttrIsPermanent: true,
        kSecAttrApplicationTag: publicKeyTag.dataUsingEncoding(NSUTF8StringEncoding)!
    ]
    let parameters: [NSString: AnyObject] = [
        kSecAttrKeyType: kSecAttrKeyTypeRSA,
        kSecAttrKeySizeInBits: keySize,
        kSecPrivateKeyAttrs: privateKeyAttr,
        kSecPublicKeyAttrs: publicKeyAttr
    ]

    let result = SecKeyGeneratePair(parameters, &publicKey, &privateKey)

   if errSecSuccess != result {
        print("generateKeyPair fail",errSecSuccess, result)
   } else {
       //print("\(publicKey)\n\n","\(privateKey)")

    var dataPtr: AnyObject?
    let query: [NSString:AnyObject] = [
        kSecClass: kSecClassKey,
        kSecAttrApplicationTag: publicKeyTag.dataUsingEncoding(NSUTF8StringEncoding)!,
        kSecReturnData: true
    ]
    let qResult = SecItemCopyMatching(query, &dataPtr)
    if (qResult == errSecSuccess) {
        let PublicKeyText = dataPtr as? NSData
        base64Encoded = PublicKeyText!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
        print("PublicKeyText \(base64Encoded)")
    }
    }
}

现在我可以使用此代码对我的 base64EncodedString blob 进行未编码...

 let data = NSData(base64EncodedString: superString8, options:   NSDataBase64DecodingOptions(rawValue:0))

但是如何使用 Swift 将其恢复为可用的 SecKey 对象?我找到了这个参考并开始翻译它;但我担心我在这里超出了我的深度?

从 base64 编码的字符串中获取 SecKeyRef

这是我到目前为止所管理的..

      let cert:SecCertificateRef!
            let policy:SecPolicyRef!
            cert = SecCertificateCreateWithData(kCFAllocatorDefault, data!)
            policy = SecPolicyCreateBasicX509();
            //var status:OSStatus!
            var publicKey: SecKeyRef!
            //var publicKeyPtr = withUnsafeMutablePointer(&publicKey, { $0 })
            var trust:SecTrust?
            let trustPtr = withUnsafeMutablePointer(&trust , { $0} )
            var certArray:[SecCertificateRef] = []
            certArray.append(cert)

            var unsafeVariable:UnsafePointer<Void>

            let certArrayPtr = withUnsafeMutablePointer(&unsafeVariable, {$0})
            var newTrustType: SecTrustResultType = UInt32(kSecTrustResultInvalid)
            let newTrustTypePtr = withUnsafeMutablePointer(&newTrustType, {$0})

            if (cert != nil) {
                //certArray[1] = {cert}()
               // let certs:[SecCertificateRef] = CFArrayCreate(kCFAllocatorDefault, certArrayPtr, 1, nil) as! [SecCertificateRef]
                let certs:[SecCertificateRef] = CFArrayCreate(kCFAllocatorDefault, certArrayPtr, 1, nil) as! [SecCertificateRef]
                var status = SecTrustCreateWithCertificates(certs, policy, trustPtr)
                if (status == errSecSuccess){
                    //status = SecTrustEvaluate(trust!, trustTypePtr)
                      status = SecTrustEvaluate(trust!, newTrustTypePtr)
                    // Evaulate the trust.
                   switch (Int(newTrustType)) {
                   case kSecTrustResultInvalid: break
                   case kSecTrustResultDeny: break
                   case kSecTrustResultUnspecified: break
                   case kSecTrustResultFatalTrustFailure: break
                   case kSecTrustResultOtherError: break
                    case kSecTrustResultRecoverableTrustFailure:
                        publicKey = SecTrustCopyPublicKey(trust!);
                        break;
                    case kSecTrustResultProceed:
                        publicKey = SecTrustCopyPublicKey(trust!);
                        break;
                    }

                }
            }
        } else {
            superString8 = superString8 + stringFromData!
        }
    }

在此处输入图像描述

4

1 回答 1

1

尝试这样的事情:

    let certArrayPtr = withUnsafeMutablePointer(&certArray, {$0})
    var newTrustType: SecTrustResultType = UInt32(kSecTrustResultInvalid)
    let newTrustTypePtr = withUnsafeMutablePointer(&newTrustType, {$0})

    if (cert != nil) {
        certArray[1] = {cert}()
        let certs = CFArrayCreate(kCFAllocatorDefault, unsafeBitCast(certArrayPtr, UnsafeMutablePointer<UnsafePointer<Void>>.self), 1, nil) as! [SecCertificateRef]

(这里的第一行和最后一行是不同的......不需要certArrayPtr是 a var,而且unsafeBitCast很恶心,但它应该让你克服障碍;我在这个相关问题中找到了它)。

于 2016-05-05T08:40:21.440 回答