8

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 を取得できます。

上記のコードは、objective-c のこの回答から取得しました

したがって、これを解決するための正しいコードを取得するのを手伝ってくれる人がいれば、とても感謝しています:)

4

3 に答える 3

17

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 の場合:

It's a bit more complicate. You need a der file. It's a binary representation of your certificate. If you need to convert an existing certificate, you can do so with the following command:

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

The .crt or .pem file contains -----BEGIN CERTIFICATE----- as the first line.

Put the der file in your bundle and do:

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 ! Key now contains a SecKey representation of your public key. Happy Pinning.

于 2015-12-08T13:24:14.650 に答える
0

これが私がこれをした方法です:

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 でエンコードされた証明書であることを確認する必要があります。関連する公開鍵を取得するには、サーバーの秘密鍵で署名された証明書が必要です。

于 2015-04-26T21:48:46.280 に答える
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

}
于 2015-08-05T07:19:42.733 に答える