RSA的"SecKey"对象创建失败,错误域代码=

编程入门 行业动态 更新时间:2024-10-22 23:41:10
本文介绍了RSA的"SecKey"对象创建失败,错误域代码= -50“从数据创建RSA私钥失败swift-iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法从私钥下方创建 SecKey 对象,我确实尝试了许多可用的答案,但无济于事.

I'm not able to create SecKey object from below private key, I did try many answers available here but nothing is helping.

我的快速代码如下:

var error: Unmanaged<CFError>? guard let keyData = Data(base64Encoded: key) else { return nil } var keyAttributes: CFDictionary { return [kSecAttrKeyType: kSecAttrKeyTypeRSA, kSecAttrKeyClass: kSecAttrKeyClassPrivate, kSecAttrKeySizeInBits: keySize] as CFDictionary } guard let secKey = SecKeyCreateWithData(keyData as CFData, keyAttributes, &error) else { print(error.debugDescription) //Error Domain Code=-50 "RSA private key creation from data failed swift-iOS return nil }

预期结果是 secKey 应该具有有效值,并且 guard 以上应该不会失败.

The expected result is secKey should have valid value and above guard should not fail.

注意:将公钥转换为相应的 secKey 的效果非常好(问题在于解密时只有唯一的私钥).我尝试从上述键中删除 \ r \ n .

Note: Public key conversion to the respective secKey is working perfectly fine (the problem is with the only private key while decryption). I have tried removing \r\n from the above key.

推荐答案

经过大量搜索后,发现此 Apple线程很有帮助.我可以使用 SwiftyRSA 库来管理ASN.1解析.

After searching a lot, found this Apple thread helpful. I'm able to manage this ASN.1 parsing using SwiftyRSA library.

let privateKeySwifty = try PrivateKey(pemEncoded: privateKey) let secPrivateKey = try PrivateKey(reference: privateKeySwifty.reference)

深入研究之后,我看到,有必要剥离 keyData 的标题( ASN.1解析).

After digging deeper, I can see, there was a need to stripe the header of keyData (ASN.1 Parsing).

更多推荐

RSA的"SecKey"对象创建失败,错误域代码=

本文发布于:2023-11-10 00:51:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573915.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   错误   代码   RSA   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!