Des encryption and decryption in ASP. NET, which can be used for parameter passing through URLs

Source: Internet
Author: User
Reference: http://www.cnblogs.com/fecitsky/articles/40659.aspx (modified some comments)
URL transmission (Form submission) parameter encryption is often encountered during webpage creation.
For example, if you want to edit a user account and pass the user ID, the URL is as follows: http: // localhost/mysystem/editaccounts. aspx? Id = 2
But do not want to let others know that the user ID is 2, malicious users may change 2 to another user ID.
The parameter values passed by encryption can solve the problem.
The following are functions for encryption and decryption. Encryption and decryption functions
/** // <Summary>
/// Dec encryption process
/// </Summary>
/// <Param name = "ptoencrypt"> encrypted string </param>
/// <Param name = "skey"> key (only 8-byte key is supported) </param>
/// <Returns> encrypted string </returns>
Public String encrypt (string ptoencrypt, string skey)
{
// Access the encapsulated object of the CSP version of the Data Encryption Standard (DES) algorithm.
Descryptoserviceprovider des = new descryptoserviceprovider ();
Des. Key = asciiencoding. ASCII. getbytes (skey); // create the key and offset of the encryption object
Des. IV = asciiencoding. ASCII. getbytes (skey); // use the getbytes method of the asciiencoding. ASCII method in the original article

Byte [] inputbytearray = encoding. Default. getbytes (ptoencrypt); // put the string in the byte array

Memorystream MS = new memorystream (); // creates a stream that supports the storage as memory
// Define the link of the data stream to the encrypted conversion stream
Cryptostream cs = new cryptostream (MS, Des. createencryptor (), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
// The encrypted result has been stored in the memory.

Stringbuilder ret = new stringbuilder ();
Foreach (byte B in ms. toarray ())
{
Ret. appendformat ("{0: X2}", B );
}
Ret. tostring ();
Return ret. tostring ();
}

/** // <Summary>
/// Dec decryption process
/// </Summary>
/// <Param name = "ptodecrypt"> decrypted string </param>
/// <Param name = "skey"> key (only eight-byte keys are supported, the same as the preceding encryption key) </param>
/// <Returns> returns the decrypted string </returns>
Public String decrypt (string ptodecrypt, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();

Byte [] inputbytearray = new byte [ptodecrypt. Length/2];
For (INT x = 0; x <ptodecrypt. Length/2; X ++)
{
Int I = (convert. toint32 (ptodecrypt. substring (x * 2, 2), 16 ));
Inputbytearray [x] = (byte) I;
}

Des. Key = asciiencoding. ASCII. getbytes (skey); // create the key and offset of the encryption object. This value is important and cannot be modified.
Des. IV = asciiencoding. ASCII. getbytes (skey );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createdecryptor (), cryptostreammode. Write );

CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();

// Create a stringbuild object. createdecrypt uses a stream object. The decrypted text must be converted to a stream object.
Stringbuilder ret = new stringbuilder ();

Return System. Text. encoding. Default. getstring (Ms. toarray ());
}

2. An example of how to use the encryption and decryption algorithm in a program is as follows:
Response. Redirect ("~ /Gridview. aspx? Id = "+ encrypt (" zlh "," abcdefgh "));

On the acceptance page
String acceptstr;
Acceptstr = decrypt (request. querystring ["ID"], "abcdefgh ");

The value of acceptstr should be the "zlh" sent"

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.