Base64, MD5, and DES encryption

Source: Internet
Author: User

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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.