JSFiddle - React, Tailwind, and code Playground

by Hifni Nazeer

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jsencrypt/2.3.1/jsencrypt.min.js"></script>
<button>
Encrypt
</button>
<textarea id="txtPublicKey" >
	
</textarea>

JavaScript

$(document).ready(function() {
  $('button').click(function() {
    //
    var crypt = new JSEncrypt();
		var publicKeyVal = $('textarea').val();
		alert(publicKeyVal);
		
    crypt.setKey(publicKeyVal); //You can use also setPrivateKey and setPublicKey, they are both alias to setKey

    //Eventhough the methods are called setPublicKey and setPrivateKey, remember
    //that they are only alias to setKey, so you can pass them both a private or
    //a public openssl key, just remember that setting a public key allows you to only encrypt.

    var text = 'test world';
    // Encrypt the data with the public key.
    var enc = crypt.encrypt(text);
		console.log(enc);
    // Now decrypt the crypted text with the private key.
    var dec = crypt.decrypt(enc);
		console.log(dec);

    // Now a simple check to see if the round-trip worked.
    if (dec === text) {
      alert('It works!!!');
    } else {
      alert('Something went wrong....');
    }
    //
  });

});