Saturday, September 6, 2014

MD5 encryptions in ASP.NET using C#

In the world of network encryption is one of the vital concept to secure your users' data from out side hackers. Generally when user input any password related field  its getting encrypted and then put that encrypted password into database. And in the case of retrieving there are two process are there. you can encrypt the imputing password and match it with the field in database or you can decrypt the stored password and match it with the input. I thing first one is much better and less time consuming.

So, how to encrypt your inputted password field. The most common  encryption method is MD5. Here in this example I will show you how to encrypt a string into an encrypted password using MD5 in ASP.NET using C#.

Create a new project and add a new WebForm to start your encrypted application. In the ASPX page add a new TextBox. Create a TextChange event and on the AutoPostBack property of the TextBox.

In the ASPX page design will be like this.

    
Encrypted password :

In the C# page write the following code.

using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;
using System;

namespace md5
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();

            Byte[] ePass;

            UTF8Encoding encoder = new UTF8Encoding();

            ePass = md5Hasher.ComputeHash(encoder.GetBytes(TextBox1.Text));

            Label1.Text = Convert.ToBase64String(ePass);
        }
    }
}

In the Label1 you can see the encrypted password. Just put the ePass into the database to store as an encrypted password. Your encryption is over. Use it in your other projects and enjoy.

You can download the full source code here.

0 comments:

Post a Comment

Popular Posts

Pageviews