Reputation: 1806
I have my public key included in the project as byte array. And I wanted to encrypt password using it.When I tried to create public key from the byte array I am getting the below given error.
SecKeyCreateWithData failed :Optional(Swift.Unmanaged<__ObjC.CFError>(_value: Error Domain=NSOSStatusErrorDomain Code=-50
"RSA public key creation from data failed" UserInfo={NSDescription=RSA public key creation from data failed}))
Below given is the code.
let attributes : [String : Any] = [
kSecAttrKeyType as String : kSecAttrKeyTypeRSA,
kSecAttrKeyClass as String : kSecAttrKeyClassPublic,
kSecAttrKeySizeInBits as String : 1024 as AnyObject]
var priKeyCreateError: Unmanaged<CFError>?
_ = SecKeyCreateWithData(PublicKeyDataDevQA as Data as CFData, attributes as CFDictionary, &priKeyCreateError)
if(priKeyCreateError != nil)
{
print("\n SecKeyCreateWithData failed :" + String(describing: priKeyCreateError));
}
I want to implement this without using any third party library like Swifty RSA.
Public Key used is available here
Upvotes: 1
Views: 2715
Reputation: 93948
Your key consists of ASCII encoded PEM. To be able to read the key it is likely that you need to decode the PEM (basically base 64 decoding the part in the middle).
Upvotes: 2