This tutorial will show you how easy to implement MD5 hash algorithm using .NET Cryptography Framework in C# to encrypt a string or password.
The code below will help you hash an input string and return the hash as a 32 character hexadecimal string.
You can use the MD5CryptoServiceProvider class to implement compute the MD5 hash value for the input data.
public static string Md5(string input)
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5 md5 = MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
// Create a new Stringbuilder to collect the bytes and create a string.
StringBuilder sb = new StringBuilder();
// Loop through each byte of the hashed data and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
sb.Append(data[i].ToString("x2"));
return sb.ToString();
}
Remember you can't decrypt MD5 hashes