Copy codeThe Code is as follows:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Text;
Using System. Security. Cryptography;
Using System. IO;
Namespace www
{
Public partial class jiami: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Bind ();
}
Private void Bind ()
{
// Encryption
This. Title = DesEncrypt ("pwd", "abcd1234 ");
This. Title + = DesDecrypt (this. Title, "abcd1234 ");
Response. Write (DesDecrypt ("2ikCw0TqKGo =", "abcd1234 "));
}
/// <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 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 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;
}
}
}
}