AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Wednesday, 27 April 2016

How to Encrypt string with MD5 hash algorithm in c#?

In this blog post I will discuss How to Encrypt string with MD5 hash in c#? Use below method for converting a string to MD5 hash.

        #region--GetMd5Hash--
        public static string GetMd5Hash(string input)
        {
            MD5 md5Hash = MD5.Create();
            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
            // Create a new Stringbuilder to collect the bytes 
            // and create a string. 
            StringBuilder sBuilder = new StringBuilder();
            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            int i = 0;
            for (i = 0; i <= data.Length - 1; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }
        #endregion

It's quite simple & easy to convert MD5 hash.

Example : 
string str = "aspbucket.com";
Console.WriteLine(GetMd5Hash(str));

Output: 38a7066d8c26659b8c6e32f3f2fddc5f

0 comments :

Post a Comment

  • Popular Posts
  • Comments