首页 > 解决方案 > RSA Key 似乎只包含它的内存地址。斯威夫特,MacOS

问题描述

我目前正在尝试为 Mac 应用程序生成 CSR,并缩小了我的密钥生成范围内的问题。

我目前正在使用下面的代码来生成密钥,这在大多数情况下都有效。但是,从我一直使用的许多参考资料来看,预期的输出应该是:

<SecKeyRef算法id:1,密钥类型:RSAPrivateKey,版本:4,块大小:2048位,地址:0x600001ae74c0>

但是,在生成密钥后在调试中检查 SecKeyRef 输出时,我得到的只是:

<SecKeyRef: 0x600000cbd6e0>

我怀疑这就是我在 Red Kestrel 上检查 CSR 时发现密钥大小为 0 并且无法读取 CSR 签名的原因。

只是想知道这是否可能是为什么?

        let publicKeyParameters: [String: Any] = [
        String(kSecAttrIsPermanent): true,
        String(kSecAttrAccessible): kSecAttrAccessibleAfterFirstUnlock,
        String(kSecAttrApplicationTag): tagPublic.data(using: .utf8)!
    ]

    let privateKeyParameters: [String: Any] = [
        String(kSecAttrIsPermanent): true,
        String(kSecAttrAccessible): kSecAttrAccessibleAfterFirstUnlock,
        String(kSecAttrKeySizeInBits): 2048,
        String(kSecAttrApplicationTag): tagPrivate.data(using: .utf8)!
    ]

    let parameters: [String: Any] = [
        String(kSecAttrKeyType): algorithm.secKeyAttrType,
        String(kSecAttrKeySizeInBits): keySize,
        String(kSecReturnRef): true,
        String(kSecPublicKeyAttrs): publicKeyParameters,
        String(kSecPrivateKeyAttrs): privateKeyParameters
    ]

    var error: Unmanaged<CFError>?
    let privateKey = SecKeyCreateRandomKey(parameters as CFDictionary, &error)

标签: swiftmacosrsa

解决方案


推荐阅读