Copy Code code 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>
Encrypt string
Note: The key must be 8 bits
</summary>
<param name= "StrText" > String </param>
<param name= "Encryptkey" > Key </param>
<param name= "Encryptkey" > returns 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>
Decrypting a string
</summary>
<param name= "this.inputstring" > Added a secret string </param>
<param name= "Decryptkey" > Key </param>
<param name= "Decryptkey" > Return 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;
}
}
}
}