Code highlighting produced by actipro codehighlighter (freeware)
Http://www.CodeHighlighter.com/
-->UsingSystem;
UsingSystem. Security. cryptography;
UsingSystem. IO;
UsingSystem. text;
///
///Security summary.
///Security class implements encryption and decryption under the. NET Framework.
///Copyright --
///
/*
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 );
**/
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 );
}
///
/// Decrypts 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 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 ); // The getbytes method of the original article using the asciiencoding. ASCII Method
Memorystream MS = New Memorystream (); // So that the password must be entered 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 ); // Establishes 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= NewStringbuilder ();//Create a stringbuild object. createdecrypt uses a stream object. The decrypted text must be converted to a stream object.
ReturnSystem. 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 ;
}
}
}
}