JavaScript Encrypt & Decrypt, Simple Encryption and Decryption Program in JS. When we sign up or register on a website they store our information in their database like MySQL, MongoDB, etc. But they store data in an encrypted form, not ordinary text form. Because if store our information in a normal text form, there will be chances of hacking. Also, their employees can see our data.
That’s why we use encryption. Large and well-known websites or companies developed their own encryption method. They develop their program using high-level programming languages like Python, Java, Ruby, etc. When we looked in the past, common websites used MD5 hash encryption. But, also a noob can decrypt MD5 hash. There are so many websites for decrypt MD5.
Even also now some small sites use the MD5 hash for store data in the database. In PHP you just have to put md5($_POST['password']);
in SQL insert command for MD5 encryption. But This program is a little bit secure than MD5.
So, Today I am sharing a simple JavaScript encrypt & decrypt program. You can say this a medium level secure hash encryption because you can set a password for encryption. Simple programs encrypt data in a default or single form every time. But this program encodes text every time different by according to given password.
Preview Of This Encryption Program in JavaScript
Let’s take a look of encrypt program what I am sharing today.
Before sharing source code, talk something about this simple javascript encrypt program. I had created this function with some lines of javascript. I used javascript loop for()
in this program. I used the key code of passcode using charCodeAt
and combine in the loop.
Note: You have to put a password for encrypting your text. The same thing will be applied when to want to decrypt. You have to put the same password you had put when you encrypt.
You May Also Like:
JavaScript Encrypt & Decrypt Programs Source Code
Now time to share the source code of this program with all my blogs visitors. You have to create 3 files for this system. First for HTML file, Second for CSS file, Third & Last one for JS file. So, let’s get started.
index.htmlÂ
Create an HTML file named ‘index.html‘ or anything as you want. Copy these code given below and paste on your HTML file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<!DOCTYPE html> <!-- code by webdevtrick ( https://webdevtrick.com ) --> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="style.css" rel="stylesheet"> <script src="encrypt.js"></script> </head> <body> <div class="center top-relative"> <div class="content voilet1"> <span>Enter Password for Encryption</span> <input class="f15pt" type="password" id="passcode" placeholder="password" /> </div> <div class="content voilet2"> <span>Put Or Type Text Here</span> <textarea id="originalContent"></textarea> </div> <div class="content voilet0"> <span>Encrypted Form</span> <textarea id="encryptedContent" readonly></textarea> </div> <div class="content voilet0"> <span>Decrypted Form</span> <textarea id="decryptedContent" readonly></textarea> </div> <button class="encrypt" onclick="webdevencrypt.setEncrypt('originalContent','encryptedContent','passcode')">Encrypt</button> <button class="encrypt" onclick="webdevencrypt.setDecrypt('originalContent','decryptedContent','passcode')">Decrypt</button> </div> </body> </html> |
style.css
Now create a CSS file named style.css for giving a little bit style to this program. Put these codes in your CSS file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
/* code by webdevtrick ( https://webdevtrick.com ) */ body { background-color: #e8eaed; } .f15pt { font-size: 15pt; } .content { background: #610bb1; padding: 10px; font-size: 15pt; color: #fefefe; width: 100%; } .voilet0 { background-color: #333; } .voilet1 { background-color: red; } .voilet2 { background-color: #333; } textarea { width: 100%; min-height: 100px; } .encrypt { font-size: 24pt; background-color: red; border-radius: 3px; border: 1px solid #6d0202; color: #ffffff; padding: 7px; margin: 5px; float: right; } .encrypt:hover { background-color: #333; } .center { width: 70%; margin: auto; } #passcode { border-radius: 3px; padding: 6px; border: 0px; outline: none; } #passcode:focus { border: 2px solid #530c96; } .top-relative { top: 50px; position: relative; } .top-bar { width: 100%; position: fixed; z-index: 5; top: 0; left: 0; background-color: #fefefe; padding: 5px; box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.75); } .ablock { text-decoration: none; color: #7c0abe; font-size: 15pt; } .h35px { height: 35px; } .txtbld { font-weight: bold; } .prb12px { position: relative; bottom: 10px; } .w35px { width: 35px; } |
encrypt.js
Final thing, Create a JavaScript file named ‘encrypt.js’ for giving life to this program. Without JS this program is nothing. Put these codes in your JS file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* code by webdevtrick ( https://webdevtrick.com ) */ var webdevencrypt = { setEncrypt: function(source,destination,passcode) { document.getElementById(destination).innerText = this.encryptCodes(document.getElementById(source).value,document.getElementById(passcode).value); }, setDecrypt: function() { document.getElementById('decryptedContent').innerText = this.decryptCodes(document.getElementById('originalContent').value,document.getElementById('passcode').value); }, encryptCodes: function(content,passcode) { var result = []; var passLen = passcode.length ; for(var i = 0 ; i < content.length ; i++) { var passOffset = i%passLen ; var calAscii = (content.charCodeAt(i)+passcode.charCodeAt(passOffset)); result.push(calAscii); } return JSON.stringify(result) ; }, decryptCodes: function(content,passcode) { var result = [];var str = ''; var codesArr = JSON.parse(content);var passLen = passcode.length ; for(var i = 0 ; i < codesArr.length ; i++) { var passOffset = i%passLen ; var calAscii = (codesArr[i]-passcode.charCodeAt(passOffset)); result.push(calAscii) ; } for(var i = 0 ; i < result.length ; i++) { var ch = String.fromCharCode(result[i]); str += ch ; } return str ; } } |
That’s It. Now you have Successfully created encryption in JS. If you have any doubt or suggestion comment down below.
Thanks For Visiting, Keep Visiting.
what is the password
you can set your own password
Hh
Great Script! Can i use it to encrypt one of the field of my form?if yes,please tell me how.
This encryption algorithm seems weak to frequency analysis. What is this algorithm? It seems to be a Variable-length pseudo-Caesar cipher….
how to set your own pass
put your password on the password field and click on encrypt.
That was simple and good one for starter, Helpful dude, cheers
It is a well-explained simple code!
However, with the security . . .
This is not secure, it is just a vigenere cipher which was considered broken even in 19th century.
I would say md5 much is more secure, even though it is a hash function and not a cipher.
It is ok when you use it for fun, but for serious things you should use a stronger binary-based cipher like aes.
Use it for fun
An improved version, use textarea or input#text form fields for your source, destination and passcode fields,
var webdevencrypt = {
setEncrypt: function(source,destination,passcode) {
document.getElementById(destination).value = this.encryptCodes(document.getElementById(source).value,document.getElementById(passcode).value);
},
setDecrypt: function(source,destination,passcode) {
//document.getElementById(‘decryptedContent’).value = this.decryptCodes(document.getElementById(‘originalContent’).value,document.getElementById(‘passcode’).value);
document.getElementById(destination).innerText = this.decryptCodes(document.getElementById(source).value,document.getElementById(passcode).value);
},
encryptCodes: function(content,passcode) {
var result = []; var passLen = passcode.length ;
for(var i = 0 ; i < content.length ; i++) {
var passOffset = i%passLen ;
var calAscii = (content.charCodeAt(i)+passcode.charCodeAt(passOffset));
result.push(calAscii);
}
return JSON.stringify(result) ;
},
decryptCodes: function(content,passcode) {
var result = [];var str = '';
var codesArr = JSON.parse(content);var passLen = passcode.length ;
for(var i = 0 ; i < codesArr.length ; i++) {
var passOffset = i%passLen ;
var calAscii = (codesArr[i]-passcode.charCodeAt(passOffset));
result.push(calAscii) ;
}
for(var i = 0 ; i < result.length ; i++) {
var ch = String.fromCharCode(result[i]); str += ch ;
}
return str ;
}
};
how do you see the password.
to get show the password in the form, remove the
type=”password”
code from line 14 of the html script.
hi there, how do you display the encrypted form in a javascript alert() box?
thanks!
.bd{
background:rgba(0,0,0,0.1);
z-index:99999999999;
position:fixed;
width:100%;
height:100%;
}
Hi, what is the name of Encryption method?
Thanks for all your help. I used your approach to generate email address dynamically and show the link as an encrypted string. Then when the link is clicked, the it will decrypt the string and click like a mailto link taking you to your email client. Pretty nifty.