Class implements encryption and decryption under the. NET Framework

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. security. cryptography;
using system. io;
using system. text;

//


// Security summary
// security class implements encryption and decryption under the. NET Framework.
//
namespace ptfair. websites. common
{< br> public class security
{< br> string _ querystringkey = configurationsettings. deleetask[ "cryptionkey"]; // URL transmission parameter encryption key
string _ passwordkey = "hgfedcba"; // password encryption key
Public Security ()
{< br> //
// todo: add the constructor logic here
//
}

///
/// Encrypted URL transmission string
///
Public String encryptquerystring (string querystring)
{
Return encrypt (querystring, _ querystringkey );
}

///
/// Decrypt the string transmitted by the URL
///
Public String decryptquerystring (string querystring)
{
Return decrypt (querystring, _ querystringkey );
}

///
/// Encrypted account password
///
Public String encryptpassword (string password)
{
Return encrypt (password, _ passwordkey );
}

///
/// Decrypt the account password
///
Public String decryptpassword (string password)
{
Return decrypt (password, _ passwordkey );
}

///
/// Dec encryption process
///
Public String encrypt (string ptoencrypt, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider (); // put the string in the byte array

Byte [] inputbytearray = encoding. Default. getbytes (ptoencrypt );
// Byte [] inputbytearray = encoding. Unicode. getbytes (ptoencrypt );
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
Memorystream MS = new memorystream (); // enables the input password to be in English text
Cryptostream cs = new cryptostream (MS, Des. createencryptor (), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Stringbuilder ret = new stringbuilder ();
Foreach (byte B in ms. toarray ())
{
Ret. appendformat ("{0: X2}", B );
}

Ret. tostring ();
Return ret. tostring ();
}

///
/// Dec decryption process
///
Public String decrypt (string ptodecrypt, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();
Int Len = ptodecrypt. Length/2;
Byte [] inputbytearray = new byte [Len];
For (INT x = 0; x <Len; 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 ();
Stringbuilder ret = new stringbuilder (); // create a stringbuild object. createdecrypt uses a stream object and the decrypted text must be converted into a stream object.
Return System. Text. encoding. Default. getstring (Ms. toarray ());
}

///
/// Check whether the encrypted string is the same as the original one
///
Public bool validatestring (string enstring, string fostring, int Mode)
{
Switch (Mode)
{
Default:
Case 1:
If (decrypt (enstring, _ querystringkey) = fostring. tostring ())
{
Return true;
}
Else
{
Return false;
}
Case 2:
If (decrypt (enstring, _ passwordkey) = fostring. tostring ())
{
Return true;
}
Else
{
Return false;
}
}
}
}
}

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.