Sample code for 30+ languages & platforms
JavaScript

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.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

var pubkey = new CkPublicKey();

success = pubkey.LoadBase64("MIICdgIBADA ... A9PXLk+j5A==");
if (success == false) {
    console.log(pubkey.LastErrorText);
    return;
}

var rsa = new CkRsa();

success = rsa.UsePublicKey(pubkey);
if (success == false) {
    console.log(rsa.LastErrorText);
    return;
}

rsa.EncodingMode = "base64";

var encryptedStr = rsa.EncryptStringENC("12345678",false);

console.log(encryptedStr);