Pages

Friday, August 16, 2013

How to Convert String into MD5 Hash in Asp.net

This article will be explain how to convert a string to an MD5 Hash form. To convert string in MD5 hash form in Asp.net you have to use two namespace  'System.Text'  and  'System.Security.Cryptography'. Here is the complete code for convert simple sting in MD5 form.


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

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string val = ConvertToMD5HASH(TextBox1.Text);
        Response.Write(val);
    }

    public string ConvertToMD5HASH(string text)
    {
        MD5 Secur = System.Security.Cryptography.MD5.Create();
        byte[] ByteText = System.Text.Encoding.ASCII.GetBytes(text);
        byte[] MdHash = Secur.ComputeHash(ByteText);

        StringBuilder Str = new StringBuilder();
        for (int i = 0; i < MdHash.Length; i++)
        {
            Str.Append(MdHash[i].ToString("X2"));
        }
        return Str.ToString();
    }


}

No comments:

Post a Comment