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.