This article shares the blog from the Green Orchard, the address is: http://www.cnblogs.com/qqingmu/archive/2008/01/10/1034168.html
We often encounter URL transfer (form submission) parameter encryption when we make Web pages.
For example: To make a user account edit, to pass the user's id,url 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 also change the 2 to change the user ID. Encrypting passed parameter values resolves the issue.
1, the following is the DEC encryption, decryption function.
Encryption process:
1 /**//// <summary>2 ///DEC Encryption Process3 /// </summary>4 /// <param name= "Ptoencrypt" >strings that are encrypted</param>5 /// <param name= "SKey" >Key (only 8-byte keys are supported)</param>6 /// <returns>the encrypted string</returns>7 Public stringEncrypt (stringPtoencrypt,stringSKey)8 {9 //A wrapper object that accesses the cryptographic service provider (CSP) version of the Data Encryption Standard (DES) algorithmTenDESCryptoServiceProvider des =NewDESCryptoServiceProvider (); OneDes. Key = ASCIIEncoding.ASCII.GetBytes (SKey);//establish keys and offsets for encrypted objects ADES.IV = ASCIIEncoding.ASCII.GetBytes (SKey);//GetBytes method of using Asciiencoding.ascii method in original text - - byte[] Inputbytearray = Encoding.Default.GetBytes (Ptoencrypt);//Put the string in a byte array the -MemoryStream ms =NewMemoryStream ();//Create a stream whose support store is memory - //define a stream that links a data flow to an encryption transformation -CryptoStream cs =NewCryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write); +Cs. Write (Inputbytearray,0, inputbytearray.length); - cs. FlushFinalBlock (); + //it's done. Put the encrypted results in memory A atStringBuilder ret =NewStringBuilder (); - foreach(byteBinchMs. ToArray ()) - { -Ret. AppendFormat ("{0:x2}", b); - } - ret. ToString (); in returnret. ToString (); -}
Decryption process:
1 /**//// <summary>2 ///DEC Decryption Process3 /// </summary>4 /// <param name= "Ptodecrypt" >the decrypted string</param>5 /// <param name= "SKey" >Key (only 8-byte keys are supported, same as previous encryption keys)</param>6 /// <returns>returns the decrypted string</returns>7 Public stringDecrypt (stringPtodecrypt,stringSKey)8 {9DESCryptoServiceProvider des =NewDESCryptoServiceProvider ();Ten One byte[] Inputbytearray =New byte[Ptodecrypt.length/2]; A for(intx =0; X < Ptodecrypt.length/2; X + +) - { - inti = (Convert.ToInt32 (ptodecrypt.substring (x *2,2), -)); theINPUTBYTEARRAY[X] = (byte) I; - } - -Des. Key = ASCIIEncoding.ASCII.GetBytes (SKey);//establishes the key and offset of the encrypted object, which is important and cannot be modified +DES.IV =ASCIIEncoding.ASCII.GetBytes (SKey); -MemoryStream ms =NewMemoryStream (); +CryptoStream cs =NewCryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write); A atCs. Write (Inputbytearray,0, inputbytearray.length); - cs. FlushFinalBlock (); - - //Create Stringbuild object, Createdecrypt use Stream object, must turn decrypted text into stream object -StringBuilder ret =NewStringBuilder (); - in returnSystem.Text.Encoding.Default.GetString (Ms. ToArray ()); -}
2, specific in the program using encryption and decryption algorithm examples are as follows:
1 on the Send page2Response.Redirect ("~/gridview.aspx?id="+ Encrypt ("ZLH","12345678"));3 4 on the Accept page5 stringAcceptstr;6Acceptstr = Decrypt (request.querystring["ID"],"12345678");
Asp. The DEC encryption and decryption process in net