Sample code for 30+ languages & platforms
Swift

Use Base64 RSA Key to Encrypt

See more RSA Examples

Loads a Base64 RSA key and uses it to encrypt a string, returning the result in base64.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let pubkey = CkoPublicKey()!

    success = pubkey.loadBase64(keyStr: "MIICdgIBADA ... A9PXLk+j5A==")
    if success == false {
        print("\(pubkey.lastErrorText!)")
        return
    }

    let rsa = CkoRsa()!

    success = rsa.usePublicKey(pubKey: pubkey)
    if success == false {
        print("\(rsa.lastErrorText!)")
        return
    }

    rsa.encodingMode = "base64"

    var encryptedStr: String? = rsa.encryptStringENC(str: "12345678", bUsePrivateKey: false)

    print("\(encryptedStr!)")

}