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