RSA的"SecKey"对象创建失败,错误域代码= -50“从数据创建RSA私钥失败swift-iOS [英] `SecKey` object creation for RSA failing, Error Domain Code=-50 "RSA private key creation from data failed 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.
推荐答案
经过大量搜索后,发现此 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)
After digging deeper, I can see, there was a need to stripe the header of keyData
(ASN.1 Parsing).
这篇关于RSA的"SecKey"对象创建失败,错误域代码= -50“从数据创建RSA私钥失败swift-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!