代码改变世界

[DEncrypt] DESEncrypt--加密/解密帮助类 (转载)

2014-03-27 15:38  无抿屎的牛牛  阅读(2426)  评论(0编辑  收藏  举报

点击下载 DESEncrypt.zip

这个类是关于加密,解密的操作,文件的一些高级操作
1.DESEncrypt加密
2.DESEncrypt解密
看下面代码吧

/// <summary>
/// 类说明:Assistant
/// 编 码 人:苏飞
/// 联系方式:361983679  
/// 更新网站:[url=http://www.cckan.net/thread-655-1-1.html]http://www.cckan.net/thread-655-1-1.html[/url]
/// </summary>
using System;
using System.Security.Cryptography;  
using System.Text;
namespace DotNet.Utilities
{
  /// <summary>
  /// DES加密/解密类。
  /// </summary>
  public class DESEncrypt
  {
    public DESEncrypt()
    {      
    }
 
    #region ========加密======== 
  
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
    public static string Encrypt(string Text) 
    {
      return Encrypt(Text,"MATICSOFT");
    }
    /// <summary> 
    /// 加密数据 
    /// </summary> 
    /// <param name="Text"></param> 
    /// <param name="sKey"></param> 
    /// <returns></returns> 
    public static string Encrypt(string Text,string sKey) 
    { 
      DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
      byte[] inputByteArray; 
      inputByteArray=Encoding.Default.GetBytes(Text); 
      des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
      des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
      System.IO.MemoryStream ms=new System.IO.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(); 
    } 
 
    #endregion
     
    #region ========解密======== 
    
  
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
    public static string Decrypt(string Text) 
    {
      return Decrypt(Text,"MATICSOFT");
    }
    /// <summary> 
    /// 解密数据 
    /// </summary> 
    /// <param name="Text"></param> 
    /// <param name="sKey"></param> 
    /// <returns></returns> 
    public static string Decrypt(string Text,string sKey) 
    { 
      DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
      int len; 
      len=Text.Length/2; 
      byte[] inputByteArray = new byte[len]; 
      int x,i; 
      for(x=0;x<len;x++) 
      { 
        i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); 
        inputByteArray[x]=(byte)i; 
      } 
      des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
      des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
      System.IO.MemoryStream ms=new System.IO.MemoryStream(); 
      CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write); 
      cs.Write(inputByteArray,0,inputByteArray.Length); 
      cs.FlushFinalBlock(); 
      return Encoding.Default.GetString(ms.ToArray()); 
    } 
  
    #endregion 
 
 
  }
}

使用方法也很简单直接调用方法就行了如下

DESEncrypt.Encrypt("要加密的字符串");
          DESEncrypt.Decrypt("要解密的字符串");