我无法从私钥下方创建 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"对象创建失败,错误域代码=
发布评论