usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Security.Cryptography;usingSystem.IO;usingSystem.Text;/// <summary>///Summary description of Des/// </summary> Public classdes{ PublicDes () {} Public Static stringEncrypt (stringStringtoencrypt,stringSKey) {DESCryptoServiceProvider des=NewDESCryptoServiceProvider (); byte[] Inputbytearray = encoding.getencoding ("UTF-8"). GetBytes (Stringtoencrypt); Des. Key=ASCIIEncoding.UTF8.GetBytes (SKey); Des.iv=ASCIIEncoding.UTF8.GetBytes (SKey); MemoryStream Ms=NewMemoryStream (); CryptoStream CS=NewCryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write); Cs. Write (Inputbytearray,0, inputbytearray.length); Cs. FlushFinalBlock (); StringBuilder ret=NewStringBuilder (); foreach(byteBinchMs. ToArray ()) {ret. AppendFormat ("{0:x2}", B); } ret. ToString (); returnret. ToString (); } Public Static stringDecrypt (stringStringtodecrypt,stringSKey) {DESCryptoServiceProvider des=NewDESCryptoServiceProvider (); byte[] Inputbytearray =New byte[Stringtodecrypt.length/2]; for(intx =0; X < Stringtodecrypt.length/2; X + +) { inti = (Convert.ToInt32 (stringtodecrypt.substring (x *2,2), -)); INPUTBYTEARRAY[X]= (byte) I; } des. Key=ASCIIEncoding.UTF8.GetBytes (SKey); Des.iv=ASCIIEncoding.UTF8.GetBytes (SKey); MemoryStream Ms=NewMemoryStream (); CryptoStream CS=NewCryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write); Cs. Write (Inputbytearray,0, inputbytearray.length); Cs. FlushFinalBlock (); StringBuilder ret=NewStringBuilder (); returnSystem.Text.Encoding.Default.GetString (Ms. ToArray ()); }}
string " cba1238b " ; string abc= des.encrypt ("ABC", key); Response.Write (ABC); Response.Write (""); string ABC2 = des.decrypt (ABC, key); Response.Write (ABC2);
C#des Encryption and decryption