(Swift 3,4,5...) RSA Encrypt and OpenSSL Decrypt
Demonstrates how to use Chilkat to RSA encrypt, and then use OpenSSL to decrypt.
func chilkatTest() {
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let rsa = CkoRsa()!
var success: Bool = rsa.generateKey(2048)
var privKey: CkoPrivateKey? = rsa.exportPrivateKeyObj()
success = privKey!.savePkcs8PemFile("qa_output/privKey.pem")
privKey = nil
rsa.encodingMode = "base64"
var plainText: String? = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890"
var bUsePrivateKey: Bool = false
var encryptedStr: String? = rsa.encryptStringENC(plainText, bUsePrivateKey: bUsePrivateKey)
let bd = CkoBinData()!
bd.appendEncoded(encryptedStr, encoding: "base64")
success = bd.writeFile("qa_output/enc.dat")
// The OpenSSL command to decrypt is:
// openssl pkeyutl -in enc.dat -inkey privKey.pem -keyform PEM -pkeyopt rsa_padding_mode:pkcs1 -decrypt
print("OK")
}
|