Encryption-Decryption Using RSA in Java Script
Here, we will use the package called hybrid-crypto-js to achieve our goals. Hybrid Crypto JS is a hybrid (RSA+AES) encryption and decryption toolkit for JavaScript. Hybrid Crypto JS combines RSA and AES encryption algorithms, making it possible to encrypt and decrypt large messages efficiently. This cross-platform library is based on Forge. Hybrid Crypto JS can be used in browsers, Node.js, or React Native.
Here, we are only using this package for the browser, more information you can find by hybrid-crypto-js.
first of all, we have to download the hybrid-crypto.min.js file here.
<script type="text/javascript" src="hybrid-crypto.min.js"></script>
Initialization :
As shown below we have to first initialize the hybrid-crypto-js.
// Basic initialization
var crypt = new Crypt();
var rsa = new RSA();
Generate pair keys:
As shown below we can generate pair of private keys and public keys.
<script>var publicKey;var privateKey;var encrypted;var decrypted;generateKeys();function generateKeys() { var rsa = new RSA(); rsa.generateKeyPair(function(keyPair) { publicKey = keyPair.publicKey; privateKey = keyPair.privateKey; }); setTimeout(function () { console.log('publicKey', publicKey); console.log('privateKey', privateKey); }, 3000);}
After generating the keys we can encrypt the message as below.
Encryption
here, we can encrypt our message with the help of the public key that we had generated.
function Encryption() { var entropy = 'Testing of RSA algorithm in javascript.'; var crypt = new Crypt({ rsaStandard: 'RSA-OAEP', entropy: entropy }); var rsa = new RSA({ entropy: entropy }); var message = 'Hello, this is the demo of encryption/decryption in javascript!'; encrypted = crypt.encrypt(publicKey, message); console.log('encrypted', encrypted);
}setTimeout(() => { Encryption();},3000);
This will give output as below.
encrypted {"v":"hybrid-crypto-js_0.2.4","iv":"yjM7NP5g+GHA6xnB6MyhsfqIcz4dbMKWvkX49MOurJ4=","keys":{"93:7f:26:33:72:25:a4:e5:40:ab:8a:55:24:b5:77:9d:df:34:28:d9":"JHYCn+wZqsRvyYjkyogxv2iXJ2Fil/vKUoACmbZTdlT2BsXtDS12fNhFqcXgmLbmZWguZJSwT70jIua8eSxn8YK3wBUBkfe8UGnBCJUHvWk8RYNoBGiVe0xg3GNzqVGMVLwLLyuu9bc+Eq7rB3H8PVojXFIp2FW7r4z/NJE5XaPSsF5sNNbevuB9igIjW9nstvCRGpz9i3ToOugxGfMY416SxIiTqPI5GGkm7XNizkHPN/9njJTAXWZeJoF7oOStnOdVQrMoKAHOAXYV9UXZZbvaOQ2e4+HS5QHNZ94AqLahzJe0hZ+Aq+PQtLISbUwiNMZKl90k2xynLVTXsI9RcHafsLfHUkGqpWz4xitkydaMhwouDl8DRIUPsKiZDFVRT+fl2lKeBm5GAm8LRfwTCKue5gMNxkKDpcqc2Dfwd03lG81es+xqa9UHH+VZrd3dQXR1yomEVTU6D2xh9auZgjkv1UXMu60mZdjcEMVdenZc3FIIk2ajlsbmFhlA57AhfT0Q0Q9FSLMdFEnfFGta5oxIF97Ukxo4MGcVpu+mm/5XbsxnA7zS2QBue+431HgKeH/bB/+v0oQaZZiL/Dy0oAPQ+a+lxiApS4vg+oIY0WgV4QcN7/Y1Ayo1Y/ZzbQr5MKj/QhqbDI28n6ktw3gd8uobTWnnRLTb7VrRt6GOt0A="},"cipher":"MlwKY5JhLwmoOvLBCjiqE74PWpR4bnUoVLWI7bFOvrdSGSzbOY1GHWuA7efCJoL380lvrs1fOVS71Z5Dx+GEbA=="}
Decryption
here, we can decrypt the message with the help of the private key that we had generated above.
function Decryption() { var entropy = 'Testing of RSA algorithm in javascript.'; var crypt = new Crypt({ rsaStandard: 'RSA-OAEP', entropy: entropy }); var rsa = new RSA({ entropy: entropy });decrypted = crypt.decrypt(privateKey, encrypted);
console.log('decrypted', decrypted);
}
setTimeout(() => { Decryption();}, 3000);
This will give us output as below.
decrypted {message: "Hello, this is the demo of encryption/decryption in javascript!", signature: undefined}
So, as above we can use RSA for encryption and decryption in javascript.
Thank you.