/// <summary>///calculates a MD5 hash from the given string and uses the given///encoding./// </summary>/// <param name= "Input" >Input String</param>/// <param name= "useencoding" >Encoding Method</param>/// <returns>MD5 Computed String</returns> Public Static stringCalculateMD5 (stringInput, Encoding useencoding) {System.Security.Cryptography.MD5CryptoServiceProvider cryptoservice; Cryptoservice=NewSystem.Security.Cryptography.MD5CryptoServiceProvider (); byte[] Inputbytes =useencoding.getbytes (Input); Inputbytes=Cryptoservice.computehash (inputbytes); returnBitconverter.tostring (inputbytes). Replace ("-","");}/// <summary>///calculates a MD5 hash from the given string.///(by using the default encoding)/// </summary>/// <param name= "Input" >Input String</param>/// <returns>MD5 Computed String</returns> Public Static stringCalculateMD5 (stringInput) { //That's just a shortcut to the base method returnCalculateMD5 (Input, System.Text.Encoding.Default);}//Invocation Example://The example below shows how to verify a password//by using a md5-hash://Password could is from user inputstringPlainpassword ="Secret Password";stringHashedpassword =CalculateMD5 (Plainpassword);//This hash could come from the databasestringStoredpassword ="a584efafa8f9ea7fe5cf18442f32b07b";//is the hashes equal?if(Hashedpassword = =Storedpassword) MessageBox.Show ("Correct password!");ElseMessageBox.Show ("Sorry, bad password:-(");
C#MD5 Calculation Code