Asp.net string encryption and decryption technology

Source: Internet
Author: User

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;
}
}
}
}

Related Article

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.