首页 > 代码库 > DESC加密与解密

DESC加密与解密

using System;

 using System.IO;
  using System.Security.Cryptography;
  using System.Text;
  using System.Web.Security;

 

public static string Decrypt(string Text)      

   {           

     return Decrypt(Text, "litianping");       //密钥可以自己填写  

   }

 public static string Decrypt(string Text, string sKey)     //Desc加密

     {        

       DESCryptoServiceProvider des = new DESCryptoServiceProvider();    

         int len = Text.Length / 2;      

        byte[] inputByteArray = new byte[len];     

        for (int x = 0; x < len; x++)    

         {      

           int i = Convert.ToInt32(Text.Substring(x * 2, 2), 0x10);  

               inputByteArray[x] = (byte) i;   

          }         

    des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));

    des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));  

     MemoryStream ms = new MemoryStream();     

     CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);  

     cs.Write(inputByteArray, 0, inputByteArray.Length);  

     cs.FlushFinalBlock();     

      return Encoding.Default.GetString(ms.ToArray());   

      }

        public static string Encrypt(string Text)   

      {          

           return Encrypt(Text, "litianping");

        }

      public static string Encrypt(string Text, string sKey)      //Desc解密

      {      

       DESCryptoServiceProvider des = new DESCryptoServiceProvider();  

       byte[] inputByteArray = Encoding.Default.GetBytes(Text);

       des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));

       des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));

       MemoryStream ms = new MemoryStream();   

       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);   

        }            

      return ret.ToString();       

  }

DESC加密与解密