来自Swift中服务器的公钥字符串的Seckey

时间:2015-03-02 10:59:10

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

我想使用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

获得了上面的代码

因此,如果任何人可以帮助我找到正确的代码来解决这个问题,我将非常感激:)

3 个答案:

答案 0 :(得分:14)

对于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!)!;
}

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

答案 1 :(得分:0)

我是这样做的:

<select ng-model="selectedBean" ng-options="bean.name for bean in beans | filter:idInArray:idsFilter">
</select>

这有效,但您需要确保传递给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(); } } 的是DER编码的证书,而不仅仅是DER编码的密钥。您需要使用服务器私钥签名的证书来获取关联的公钥。

答案 2 :(得分:0)

我这是用过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

}