简体   繁体   English

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

I'm not able to create SecKey object from below private key, I did try many answers available here but nothing is helping. 我无法从私钥下面创建SecKey对象,我确实尝试了许多可用的答案,但没有任何帮助。

My swift piece of code is as below: 我的快速代码如下:

    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
    }

The expected result is secKey should have valid value and above guard should not fail. 预期结果是secKey应该具有有效值,并且上述guard措施不应失败。

Note: Public key conversion to the respective secKey is working perfectly fine (the problem is with the only private key while decryption). 注意:将公钥转换为相应的secKey可以正常工作(问题在于解密时只有唯一的私钥)。 I have tried removing \\r\\n from the above key. 我尝试从上述键中删除\\r\\n

After searching a lot, found this Apple thread helpful. 经过大量搜索后,发现此Apple线程很有帮助。 I'm able to manage this ASN.1 parsing using SwiftyRSA library. 我可以使用SwiftyRSA库管理此ASN.1解析。

    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 ). 深入研究之后,我看到,有必要keyData的标题( ASN.1 Parsing )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM