Encrypt data in browser with Javascript and Decrypt on server with asp.net c#

Introduction:
In now days sending data over the internet is unsecured. As a web developer you have to ensure that your web application is safe and secure and there are no chances of hacking when data is on the way to server. 

Scenario:
Imagine if your website has a login page and your end user enter his user id and password in provided text box's. after that he press the submit button. now these user id and password sent to the server using internet. before sent data reached to server, this data is passed from many routers and servers. there are chances that any person sitting on servers can track user id and password (this data will be in clear text format).

Solution:
 To avoid data stealing over the network there are two options.


  1. Use HTTP instead of HTTP. for this option you have to purchase SSL for your website and this is paid option.
  2. Other option is embed a small encryption and decryption code to secure user important data which will travel over the internet.


Below is the java script  Encryption.

JavaScript Code for Encryption:
Below code will be called before page submitted to server and every textbox value will be passed from this function and Encrypted text will be placed in same text box.(This will overwrite the clear text in textbox control)

function Encrypt(PlainText, Key) 
{
            var to_enc = PlainText.toString().replace(/^\n+/, "").replace(/\n+$/, "");
            var xor_key = Key;
            var the_res = "";
            for (i = 0; i < to_enc.length; ++i) {

                if (to_enc.charCodeAt(i) <= 32) {
                    the_res += String.fromCharCode((to_enc.charCodeAt(i)));    
                }
                else {

                    the_res += String.fromCharCode((to_enc.charCodeAt(i)) - Key);     
                }
            }
            return (the_res);
}

Asp.net C# Code for Decryption:

Add the following reference in header.

using System.Text;

Place the following function on Asp.net c# page.

public static string Decrypt(string textToEncrypt, int key)
{
        StringBuilder inSb = new StringBuilder(textToEncrypt);
        StringBuilder outSb = new StringBuilder(textToEncrypt.Length);
        char c;
        for (int i = 0; i < textToEncrypt.Length; i++)
        {
            c = inSb[i];

            if (c <= 32)
            {
            }
            else
            {
                c = (char)((c + key));            
            }
            outSb.Append(c);
        }
        return outSb.ToString();

}

On page load you have to call above function and pass all text box's values from above function to get original text. this text will be replaced with encrypted text in same control. after that you can use the control values in your application.

Conclusion:
 Using above methods you can make safe web application important data collected from client and sent to server.Many website specially banking application's using this techniques along with SSL for extra security layer protection.

Sample Project:
 Click on "Encryption.rar " in Download file option.

Click here to Dowload Sample Project...

Key points for use:

  • Provide same key in both function for Encryption and Decryption.



Follow us on below channels to stay updated for new post and tutorials:
Blog: http://easytutorials4all.blogspot.com
Google+ : https://plus.google.com/u/0/112994256924713874536
Facebook Page : https://facebook.com/EasyTutorials4All
Youtube: https://www.youtube.com/channel/UCod8G-TeTUTz9DjaqK1C5-g

Comments

  1. Follow us on Facebook and you tube for more tutorials.

    ReplyDelete

Post a Comment

Popular posts from this blog