Reputation: 4561
I'm not able to create SecKey
object from below private key, I did try many answers available here but nothing is helping.
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.
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.
Upvotes: 2
Views: 3855
Reputation: 4561
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).
Upvotes: 2