This JavaScript code snippet helps you to encrypt and decrypt text/string in JavaScript with a key. It gets the message and secret key from the user input fields. Then, it iterates over the message and shifts each letter by the secret key. The new message is then displayed in the output field
You can use this code in web applications requiring basic message encryption and decryption. It’s ideal for secure messaging features, ensuring data confidentiality and a user-friendly experience.
How to Encrypt and Decrypt in JavaScript with a Key
1. In your HTML file, include the following code structure. The container holds the cipher title, input fields, and buttons for encryption and decryption:
<div class="container"> <h1>Ceaser Cipher<span> <!-- <img src="icon.png" alt="secretkey" class="logo"> --> </span></h1> <p class="info"> The Caesar cipher is a type of substitution cipher in which each letter in the plaintext is shifted to a certain number of places down the alphabet.The method is named after Julius Caesar, who allegedly used it to communicate with his generals. </p> <div class="input-sect"> <input type="text" class="js-input" placeholder="Enter Your Message to decrypt" autofocus> <div> <label class="shift-key">Secret key:</label> <input type="number" class="js-numb"> </div> <div class="btn-sect"> <button class="btn encrypt-btn">Encrypt Me</button> <button class="btn decrypt-btn">Decrypt Me</button> </div> </div> </div> <div class="new-msg"></div>
2. Use the following CSS code for a clean and visually appealing design. Customize the look to fit your application’s design while ensuring a responsive layout.
*{ margin: 0; padding: 0; } body{ font-family: 'Courier New', Courier, monospace; background-color: #d1d0d0; } .container{ width: 90%; margin: 15px auto 0; text-align: center; } /* .logo{ width: 50px; } */ h1{ font-size: 2.5em; display: flex; justify-content: center; } .info{ width: 90%; margin: 0 auto; line-height: 20px; } .input-sect{ width: 70%; margin: 30px auto 0; display: flex; flex-direction: column; justify-content: center; } input{ height: 39px; outline: none; border: 1px solid; border-radius: 8px; margin-bottom: 21px; padding: 1px 7px; font-size: 1em; font-weight: bold; background-color: transparent; } .shift-key{ font-size: 1em; font-weight: bold; } .js-numb{ width: 40px; height: 30px; } .btn{ padding: 10px 15px; font-weight: 600; font-size: 1em; background-color: #29a297; color: #ece9e9; letter-spacing: 1px; outline: none; border: none; border-radius: 20px; margin-bottom: 10px; cursor: pointer; } .btn:hover{ box-shadow: inset -1px 2px 5px 0px #737373;; } /*Decrypt/Encrypt message will show here*/ .new-msg{ width: 60%; margin: 0 auto; padding: 18px 0; border: 2px solid black; text-align: center; margin-top: 5px; } .new-msg h2{ margin-bottom:5px; } .new-msg p{ font-size: 1.5em; color: blue; font-weight: bold; } .new-msg:empty{ display: none; }
3. Finally, add the following JavaScript code to your project to enable encryption and decryption functionality. It handles the logic behind the Caesar Cipher, both for encryption and decryption.
/* The Caesar cipher is a type of substitution cipher in which each letter in the plaintext is shifted to a certain number of places down the alphabet. For example, with a shift of 1, A would be replaced by B, B would become C, and so on. The method is named after Julius Caesar, who allegedly used it to communicate with his generals. For example: Input: "defend the east wall of the castle" Output: "efgfoe uif fbtu xbmm pg uif dbtumf" */ const encryptbtn = document.querySelector('.encrypt-btn'); const decryptbtn = document.querySelector('.decrypt-btn'); encryptbtn.addEventListener('click', ()=> { const msg = document.querySelector('.js-input').value; const key = parseInt(document.querySelector('.js-numb').value); if (msg !== "" & key !== ""){ var newmsg = ''; for(let k=0; k < msg.length; k++){ var x = msg.charCodeAt(k) + key; newmsg += String.fromCharCode(x); } document.querySelector('.new-msg').innerHTML = ` <h2>Encrypt message</h2> <p>${newmsg}</p> `; } else{ alert("!Please Enter your message & Secret key!"); } }); decryptbtn.addEventListener('click', ()=> { const msg = document.querySelector('.js-input').value; const key = parseInt(document.querySelector('.js-numb').value); if (msg !== "" & key !== ""){ var newmsg = ''; for(let k=0; k < msg.length; k++){ var x = msg.charCodeAt(k) - key; newmsg += String.fromCharCode(x); } document.querySelector('.new-msg').innerHTML = ` <h2>Decrypt message</h2> <p>${newmsg}</p> `; } else{ alert("!Please Enter your message & Secret key!"); } })
That’s all! hopefully, you have successfully created a tool to encrypt and decrypt text with a key on your website. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.