C # MD5 verification,

Source: Internet
Author: User

C # MD5 verification,

Sun Guangdong 2014.6.24


After data is transmitted over the network, it becomes very insecure. The simplest and most effective solution is to add a key to the data and use the MD5 algorithm to calculate the verification code, after receiving the data and verification code, the server checks whether the verification code is correct to determine whether the data has been modified. The MD5 verification generated by PHP is a 32-bit string by default, while C # Is a 16-bit byte array by default. You need to modify it slightly to convert it into a 32-byte string. The Code is as follows:

Public static string Md5Sum (string strToEncrypt) {// convert the string to be encrypted into a byte array byte [] bs = UTF8Encoding. UTF8.GetBytes (strToEncrypt); // create the md5 object System. security. cryptography. MD5 md5; md5 = System. security. cryptography. MD5CryptoServiceProvider. create (); // generate a 16-bit binary Verification Code byte [] hashBytes = md5.ComputeHash (bs); // convert it to a 32-Bit string hashString = ""; for (int I = 0; I 


Using this MD5 function is very simple. In the following code example, the data contains a string containing "hello world" and the key bit is 123. Use Md5Sum to calculate the 32-bit verification code string.

String data = "hello world"; string key = "123"; Md5Sum (data + key); // return






Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.