Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp Asp.net C# Create MD5 Hash

Asp.net C# Create MD5 Hash

As you know, MD5 is a very common hash algorithm which is usually used to encrypt the user’s password stored in database or verify data to ensure it’s not be changed in a complex transaction with multiple steps like online payment while transmit among parties.

In PHP, there is a built in function md5(string $str) which easily calculates the md5 hash of a string. However, with .Net Framework Web or Windows application, you have to write something to encrypt a string with MD5 hash algorithm.

Below is the MD5 function written in C#:

public string CreateMD5Hash(string RawData)
    {
        byte[] hash = System.Security.Cryptography.MD5.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(RawData));
        string str = "";
        byte[] numArray = hash;
        int index = 0;
        while (index < numArray.Length)
        {
            byte num = numArray[index];
            str = str + num.ToString("x2");
            checked { ++index; }
        }
        return str;
    }

public string CreateMD5Hash(string RawData) { byte[] hash = System.Security.Cryptography.MD5.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(RawData)); string str = ""; byte[] numArray = hash; int index = 0; while (index < numArray.Length) { byte num = numArray[index]; str = str + num.ToString("x2"); checked { ++index; } } return str; }

Example and full source code

 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class aspnet_csharp_md5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(CreateMD5Hash("123456"));
        //Response: e10adc3949ba59abbe56e057f20f883e
    }
 
    public string CreateMD5Hash(string RawData)
    {
        byte[] hash = System.Security.Cryptography.MD5.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(RawData));
        string str = "";
        byte[] numArray = hash;
        int index = 0;
        while (index < numArray.Length)
        {
            byte num = numArray[index];
            str = str + num.ToString("x2");
            checked { ++index; }
        }
        return str;
    }
}

using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class aspnet_csharp_md5 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.Write(CreateMD5Hash("123456")); //Response: e10adc3949ba59abbe56e057f20f883e } public string CreateMD5Hash(string RawData) { byte[] hash = System.Security.Cryptography.MD5.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(RawData)); string str = ""; byte[] numArray = hash; int index = 0; while (index < numArray.Length) { byte num = numArray[index]; str = str + num.ToString("x2"); checked { ++index; } } return str; } }

You can verify the MD5 result via a Online MP5 Generator tool.

Jul 12, 2011Hoan Huynh

Source Code

Get Image Width Height With JQuery And JavaScriptDetect Mobile Devices (iPhone, Android) With JavaScript, PHP And .Net (CSharp)
You Might Also Like:
  • Mysql Create FullText Index and Load Index Into Cache
  • MSSQL Split String Function
  • C# ASP.Net Validate Date
  • String To Lower Case In PHP, JavaScript And .Net (CSharp)
  • String To Upper Case In PHP, JavaScript And .Net (CSharp)
  • Auto Rotate Web Page Title With JavaScript
  • C# Read File Content
  • ASP.Net C# Download Or Save Image File From URL
  • CSharp Validate Internet URL Function With Regular Expression
  • CSharp Validate Email Address Function With Regular Expression
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

11 years ago CSharphash algorithm, md5498
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,460 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,839 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,656 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,998 views
C# Read Json From URL And Parse/Deserialize Json
11,723 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development