Very simple, just a piece of code. Response. Write (FormsAuthentication. HashPasswordForStoringInConfigFile ("string to be encrypted", "MD5 "));
To make the blog post longer and more valuable, add the frequently-used encryption and decryption code:
Code
/* Usage
Protected void Page_Load (object sender, EventArgs e)
{
// Encryption
This. Title = CEncrypt. DesEncrypt ("pwd", CEncrypt. Key );
This. Title + = CEncrypt. DesDecrypt (this. Title, CEncrypt. Key );
Response. Write (CEncrypt. DesDecrypt ("gAYyhdLQunc =", CEncrypt. Key ));
}
*/
Using System;
Using System. IO;
Using System. Text;
Using System. Security. Cryptography;
Using System. Web;
Namespace YD. Common
{
/// <Summary>
/// Add a password
/// </Summary>
Public class CEncrypt
{
/// <Summary>
/// Encryption
/// </Summary>
/// <Param name = "inputString"> </param>
/// <Returns> </returns>
Public static string DesEncrypt (string inputString)
{
Return DesEncrypt (inputString, Key );
}
/// <Summary>
/// Decrypt
/// </Summary>
/// <Param name = "inputString"> </param>
/// <Returns> </returns>
Public static string DesDecrypt (string inputString)
{
Return DesDecrypt (inputString, Key );
}
/// <Summary>
/// Key
/// </Summary>
Private static string Key
{
Get
{
Return "hongye10 ";
}
}
/// <Summary>
/// Encrypted string
/// Note: The key must be 8 bits.
/// </Summary>
/// <Param name = "strText"> string </param>
/// <Param name = "encryptKey"> key </param>
/// <Param name = "encryptKey"> return the encrypted string </param>
Public static string DesEncrypt (string inputString, string encryptKey)
{
Byte [] byKey = null;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Try
{
ByKey = System. Text. Encoding. UTF8.GetBytes (encryptKey. Substring (0, 8 ));
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
Byte [] inputByteArray = Encoding. UTF8.GetBytes (inputString );
MemoryStream MS = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, des. CreateEncryptor (byKey, IV), CryptoStreamMode. Write );
Cs. Write (inputByteArray, 0, inputByteArray. Length );
Cs. FlushFinalBlock ();
Return Convert. ToBase64String (ms. ToArray ());
}
Catch (System. Exception error)
{
// Return error. Message;
Return null;
}
}
/// <Summary>
/// Decrypt the string
/// </Summary>
/// <Param name = "this. inputString"> encrypted string </param>
/// <Param name = "decryptKey"> key </param>
/// <Param name = "decryptKey"> return the decrypted string </param>
Public static string DesDecrypt (string inputString, string decryptKey)
{
Byte [] byKey = null;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Byte [] inputByteArray = new Byte [inputString. Length];
Try
{
ByKey = System. Text. Encoding. UTF8.GetBytes (decryptKey. Substring (0, 8 ));
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
InputByteArray = Convert. FromBase64String (inputString );
MemoryStream MS = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, des. CreateDecryptor (byKey, IV), CryptoStreamMode. Write );
Cs. Write (inputByteArray, 0, inputByteArray. Length );
Cs. FlushFinalBlock ();
System. Text. Encoding encoding = new System. Text. UTF8Encoding ();
Return encoding. GetString (ms. ToArray ());
}
Catch (System. Exception error)
{
// Return error. Message;
Return null;
}
}
}
}