First, import two namespaces.
Using System. Security. Cryptography; // md5-encrypted namespace
Using System. Text;
Second, place the following MD5 method in the public class you wrote.
/// <Summary>
/// MD5 Encryption
/// </Summary>
/// <Param name = "strPwd"> encrypted string </param>
/// <Returns> returns the encrypted string </returns>
Public string MD5 (string strPwd)
{
MD5 md5 = new MD5CryptoServiceProvider ();
Byte [] data = Encoding. Default. GetBytes (strPwd); // encode the character into a byte sequence
Byte [] md5Data = md5.ComputeHash (data); // calculate the hash value of the data byte array;
Md5.Clear (); // release all resources used;
String str = "";
For (int I = 0; I <md5Data. Length-1; I ++)
{
Str + = md5Data [I]. ToString ("x"). PadLeft (2, '0 ');
}
Return str;
}
Third, call the md5 method.
Instantiate your common class name. The string to be encrypted in the MD5 method;
The public class name here is DB method name MD5 txtPwd is a textbox Control used to fill in the password
Call Method
DB db = new DB (); // instantiate a public class object;
String passwordmongodb.md5(this.txt Pwd. Text. Trim () // encrypt the password