How to use ASP. Net to encrypt cookies (1)

Source: Internet
Author: User

Cookie is an important global variable in ASP. NET applications. It can be used to save user logon information, user session information, and some global variables or data. To improve the security of applications, it is necessary to encrypt cookies. This example describes how to encrypt cookies in ASP. NET applications.

This example describes how to encrypt a Cookie in an ASP. NET application. The instance uses the DES and TripleDES encryption methods. The program implements the DES and TripleDES encryption and decryption methods, and also sets the CookieEncrypt class for Cookie processing.

1. Create a new ASP. NET application

Create an ASP. NET Web application in the integrated development environment of Visual Studio. NET 2003 and name it Example_12_6.

2. Create an encryption Cookie class EncryptString

Add the class file EncryptString in application Example_12_6. cs. This file implements the methods of using the DES Method to encrypt and decrypt data, and using the TripleDES method to encrypt and decrypt data. It also defines the Key and IV used for encryption and decryption. The program code of the class file EncryptString. cs is as follows:

Public class EncryptString {private static byte [] Key64 = {42, 16, 93,156, 78, 4,218, 32}; private static byte [] IV64 = {55,103,246, 79, 36, 99,167, 3}; private static byte [] Key192 = {42, 16, 93,156, 78, 4,218, 167, 26,250,155,112, 11,204,119, 35,184,197 }; private static byte [] IV192 = {55,103,246, 79, 36, 99,167, 184, 7,209, 13,145, 23,200, 58,173, 10,121,222}; public static String Encrypt (String valueString) {if (valueString! = "") {// Defines the Provider DESCryptoServiceProvider desprovider = new DESCryptoServiceProvider (); // defines the memory stream MemoryStream memoryStream = new MemoryStream (); // define the encryption stream CryptoStream cryptoStream = new CryptoStream (memoryStream, desprovider. createEncryptor (Key64, IV64), CryptoStreamMode. write); // define the Write IO stream StreamWriter writerStream = new StreamWriter (cryptoStream); // Write the encrypted writable stream writerStream. write (valueString); wr IterStream. flush (); cryptoStream. flushFinalBlock (); memoryStream. flush (); // return the encrypted string return (Convert. toBase64String (memoryStream. getBuffer (), 0, (int) memoryStream. length);} return (null);} public static String Decrypt (String valueString) {if (valueString! = "") {// Define DES Provider DESCryptoServiceProvider desprovider = new DESCryptoServiceProvider (); // Convert and decrypt the string to binary byte [] buffer = Convert. fromBase64String (valueString); // defines the memory stream MemoryStream memoryStream = new MemoryStream (); // defines the encryption stream CryptoStream cryptoStream = new CryptoStream (memoryStream, desprovider. createEncryptor (Key64, IV64), CryptoStreamMode. read); // define Read IO Stream StreamReader readerStream = new Stream Reader (cryptoStream); // return the decrypted string return (readerStream. readToEnd ();} return (null);} public static String EncryptTripleDES (String valueString) {if (valueString! = "") {// Defines the Provider TripleDESCryptoServiceProvider triprovider = new TripleDESCryptoServiceProvider (); // defines the memory stream MemoryStream memoryStream = new MemoryStream (); // define the encryption stream CryptoStream cryptoStream = new CryptoStream (memoryStream, triprovider. createEncryptor (Key192, IV192), CryptoStreamMode. write); // define the Write IO stream StreamWriter writerStream = new StreamWriter (cryptoStream); // Write the encrypted writable stream writerStream. wr Ite (valueString); writerStream. flush (); cryptoStream. flushFinalBlock (); memoryStream. flush (); // return the encrypted string return (Convert. toBase64String (memoryStream. getBuffer (), 0, (int) memoryStream. length);} return (null);} public static String DecryptTripleDES (String valueString) {if (valueString! = "") {// Define TripleDES's Provider TripleDESCryptoServiceProvider triprovider = new TripleDESCryptoServiceProvider (); // Convert and decrypt the string to binary byte [] buffer = Convert. fromBase64String (valueString); // defines the memory stream MemoryStream memoryStream = new MemoryStream (); // defines the encrypted stream CryptoStream cryptoStream = new CryptoStream (memoryStream, triprovider. createEncryptor (Key64, IV64), CryptoStreamMode. read); // define Read IO stream StreamReader readerStream = new StreamReader (cryptoStream); // return the decrypted string return (readerStream. readToEnd ();} 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.