简体   繁体   中英

RSA public key encryption in Swift

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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