javascript encrypt decrypt program

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.

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.

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.

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.

16 COMMENTS

  1. 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

  2. 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 ;
    }
    };

  3. 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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here