On the internet for a while, and found a lot of similar articles, quotations are as follows:
public static string MD5(string Sourcein)
{
MD5CryptoServiceProvider MD5CSP = new MD5CryptoServiceProvider();
byte[] MD5Source = System.Text.Encoding.UTF8.GetBytes(Sourcein);
byte[] MD5Out = MD5CSP.ComputeHash(MD5Source);
return Convert.ToBase64String(MD5Out);
}
In so doing, convert.tobase64string will be a string of alphanumeric and conforming strings, such as: afw1fw3j=-
This is not the same as the encrypted string stored in the database and cannot be compared naturally. A string like baba327d241746ee0829e7e88117d4d5 is stored in the database.
Google for a long time, never found in the ASP.net how to achieve the above appearance of output. So do-it-Yourself, check MSDN, countless practices, and finally find a way to use. NET in the Bitconverter class, nonsense not to say, the code posted out,
private void Btnmd5_click (object sender, EventArgs e) {
if (string). IsNullOrEmpty (Txtsrc.text)) {
Txtsrc.text = "Please Input your string to Encrype";
Return
}
MD5CryptoServiceProvider md5encrypter = new MD5CryptoServiceProvider ();
byte[] thesrc = Encoding.UTF8.GetBytes (Txtsrc.text);
byte[] theresbytes = Md5encrypter.computehash (THESRC);
Txtresult.text = bitconverter.tostring (theresbytes). Replace ("-", "");
}
private void Btnhash_click (object sender, EventArgs e) {
if (string). IsNullOrEmpty (Txtsrc.text)) {
Txtsrc.text = "Please Input your string to Encrype";
Return
}
HashAlgorithm hashencrypter = new sha1managed ();
byte[] thesrc = Encoding.UTF8.GetBytes (Txtsrc.text);
byte[] theresbytes = Hashencrypter.computehash (THESRC);
Txtresult.text = bitconverter.tostring (Theresbytes). Replace ("-", "");
}