ASP. NET string encryption and decryption

Source: Internet
Author: User

1 usingSystem;2 usingSystem.Data;3 usingSystem.Configuration;4 usingsystem.web;5 usingSystem.Web.Security;6 usingSystem.Web.UI;7 usingSystem.Web.UI.WebControls;8 usingSystem.Web.UI.WebControls.WebParts;9 usingSystem.Web.UI.HtmlControls;Ten usingSystem.Xml; One usingSystem.Data.SqlClient; A usingSystem.IO; - usingSystem.Net; - usingSystem.Text; the usingSystem.Text.RegularExpressions; - usingSystem.Security.Cryptography; -  -  + /// <summary> -     ///Encrypt +     /// </summary> A     /// <param name= "Ptoencrypt" >the string to encrypt</param> at     /// <param name= "SKey" >Encryption Key Example: "ABCDEFGH"</param> -     /// <returns></returns> -      Public stringEncrypt (stringptoencrypt) -     { -         stringSKey ="ABCDEFGH"; -DESCryptoServiceProvider des =NewDESCryptoServiceProvider ();//Put the string in a byte array in         byte[] Inputbytearray =Encoding.Default.GetBytes (ptoencrypt); -         //byte[] Inputbytearray=encoding.unicode.getbytes (ptoencrypt);  toDes. Key = ASCIIEncoding.ASCII.GetBytes (SKey);//establish keys and offsets for encrypted objects +DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey);//GetBytes method of using Asciiencoding.ascii method in original text -MemoryStream ms =NewMemoryStream ();//make the input password must be entered in English text theCryptoStream cs =NewCryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write); *Cs. Write (Inputbytearray,0, inputbytearray.length); $ cs. FlushFinalBlock ();Panax NotoginsengStringBuilder ret =NewStringBuilder (); -         foreach(byteBinchMs. ToArray ()) the         { +Ret. AppendFormat ("{0:x2}", b); A         } the ret. ToString (); +         returnret. ToString (); -     } $     /// <summary> $     ///decryption -     /// </summary> -     /// <param name= "Ptodecrypt" >to decrypt a string</param> the     /// <param name= "SKey" >Decryption Key Example: "ABCDEFGH"</param> -     /// <returns></returns>Wuyi      Public stringDecrypt (stringptodecrypt) the     { -         stringSKey ="ABCDEFGH"; WuDESCryptoServiceProvider des =NewDESCryptoServiceProvider (); -         byte[] Inputbytearray =New byte[Ptodecrypt.length/2]; About          for(intx =0; X < Ptodecrypt.length/2; X + +) $         { -             inti = (Convert.ToInt32 (ptodecrypt.substring (x *2,2), -)); -INPUTBYTEARRAY[X] = (byte) I; -         } ADes. 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); theMemoryStream ms =NewMemoryStream (); -CryptoStream cs =NewCryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write); $Cs. Write (Inputbytearray,0, inputbytearray.length); the cs. FlushFinalBlock (); theStringBuilder ret =NewStringBuilder ();//Create Stringbuild object, Createdecrypt use Stream object, must turn decrypted text into stream object the         returnSystem.Text.Encoding.Default.GetString (Ms. ToArray ()); the}

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.