Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Security.Cryptography;
Namespace XXXXX
{
<summary>
Encrypting files Help Class
</summary>
public class Encryptionhelper
{
<summary>
MD5 (16-bit encryption)
</summary>
<param name= "convertstring" > Strings that need to be encrypted </param>
<RETURNS>MD5 Encrypted String </returns>
public static string Getmd5str (String convertstring)
{
String md5pwd = String. Empty;
Using Cryptographic service Providers
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider ();
Converts the numeric value of each element of the specified byte subarray to its equivalent hexadecimal string representation.
Md5pwd = bitconverter.tostring (Md5.computehash (UTF8Encoding.Default.GetBytes (convertstring)), 4, 8);
Md5pwd = Md5pwd.replace ("-", "" ");
return md5pwd;
}
<summary>
MD5 (32-bit encryption)
</summary>
<param name= "str" > strings that need to be encrypted </param>
<RETURNS>MD5 Encrypted String </returns>
public static string Getmd5hashstr (String str)
{
string pwd = string. Empty;
Instantiate a MD5 pair of images
MD5 MD5 = MD5. Create ();
After the encryption is an array of byte type, here should pay attention to the choice of coding utf8/unicode, etc.
Byte[] s = Md5.computehash (Encoding.UTF8.GetBytes (str));
Converts an array of byte types to a string by using a loop, which is a regular character formatting the resulting
for (int i = 0; i < s.length; i++)
{
The resulting string is formatted using the hexadecimal type. The formatted character is a lowercase letter, and if uppercase (X) is used, the character after the format is uppercase characters
PWD = pwd + s[i]. ToString ("X");
}
return pwd;
}
}
}
C # Two ways to MD5 encryption