C # MD5 Validation

Source: Internet
Author: User

Sun Guangdong 2014.6.24


Data transmission after the network will become very insecure, the simplest and most effective solution is to add a key to the data, using the MD5 algorithm to calculate the check code, the server received data and check code after the comparison check code is correct, to determine whether the data has been modified. PHP generates the MD5 checksum by default to a 32-bit string, while the C # default is a 16-bit byte array that needs to be slightly modified to 32 bytes of string, the code is as follows:

        public static string Md5sum (String strtoencrypt)        {            //converts the string that needs to be encrypted into a byte array            byte[] bs = UTF8Encoding.UTF8.GetBytes (strtoencrypt);            Create MD5 object            System.Security.Cryptography.MD5 MD5;            MD5 = System.Security.Cryptography.MD5CryptoServiceProvider.Create ();            Generates a 16-bit binary check code            byte[] hashbytes = Md5.computehash (BS);            Convert to 32-bit string            hashstring = "";            for (int i = 0; i < hashbytes.length; i++)            {                hashstring + = System.Convert.ToString (Hashbytes[i], 16). PadLeft (2, ' 0 ');            }            return Hashstring.padleft (+, ' 0 ');        }


Using this MD5 function is very simple, in the following code example, the data is a string containing "Hello World", the key bit 123, using md5sum to calculate the 32-bit checksum string.

            String data = "Hello World";            string key = "123";            md5sum (data + key);  Return






??

C # MD5 Validation

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.