Encrypt your passwords with MD5 hashing in C#

In this blog post I will be demonstrating a simple C# method that can be utilized to encrypt a string (password) to MD5 hash



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. (From Wikipedia)




To use the MD5 encryption in your application you should include the <System.Security.Cryptography> Namespace using the code above

 using System.Security.Cryptography;  

The Bloc code below carries a method (GetMD5) that convert a chain to an MD5 hash, returning a 32 characters, white one string parameter
GetMD5 function
  public string GetMD5(string text)  
     {  
       MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();  
       md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));  
       byte[] result = md5.Hash;  
       StringBuilder str = new StringBuilder();  
       for (int i = 0; i < result.Length; i++)  
       {  
         str.Append(result[i].ToString("x2"));  
       }  
       return str.ToString();  
     }  

To use the GetMD5 method
 string result = GetMD5("Net4free");  
      MD5 hash for Net4free is : 52db46c9e0d26245d5d24c38c6517a91




Download



For more information on MD5 : wikipedia.org