Hello.
Here is my Swift code to generate RSA public / private key
static func createPrivateKey()-> (SecKey?, SecKey?){
let attributes: [CFString: Any] = [
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeySizeInBits: 2048,
kSecPrivateKeyAttrs: [
kSecAttrIsPermanent: true,
kSecAttrApplicationTag: "tagData"
]
]
var error: Unmanaged<CFError>?
guard let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error),
let publicKey = SecKeyCopyPublicKey(privateKey) else
{
NSLog("\tError generating keypair. %@", "\(error!.takeRetainedValue().localizedDescription)")
return (nil, nil)
}
return (privateKey, publicKey)
}
static func printRSAPublicKey(_ publicKey: SecKey) {
guard let publicKeyData = SecKeyCopyExternalRepresentation(publicKey, nil) as Data? else {
print("Failed to get public key data.")
return
}
print("RSA Public Key:")
print(publicKeyData.base64EncodedString())
}
I got public key but it does not work
I got stuck there.