Sample code for 30+ languages & platforms
JavaScript

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.
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;

var cert = new CkCert();

success = cert.LoadFromFile("qa_data/pem/mf_public_rsa.pem");
if (success == false) {
    console.log(cert.LastErrorText);
    return;
}

var pubKey = new CkPublicKey();
cert.GetPublicKey(pubKey);

var rsa = new CkRsa();
rsa.UsePublicKey(pubKey);

rsa.EncodingMode = "base64";
var encryptedStr = rsa.EncryptStringENC("hello",false);
console.log("encrypted string = " + encryptedStr);