Get started with jsencrypt CDN

MIT licensed

JsEncrypt is a library for performing RSA encryption and decryption in the browser.

Tags:
  • OpenSSL
  • RSA
  • Javascript encryption

Stable version

Copied!

How to start using jsencrypt CDN


// Include the library from CDN
const JSEncrypt = (() => {
  const crypto = document.createElement('script');
  crypto.src = 'https://cdn.cdnhub.io/jsencrypt/3.3.2/jsencrypt.min.js';
  document.head.appendChild(crypto);

  return new Promise((resolve) => {
    crypto.onload = () => {
      document.head.removeChild(crypto);
      resolve(jsEncrypt);
    };
  });
})();

// Initialize JSEncrypt instance
const publicKey = new JSEncrypt();
publicKey.keyLength(2048);

// Generate a public key
const publicKeyString = publicKey.getPublicKey();
document.body.innerHTML += `<p>Public key: ${publicKeyString}</p>`;

// Generate a private key and associate it with the public key
publicKey.initKeyPair();
const privateKey = publicKey.getPrivateKey();

// Encrypt a message using the public key
const message = 'Hello, World!';
const encrypted = publicKey.encrypt(message);

// Decrypt the message using the private key
const decrypted = privateKey.decrypt(encrypted);

// Display the decrypted message
document.body.innerHTML += `<p>Decrypted message: ${decrypted}</p>`;
Copied!
Copied!

All versions