. NET MD5 and hash encryption and ciphertext output (C #)

Source: Internet
Author: User
Tags hash md5 tostring

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 ("-", "");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.