File Name: security. CS
Using system;
Using system. Security. cryptography;
Using system. IO;
Using system. text;
Namespace EIP. Framework
{
///
/// Security summary.
/// Security class implements encryption and decryption under the. NET Framework.
// Copyright KangSoft@Hotmail.com @ Hotmail.com@hotmail.com
///
Public class security
{
String _ querystringkey = "abcdefgh"; // URL transmission parameter encryption key
String _ passwordkey = "hgfedcba"; // password encryption key
Public Security ()
{
//
// 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 ();
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 ();
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;
}
}
}
}
}
Different keys are used for URL and account encryption. The URL encryption process is as follows:
EIP. Framework. Security objsecurity = new EIP. Framework. Security ();
Objsecurity. encryptquerystring (''string to be encrypted '');
Decryption: objsecurity. decryptquerystring (''passed parameter );
In the aspx file, you must first instantiate the public class in Aspx. CS, and then use the class. method call method in aspx to use
Directly instantiate in Aspx. CS and then use