MD5 Hash Code Generator In C# – ASP.Net

In this post I’m gonna show you how you can generate MD5 encryption code using C#.

A brief description about MD5

The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.

MD5 was designed by Ron Rivest in 1991 to replace an earlier hash function, MD4.[3] The source code in RFC 1321 contains a “by attribution” RSA license.

Now lets see the code.

 

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <h1>Hey! By using this you'll be able to create MD5 hash code for any string</h1>
<table>
 <tr>
 <th>Enter String</th>
 <td>
 <asp:TextBox ID="text_string" runat="server"></asp:TextBox>
 <asp:RequiredFieldValidator ID="validate_text_string" runat="server"
 ControlToValidate="text_string" Display="Dynamic" ForeColor="Red" ErrorMessage="Please Enter The String"></asp:RequiredFieldValidator>
 </td>
 </tr>
 <tr>
 <td>
 <asp:Button ID="btn_create_md5" runat="server" Text="Generate MD5 Hash Code" OnClick="btn_create_md5_Click" />
 </td>
 </tr>
 <tr>
 <th>
 <asp:Label ID="lbl_md5_code" runat="server"></asp:Label>
 </th>
 </tr>
 </table>
 </div>
 </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;

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

    }
    protected void btn_create_md5_Click(object sender, EventArgs e)
    {
        //Local variable var, store value from user
        string var = text_string.Text;

        //Creating new instance md5 for MD5 class.
        MD5 md5 = new MD5CryptoServiceProvider();

        //Convert the input string into byte array and compute the hash
        byte[] result = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(var));

        //Create a new string builder to collect the bytes and create a string
        StringBuilder stringbuilder = new StringBuilder();

        //Loop through each byte of hash data
        //and format each one as a hexadecimal string
        for (int i = 0; i < result.Length; i++)
        {
            stringbuilder.Append(result[i].ToString("x2"));
        }

        //storing the hash code string in the local variable md5_code.
        string md5_code = stringbuilder.ToString();
    }
}

The above code is all who plays the game.

Please make sure to include two important namespaces, else your code won’t work at all.

using System.Security.Cryptography;
using System.Text;

These two namespaces contains all the necessary classes and objects, required to generate MD5 code.

This was all about MD5 Hash Code Generator In C#, hope you liked the post.


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Recent Articles

Powered by Paras Babbar · Designed by Paras Babbar