如何导出公钥< SecKey>使用SecKeyGeneratePair生成在服务器上使用? [英] How do I export a public key <SecKey> that was generated using SecKeyGeneratePair to be used on a server?

查看:1453
本文介绍了如何导出公钥< SecKey>使用SecKeyGeneratePair生成在服务器上使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SecKeyGeneratePair生成了一名守门员。

I generated a keeper using SecKeyGeneratePair.

        var publicKeyPtr, privateKeyPtr: Unmanaged<SecKey>?

        let publicKeyParameters: [String: AnyObject] = [
            kSecAttrIsPermanent: true,
            kSecAttrApplicationTag: "com.example.site.public"
        ]
        let privateKeyParameters: [String: AnyObject] = [
            kSecAttrIsPermanent: true,
            kSecAttrApplicationTag: "com.example.site.private"
        ]
        let parameters: [String: AnyObject] = [
            kSecAttrKeyType: kSecAttrKeyTypeRSA,
            kSecAttrKeySizeInBits: 2048,
            kSecPublicKeyAttrs.takeUnretainedValue() as String: publicKeyParameters,
            kSecPrivateKeyAttrs.takeUnretainedValue() as String: privateKeyParameters
        ]
        let result = SecKeyGeneratePair(parameters, &publicKeyPtr, &privateKeyPtr)
        let publicKey = publicKeyPtr!.takeRetainedValue()
        let privateKey = privateKeyPtr!.takeRetainedValue()
        let blockSize = SecKeyGetBlockSize(publicKey)

如果我打印出publicKey,我可以看到模数,我很漂亮确定是我需要的:

If I print out the publicKey I can see the modulus, which I'm pretty sure is what I need:

publicKey: <SecKeyRef algorithm id: 1, key type: RSAPublicKey, version: 3, block size: 2048 bits, exponent: {hex: 10001, decimal: 65537}, modulus: B2A7BD90C909F8084AD5B34040ABDAF7D1A6AFBADB35F3B6AB5CDDAB473449B0F175DEA32A7476F339D98F4AB3716AA2C1476D4009A80574B984DDFA1EF1A2550E48C46791CEFBFC39EF281049AA74E4C734C3B2A7B3F621B8A41F8B6689C4978696690D4EF9FFF0F90DB85C8ECBCF721FB7652AD7B337880A09D97EA736008C3ADBB72223F18C522C0C0889B05122561042D8637D1CBEF8F9F5AE88CDC43E411AA217E2A81C2D812B46D01C3BDC2799DFF3EAD46BB092A566E18EE94F63C4690ECE806B993FDDAC3159BE2098C2428F24969C109E221D8F066BEE3530848DE328D888B4C7E701435EACB116F97BB77B9379EF818B4D280890262EE678B92705, addr: 0x144841a00>

但我无法弄清楚如何导出密钥以便我可以将其发送到我的服务器以便在那里使用。

But I cannot figure out how to export the key so I can send it to my server for use there.

根据我的理解。 SecKey存储在Keychain中,是指向它的指针,块大小是内存中密钥的长度。所以从理论上讲,我可以将其提取为NSData,然后将其转换为我的服务器可以读取的内容。从理论上讲,我认为这样可行,但我在实践中试图做到这一点。所有帮助将不胜感激。

From my understanding. A SecKey is stored in Keychain and is a pointer to it, the block size is the length of the key in the memory. So in theory I can extract it as NSData and then convert it to something my server can read. In theory I think that will work, I've hit a wall trying to do that in practice. All help will be greatly appreciated.

推荐答案

SecItemCopyMatching 是用于你:

SecItemCopyMatching is for you:

var dataPtr:Unmanaged<AnyObject>?
let query: [String:AnyObject] = [
    kSecClass: kSecClassKey,
    kSecAttrApplicationTag: "com.example.site.public",
    kSecReturnData: kCFBooleanTrue
]
let qResult = SecItemCopyMatching(query, &dataPtr)

// error handling with `qResult` ...

let publicKeyData = dataPtr!.takeRetainedValue() as NSData

// convert to Base64 string
let base64PublicKey = publicKeyData.base64EncodedStringWithOptions(nil)



Swift 4:



Swift 4:

var dataPtr:CFTypeRef?
let query: [String: Any] = [
    kSecClass as String: kSecClassKey,
    kSecAttrApplicationTag as String: "com.example.site.public",
    kSecReturnData as String: true
]

let qResult = SecItemCopyMatching(query as CFDictionary, &dataPtr)

// error handling with `qResult` ...

let data = dataPtr as! Data
let base64PublicKey = data.base64EncodedString()

注意数据的大小是270,与密钥的块大小不同。请参阅crypto.stackexchange.com上的此问题

Note that the size of the data is 270, not the same as block size of the key. See this question on the crypto.stackexchange.com.

这篇关于如何导出公钥&lt; SecKey&gt;使用SecKeyGeneratePair生成在服务器上使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆