Sample code for 30+ languages & platforms
Swift

RSA Import Public Key from Certificate PEM

See more RSA Examples

Uses a certificate's public key for RSA encryption. The public key from the certificate .pem file is used.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let cert = CkoCert()!

    success = cert.load(fromFile: "qa_data/pem/mf_public_rsa.pem")
    if success == false {
        print("\(cert.lastErrorText!)")
        return
    }

    let pubKey = CkoPublicKey()!
    cert.getPublicKey(pubKey: pubKey)

    let rsa = CkoRsa()!
    rsa.usePublicKey(pubKey: pubKey)

    rsa.encodingMode = "base64"
    var encryptedStr: String? = rsa.encryptStringENC(str: "hello", bUsePrivateKey: false)
    print("encrypted string = \(encryptedStr!)")

}