How to Encrypt Password using MD5 in Asp.Net C# VB.Net application

Encryption for sensitive data like password is essential in everyday development. MD5 hashing algorithm is one of the most commonly used algorithm in arena and is one of the best. There are two general classes of encryption: one-way encryption and two-way encryption. Using two-way encryption you can encrypt a text as well as you can decrypt it. But for one-way encryption the difference is you can’t decrypt it. MD5 encryption is an example of a one-way encryption algorithm.

This is the common task for every Registration page where user put their name & their password. You can encrypt the both or only password & save it into the database. So that no one can read the encrypted password which will increase the application security policy. Here i will show you how you can encrypt password.

Write a Function/Method to Encrypt Password Using MD5:
C# Code:

    private string md5(string sPassword)
        System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] bs = System.Text.Encoding.UTF8.GetBytes(sPassword);
        bs = x.ComputeHash(bs);
        System.Text.StringBuilder s = new System.Text.StringBuilder();
        foreach (byte b in bs)
        return s.ToString();

VB.Net Code:

    Private Function md5(sPassword As String) As String
        Dim x As New System.Security.Cryptography.MD5CryptoServiceProvider()
        Dim bs As Byte() = System.Text.Encoding.UTF8.GetBytes(sPassword)
        bs = x.ComputeHash(bs)
        Dim s As New System.Text.StringBuilder()
        For Each b As Byte In bs
        Return s.ToString()
    End Function

To test the example add below HTML Markup:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Label ID="Label1" runat="server" Text="Label">Enter Password</asp:Label>
    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><br />
    <asp:Label ID="Label2" runat="server" Text="" Font-Bold="true"></asp:Label><br />
    <asp:Button ID="Button1" runat="server" Text="Convert to MD5" 
        onclick="Button1_Click" />

Now under Button1 Click Event write below code:
C# Code:

Label2.Text = md5(txtPassword.Text);

VB.Net Code:

Label2.Text = md5(txtPassword.Text)

How to Encrypt Passwords using MD5 in ASP.NET Csharp VB.Net

Now you got an encrypted data of password. Insert it into your database table. So user creation is completed. Now how we can authenticate the valid user? Its easy, again send the user input to the md5 method which will return you an encrypted string — now compare with your database. If matched then the user verified.

Good to Know:

For Cryptographic details:

Download Code Example C#        Download Code Example VB.Net

6 thoughts on “How to Encrypt Password using MD5 in Asp.Net C# VB.Net application

  • December 24, 2015 at 1:10 pm

    I am trying to encrypt in java and decrypt in c# with query string for login page..Any one send coding for this or reference for this solution… Tanks in advance 🙂

  • February 8, 2016 at 5:40 pm

    to encrypt using C# you can use this code i found on using System.Security.Cryptography.MD5; namespace MD5Sample { class Program { static void Main(string[] args) { string pass = “Your_password”; MD5 md5Hash = MD5.Create(); // Calculate MD5 hash from input byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(pass)); StringBuilder sBuilder = new StringBuilder(); // Convert byte array to hex string for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } string hash = sBuilder.ToString(); Console.WriteLine(hash); // 3df11a12265e10434edcbea4de121dec } } }

    • April 15, 2016 at 6:22 am

      but how to Decrypt it in original value ??????????

      • June 28, 2016 at 7:37 am

        HI, Thanks for comment, Its one way encryption so you could not decrypt.

  • April 15, 2016 at 6:21 am

    but how to Decrypt it in original value ??????????

  • June 4, 2016 at 9:03 am

    wow!!! your extreme good and help me a lot and save my time. much appreciated and developer we suppose to be like you simple code, by Kadama, Tanzania


Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code