On the Internet, many people are asking how to implement URL transmission (Form submission) parameter encryption in the web system. 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 the base classes of Dec encryption and decryption.
File Name: security. CS
Namespace EIP. framework {// security class implements encryption and decryption under the. NET Framework. Public class security {string _ querystringkey = "abcdefgh"; // URL transmission parameter encryption key string _ passwordkey = "hgfedcba "; // password encryption key // The encrypted URL transmission string Public String encryptquerystring (string querystring) {return encrypt (querystring, _ querystringkey );} // decrypt the Public String decryptquerystring (string querystring) {return decrypt (querystring, _ querystringkey);} // encrypt the 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); // establishes the key and offset des of the encryption object. IV = asciiencoding. ASCII. getbytes (skey); // use asciiencoding in the original text. the getbytes method of the ASCII method memorystream MS = new memorystream (); // enables the input password to have the 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 () ;}// 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. 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. 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 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 );