Using system;
Using system. Data;
Using system. configuration;
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. IO;
Using system. Security. cryptography;
Public partial class _ default: system. Web. UI. Page
{
// Default key vector
Private Static byte [] keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
Protected void page_load (Object sender, eventargs E)
{
}
# Region base64
// Encryption
Protected void button#click (Object sender, eventargs E)
{
String context = This. textbox1.text. Trim ();
Byte [] arrs = encoding. getencoding ("UTF-8"). getbytes (context );
This. textbox2.text = convert. tobase64string (arrs );
}
// Decrypt
Protected void button2_click (Object sender, eventargs E)
{
String context = This. textbox1.text. Trim ();
Char [] chars = context. tochararray ();
Byte [] byts = convert. frombase64chararray (chars, 0, chars. Length );
This. textbox2.text = encoding. getencoding ("UTF-8"). getstring (byts );
// Convert.
}
# Endregion
# Region MD5
Protected void button3_click (Object sender, eventargs E)
{
String context = This. textbox3.text. Trim ();
This. textbox4.text = system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (context, "MD5 ");
}
# Endregion
# Region DES encryption and decryption
// Encryption
Protected void button4_click (Object sender, eventargs E)
{
String key = This. texkey. Text. Trim ();
// Stringbuilder key = new stringbuilder (this. texkey. Text. Trim ());
If (key. Length <8)
{
For (INT I = key. length; I <= 8; I ++)
{
Key + = "-";
}
This. texkey. Text = key. tostring ();
}
// Obtain the string to be encrypted
String encryptstring = This. texdes. Text. Trim ();
Byte [] keybytes = encoding. utf8.getbytes (key. substring (0, 8 ));
// Byte [] keyiv = keybytes;
Byte [] keyiv = keys;
Byte [] inputbytearray = encoding. utf8.getbytes (encryptstring );
Descryptoserviceprovider provider = new descryptoserviceprovider ();
Memorystream mstream = new memorystream ();
Cryptostream cstream = new cryptostream (mstream, provider. createencryptor (keybytes, keyiv), cryptostreammode. Write );
Cstream. Write (inputbytearray, 0, inputbytearray. Length );
Cstream. flushfinalblock ();
// Return convert. tobase64string (mstream. toarray ());
This.txt result. Text = convert. tobase64string (mstream. toarray ());
}
// Decrypt
Protected void button5_click (Object sender, eventargs E)
{
// Obtain the decryption string
String decryptstring = This. texdes. Text. Trim ();
// Obtain the key
String key = This. texkey. Text. Trim ();
Byte [] keybytes = encoding. utf8.getbytes (key. substring (0, 8 ));
// Byte [] keyiv = keybytes;
Byte [] keyiv = keys;
Try
{
Byte [] inputbytearray = convert. frombase64string (decryptstring );
Descryptoserviceprovider provider = new descryptoserviceprovider ();
Memorystream mstream = new memorystream ();
Cryptostream cstream = new cryptostream (mstream, provider. createdecryptor (keybytes, keyiv), cryptostreammode. Write );
Cstream. Write (inputbytearray, 0, inputbytearray. Length );
Cstream. flushfinalblock ();
// Return encoding. utf8.getstring (mstream. toarray ());
This.txt result. Text = encoding. utf8.getstring (mstream. toarray ());
}
Catch
{
Texkey. Text = "Incorrect password! ";
}
}
# Endregion
}