gpt4 book ai didi

ios - 将私有(private) base64 编码 key 转换为 SecKey

转载 作者:行者123 更新时间:2023-11-28 05:45:28 32 4
gpt4 key购买 nike

我确实像这样使用 OpenSSL 生成私钥:

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 563 -nodes -subj "//C=LT\ST=Vilnius\L=Vilnius\O=Briedis\OU=Org\CN=www.example.com"

key.pem 文件包含 base64 格式的私钥。删除了 BEGIN 和 END 标记。以及新线路。应用这些更改后,私钥如下所示:

let privteKeyString = “MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQCxFZF3YHfkbvNbC5kuXo4QxtHVRBKe35lOUVFPIRPxc87QUJHwb0b62aG84AHRhx6fawX66Sby/U31U0HBXh0QY3IRGqSm3xOZBjrJvAk2vd8T5y4fCYFuEk3exTm7FNVeucf0W3ibPyW/zWTbMek1XMSbii130LUKBhMFg1Jmf/M2YwSRmPO4gZ3uzK+hvE02QtvEI8/ruiO1GfRsqsYNPd67Cayley8DvYXzQKCS6+LJUW1lTQObgY0xHLpmlLmXjLj9pAvXPVp0jqbVml2TcKCeC/7cyaf0jxEfkajlYGORpw/0/zcYk2mde/UzzEzvKsOXAnZo1XTj+BdOMVuaeKdxjtEOoFBGVabSmBl0NRTg2JEZKTHJ6gywiua1jemnKAnwrwsc3DOFIKDpGS9EBiFgbg86c/2/76EcQ4pYtiZgwYABahPpC+JAEpw99PGK9jZcSsFpTO81ZNIwoy/deH/frZ0G1hQCnB85T3DDELC8aJMM9R+NUJcLnq9NHywE15Sm2xzepkY1sWo0XmcSn/e3lBqOLAIIw+bygsM9Xksxhs6FLb0BOWMh/C3urKnG/jdPLp0ROKD6t9/5iVgCPDftHEEVxPrKcVqOSBoFPRhKvl8SP6OdyS4ken8xzKNCN7bs1ZvhpvhtpFysgAOHZF9y9GcafltZEFVECfBd0wIDAQABAoICAQCJle9ip/Ie6tFrMLTAYTjR8hfAMlahV6r+PZIF//ZFyCuskJ0DCQJGDmdq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+IRDy4U0KjgJq04sTnDqhjLrV++f1vTtTUOWSIawIVxequ/QKO24lyRtEqYQZtCFs+AEeZNFkay9npYY/nNplQV/K/b9E4WYUtNWHukjwll8ftwsiDJqj2JV5GYmfLbUgJbuxJ6eGNXcgrW3Vs8ye9mVHiL8QSpYpQS3ys0Kiv3zjvkobCO4bQVfIQGxIj5mnWL/is=“

从base64到SecKey的转换:

let privateKeyData = Data(privteKeyString.utf8)
var error: Unmanaged<CFError>?
let secKey = SecKeyCreateWithData(privateKeyData as! NSData, [
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeySizeInBits: 4096,
kSecAttrKeyClass: kSecAttrKeyClassPrivate,
] as NSDictionary, &error)

执行后出现 -50 错误。

私钥base64转SecKey的方法是什么?

最佳答案

通常所有 RSAPrivateKey 都不是普通的 RSAPrivateKey,而是 PrivateKeyInfo(请参阅 RFC 5208 的第 5 节)。 IOS 不支持 PrivateKeyInfo key 结构。在这种情况下,需要转换为 RSAPrivateKey。从 PrivateKeyInfo 创建 iOS SecKey 的步骤:

  1. 使用 OpenSSL 将私钥.pem 转换为 DER 格式。

    openssl rsa -in key.pem -outform der -out key.der
  2. 将 key.der 文件拖放到 Xcode 项目中。

  3. 加载并打印私钥作为 vanilla RSAPrivateKey:

    let keyUrl = Bundle.main.url(forResource: "key", withExtension: "der")
    let keyDerData = try? Data(contentsOf: keyUrl!)
    print(keyDerData?.base64EncodedString())
  4. 将原始 RSAPrivateKey 原始数据转换为 SecKey。

       var error: Unmanaged<CFError>?
    let secKey = SecKeyCreateWithData(keyDerData as! NSData, [
    kSecAttrKeyType: kSecAttrKeyTypeRSA,
    kSecAttrKeySizeInBits: 4096,
    kSecAttrKeyClass: kSecAttrKeyClassPrivate,
    ] as NSDictionary, &error)

关于ios - 将私有(private) base64 编码 key 转换为 SecKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54919088/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com