 
                    
                
         
    
    
    
	
	
		
	
		
		
		
			    
        
        ASP.NET 加密类,个人觉得很适用,收藏
    
Type text here
![]()
![]() /**//**********************Created by Chen**************************
/**//**********************Created by Chen**************************
![]()
![]() *如果你觉得本人的文章好,要引用请尊重著作人的劳动果实,说明
*如果你觉得本人的文章好,要引用请尊重著作人的劳动果实,说明 
![]()
![]() *出处以及原创作者,Thank you!!!   email:aishen944-sohu.com
*出处以及原创作者,Thank you!!!   email:aishen944-sohu.com
![]()
![]() *******************************************************************/
*******************************************************************/
![]()
![]() using System;
using System;
![]() using System.Text;
using System.Text;
![]() using System.Security;
using System.Security;
![]() using System.Security.Cryptography;
using System.Security.Cryptography;
![]() using System.IO;
using System.IO;
![]() namespace EncryptClasses
namespace EncryptClasses
![]()
![]()
![]() {
{
![]()
![]() /**//// <summary>
 /**//// <summary>
![]() /// 此处定义的是DES加密,为了便于今后的管理和维护
 /// 此处定义的是DES加密,为了便于今后的管理和维护
![]() /// 请不要随便改动密码,或者改变了密码后请一定要
 /// 请不要随便改动密码,或者改变了密码后请一定要
![]() /// 牢记先前的密码,否则将会照成不可预料的损失
 /// 牢记先前的密码,否则将会照成不可预料的损失
![]() /// </summary>
 /// </summary>
![]() public class DESEncrypt
 public class DESEncrypt
![]()
![]() 
 ![]() {
{
![]()
![]() "member fields"#region "member fields"
  "member fields"#region "member fields"
![]() private string iv="12345678";
  private string iv="12345678";
![]() private string key="12345678";
  private string key="12345678";
![]() private Encoding encoding=new UnicodeEncoding();
  private Encoding encoding=new UnicodeEncoding();
![]() private DES des;
  private DES des;
![]() #endregion
  #endregion
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 构造函数
  /// 构造函数
![]() /// </summary>
  /// </summary>
![]() public DESEncrypt()
  public DESEncrypt()
![]()
![]() 
  ![]() {
{
![]() des=new DESCryptoServiceProvider();
   des=new DESCryptoServiceProvider();
![]() }
  }
![]()
![]() "propertys"#region "propertys"
  "propertys"#region "propertys"
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 设置加密密钥
  /// 设置加密密钥
![]() /// </summary>
  /// </summary>
![]() public string EncryptKey
  public string EncryptKey
![]()
![]() 
  ![]() {
{
![]()
![]() get
   get![]() {return this.key;}
{return this.key;}
![]() set
   set
![]()
![]() 
   ![]() {
{
![]() this.key=value;
      this.key=value;
![]() }
   }
![]() }
  }
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 要加密字符的编码模式
  /// 要加密字符的编码模式
![]() /// </summary>
  /// </summary>
![]() public Encoding EncodingMode
  public Encoding EncodingMode
![]()
![]() 
  ![]() {
{
![]()
![]() get
   get![]() {return this.encoding;}
{return this.encoding;}
![]()
![]() set
   set![]() {this.encoding=value;}
{this.encoding=value;}
![]() }
  }
![]() #endregion
  #endregion
![]()
![]() "methods"#region "methods"
  "methods"#region "methods"
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 加密字符串并返回加密后的结果
  /// 加密字符串并返回加密后的结果
![]() /// </summary>
  /// </summary>
![]() /// <param name="str"></param>
  /// <param name="str"></param>
![]() /// <returns></returns>
  /// <returns></returns>
![]() public string EncryptString(string str)
  public string EncryptString(string str)
![]()
![]() 
  ![]() {
{
![]() byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
   byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
![]() byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);//得到加密密钥
   byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);//得到加密密钥
![]() byte[] toEncrypt=this.EncodingMode.GetBytes(str);//得到要加密的内容
   byte[] toEncrypt=this.EncodingMode.GetBytes(str);//得到要加密的内容
![]() byte[] encrypted;
   byte[] encrypted;
![]() ICryptoTransform encryptor=des.CreateEncryptor(keyb,ivb);
   ICryptoTransform encryptor=des.CreateEncryptor(keyb,ivb);
![]() MemoryStream msEncrypt=new MemoryStream();
   MemoryStream msEncrypt=new MemoryStream();
![]() CryptoStream csEncrypt=new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write);
   CryptoStream csEncrypt=new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write);
![]() csEncrypt.Write(toEncrypt,0,toEncrypt.Length);
   csEncrypt.Write(toEncrypt,0,toEncrypt.Length);
![]() csEncrypt.FlushFinalBlock();
   csEncrypt.FlushFinalBlock();
![]() encrypted=msEncrypt.ToArray();
   encrypted=msEncrypt.ToArray();
![]() csEncrypt.Close();
   csEncrypt.Close();
![]() msEncrypt.Close();
   msEncrypt.Close();
![]() return this.EncodingMode.GetString(encrypted);
   return this.EncodingMode.GetString(encrypted);
![]() }
  }
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 加密指定的文件,如果成功返回True,否则false
  /// 加密指定的文件,如果成功返回True,否则false
![]() /// </summary>
  /// </summary>
![]() /// <param name="filePath">要加密的文件路径</param>
  /// <param name="filePath">要加密的文件路径</param>
![]() /// <param name="outPath">加密后的文件输出路径</param>
  /// <param name="outPath">加密后的文件输出路径</param>
![]() public void EncryptFile(string filePath,string outPath)
  public void EncryptFile(string filePath,string outPath)
![]()
![]() 
  ![]() {
{
![]() bool isExist=File.Exists(filePath);
   bool isExist=File.Exists(filePath);
![]() if(isExist)//如果存在
   if(isExist)//如果存在
![]()
![]() 
   ![]() {
{
![]() byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
    byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
![]() byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
    byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
![]() //得到要加密文件的字节流
    //得到要加密文件的字节流
![]() FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
    FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
![]() StreamReader reader=new StreamReader(fin,this.EncodingMode);
    StreamReader reader=new StreamReader(fin,this.EncodingMode);
![]() string dataStr=reader.ReadToEnd();
    string dataStr=reader.ReadToEnd();
![]() byte[] toEncrypt=this.EncodingMode.GetBytes(dataStr);
    byte[] toEncrypt=this.EncodingMode.GetBytes(dataStr);
![]() fin.Close();
    fin.Close();
![]()
![]() FileStream fout=new FileStream(outPath,FileMode.Create,FileAccess.Write);
    FileStream fout=new FileStream(outPath,FileMode.Create,FileAccess.Write);
![]() ICryptoTransform encryptor=des.CreateEncryptor(keyb,ivb);
    ICryptoTransform encryptor=des.CreateEncryptor(keyb,ivb);
![]() CryptoStream csEncrypt=new CryptoStream(fout,encryptor,CryptoStreamMode.Write);
    CryptoStream csEncrypt=new CryptoStream(fout,encryptor,CryptoStreamMode.Write);
![]() try
    try
![]()
![]() 
    ![]() {
{
![]() //加密得到的文件字节流
     //加密得到的文件字节流
![]() csEncrypt.Write(toEncrypt,0,toEncrypt.Length);
     csEncrypt.Write(toEncrypt,0,toEncrypt.Length);
![]() csEncrypt.FlushFinalBlock();
     csEncrypt.FlushFinalBlock();
![]() }
    }
![]() catch(Exception err)
    catch(Exception err)
![]()
![]() 
    ![]() {
{
![]() throw new ApplicationException(err.Message);
     throw new ApplicationException(err.Message);
![]() }
    }
![]() finally
    finally
![]()
![]() 
    ![]() {
{
![]() try
     try
![]()
![]() 
     ![]() {
{
![]() fout.Close();
      fout.Close();
![]() csEncrypt.Close();
      csEncrypt.Close();
![]() }
     }
![]() catch
     catch
![]()
![]() 
     ![]() {
{
![]() ;
      ;
![]() }
     }
![]() }
    }
![]() }
   }
![]() else
   else
![]()
![]() 
   ![]() {
{
![]() throw new FileNotFoundException("没有找到指定的文件");
    throw new FileNotFoundException("没有找到指定的文件");
![]() }
   }
![]() }
  }
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 文件加密函数的重载版本,如果不指定输出路径,
  /// 文件加密函数的重载版本,如果不指定输出路径,
![]() /// 那么原来的文件将被加密后的文件覆盖
  /// 那么原来的文件将被加密后的文件覆盖
![]() /// </summary>
  /// </summary>
![]() /// <param name="filePath"></param>
  /// <param name="filePath"></param>
![]() public void EncryptFile(string filePath)
  public void EncryptFile(string filePath)
![]()
![]() 
  ![]() {
{
![]() this.EncryptFile(filePath,filePath);
   this.EncryptFile(filePath,filePath);
![]() }
  }
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 解密给定的字符串
  /// 解密给定的字符串
![]() /// </summary>
  /// </summary>
![]() /// <param name="str">要解密的字符</param>
  /// <param name="str">要解密的字符</param>
![]() /// <returns></returns>
  /// <returns></returns>
![]() public string DecryptString(string str)
  public string DecryptString(string str)
![]()
![]() 
  ![]() {
{
![]() byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
   byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
![]() byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
   byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
![]() byte[] toDecrypt=this.EncodingMode.GetBytes(str);
   byte[] toDecrypt=this.EncodingMode.GetBytes(str);
![]() byte[] deCrypted=new byte[toDecrypt.Length];
   byte[] deCrypted=new byte[toDecrypt.Length];
![]() ICryptoTransform deCryptor=des.CreateDecryptor(keyb,ivb);
   ICryptoTransform deCryptor=des.CreateDecryptor(keyb,ivb);
![]() MemoryStream msDecrypt=new MemoryStream(toDecrypt);
   MemoryStream msDecrypt=new MemoryStream(toDecrypt);
![]() CryptoStream csDecrypt=new CryptoStream(msDecrypt,deCryptor,CryptoStreamMode.Read);
   CryptoStream csDecrypt=new CryptoStream(msDecrypt,deCryptor,CryptoStreamMode.Read);
![]() try
   try
![]()
![]() 
   ![]() {
{
![]() csDecrypt.Read(deCrypted,0,deCrypted.Length);
    csDecrypt.Read(deCrypted,0,deCrypted.Length);
![]() }
   }
![]() catch(Exception err)
   catch(Exception err)
![]()
![]() 
   ![]() {
{
![]() throw new ApplicationException(err.Message);
    throw new ApplicationException(err.Message);
![]() }
   }
![]() finally
   finally
![]()
![]() 
   ![]() {
{
![]() try
    try
![]()
![]() 
    ![]() {
{
![]() msDecrypt.Close();
     msDecrypt.Close();
![]() csDecrypt.Close();
     csDecrypt.Close();
![]() }
    }
![]()
![]() catch
    catch![]() {;}
{;}
![]() }
   }
![]() return this.EncodingMode.GetString(deCrypted);
   return this.EncodingMode.GetString(deCrypted);
![]() }
  }
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 解密指定的文件
  /// 解密指定的文件
![]() /// </summary>
  /// </summary>
![]() /// <param name="filePath">要解密的文件路径</param>
  /// <param name="filePath">要解密的文件路径</param>
![]() /// <param name="outPath">解密后的文件输出路径</param>
  /// <param name="outPath">解密后的文件输出路径</param>
![]() public void DecryptFile(string filePath,string outPath)
  public void DecryptFile(string filePath,string outPath)
![]()
![]() 
  ![]() {
{
![]() bool isExist=File.Exists(filePath);
   bool isExist=File.Exists(filePath);
![]() if(isExist)//如果存在
   if(isExist)//如果存在
![]()
![]() 
   ![]() {
{
![]() byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
    byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
![]() byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
    byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
![]() FileInfo file=new FileInfo(filePath);
    FileInfo file=new FileInfo(filePath);
![]() byte[] deCrypted=new byte[file.Length];
    byte[] deCrypted=new byte[file.Length];
![]() //得到要解密文件的字节流
    //得到要解密文件的字节流
![]() FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
    FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
![]() //解密文件
    //解密文件
![]() try
    try
![]()
![]() 
    ![]() {
{
![]() ICryptoTransform decryptor=des.CreateDecryptor(keyb,ivb);
     ICryptoTransform decryptor=des.CreateDecryptor(keyb,ivb);
![]() CryptoStream csDecrypt=new CryptoStream(fin,decryptor,CryptoStreamMode.Read);
     CryptoStream csDecrypt=new CryptoStream(fin,decryptor,CryptoStreamMode.Read);
![]() csDecrypt.Read(deCrypted,0,deCrypted.Length);
     csDecrypt.Read(deCrypted,0,deCrypted.Length);
![]() }
    }
![]() catch(Exception err)
    catch(Exception err)
![]()
![]() 
    ![]() {
{
![]() throw new ApplicationException(err.Message);
     throw new ApplicationException(err.Message);
![]() }
    }
![]() finally
    finally
![]()
![]() 
    ![]() {
{
![]() try
     try
![]()
![]() 
     ![]() {
{
![]() fin.Close();
      fin.Close();
![]() }
     }
![]()
![]() catch
     catch![]() {;}
{;}
![]() }
    }
![]() FileStream fout=new FileStream(outPath,FileMode.Create,FileAccess.Write);
    FileStream fout=new FileStream(outPath,FileMode.Create,FileAccess.Write);
![]() fout.Write(deCrypted,0,deCrypted.Length);
    fout.Write(deCrypted,0,deCrypted.Length);
![]() fout.Close();
    fout.Close();
![]() }
   }
![]() else
   else
![]()
![]() 
   ![]() {
{
![]() throw new FileNotFoundException("指定的解密文件没有找到");
    throw new FileNotFoundException("指定的解密文件没有找到");
![]() }
   }
![]() }
  }
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 解密文件的重载版本,如果没有给出解密后文件的输出路径,
  /// 解密文件的重载版本,如果没有给出解密后文件的输出路径,
![]() /// 则解密后的文件将覆盖先前的文件
  /// 则解密后的文件将覆盖先前的文件
![]() /// </summary>
  /// </summary>
![]() /// <param name="filePath"></param>
  /// <param name="filePath"></param>
![]() public void DecryptFile(string filePath)
  public void DecryptFile(string filePath)
![]()
![]() 
  ![]() {
{
![]() this.DecryptFile(filePath,filePath);
   this.DecryptFile(filePath,filePath);
![]() }
  }
![]() #endregion
  #endregion
![]() }
 }
![]()
![]() /**//// <summary>
 /**//// <summary>
![]() /// MD5加密类,注意经MD5加密过的信息是不能转换回原始数据的
 /// MD5加密类,注意经MD5加密过的信息是不能转换回原始数据的
![]() /// ,请不要在用户敏感的信息中使用此加密技术,比如用户的密码,
 /// ,请不要在用户敏感的信息中使用此加密技术,比如用户的密码,
![]() /// 请尽量使用对称加密
 /// 请尽量使用对称加密
![]() /// </summary>
 /// </summary>
![]() public class MD5Encrypt
 public class MD5Encrypt
![]()
![]() 
 ![]() {
{
![]() private MD5 md5;
  private MD5 md5;
![]() public MD5Encrypt()
  public MD5Encrypt()
![]()
![]() 
  ![]() {
{
![]() md5=new MD5CryptoServiceProvider();
   md5=new MD5CryptoServiceProvider();
![]() }
  }
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 从字符串中获取散列值
  /// 从字符串中获取散列值
![]() /// </summary>
  /// </summary>
![]() /// <param name="str">要计算散列值的字符串</param>
  /// <param name="str">要计算散列值的字符串</param>
![]() /// <returns></returns>
  /// <returns></returns>
![]() public string GetMD5FromString(string str)
  public string GetMD5FromString(string str)
![]()
![]() 
  ![]() {
{
![]() byte[] toCompute=Encoding.Unicode.GetBytes(str);
   byte[] toCompute=Encoding.Unicode.GetBytes(str);
![]() byte[] hashed=md5.ComputeHash(toCompute,0,toCompute.Length);
   byte[] hashed=md5.ComputeHash(toCompute,0,toCompute.Length);
![]() return Encoding.ASCII.GetString(hashed);
   return Encoding.ASCII.GetString(hashed);
![]() }
  }
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 根据文件来计算散列值
  /// 根据文件来计算散列值
![]() /// </summary>
  /// </summary>
![]() /// <param name="filePath">要计算散列值的文件路径</param>
  /// <param name="filePath">要计算散列值的文件路径</param>
![]() /// <returns></returns>
  /// <returns></returns>
![]() public string GetMD5FromFile(string filePath)
  public string GetMD5FromFile(string filePath)
![]()
![]() 
  ![]() {
{
![]() bool isExist=File.Exists(filePath);
   bool isExist=File.Exists(filePath);
![]() if(isExist)//如果文件存在
   if(isExist)//如果文件存在
![]()
![]() 
   ![]() {
{
![]() FileStream stream=new FileStream(filePath,FileMode.Open,FileAccess.Read);
    FileStream stream=new FileStream(filePath,FileMode.Open,FileAccess.Read);
![]() StreamReader reader=new StreamReader(stream,Encoding.Unicode);
    StreamReader reader=new StreamReader(stream,Encoding.Unicode);
![]() string str=reader.ReadToEnd();
    string str=reader.ReadToEnd();
![]() byte[] toHash=Encoding.Unicode.GetBytes(str);
    byte[] toHash=Encoding.Unicode.GetBytes(str);
![]() byte[] hashed=md5.ComputeHash(toHash,0,toHash.Length);
    byte[] hashed=md5.ComputeHash(toHash,0,toHash.Length);
![]() stream.Close();
    stream.Close();
![]() return Encoding.ASCII.GetString(hashed);
    return Encoding.ASCII.GetString(hashed);
![]() }
   }
![]() else//文件不存在
   else//文件不存在
![]()
![]() 
   ![]() {
{
![]() throw new FileNotFoundException("指定的文件没有找到");
    throw new FileNotFoundException("指定的文件没有找到");
![]() }
   }
![]() }
  }
![]() }
 }
![]()
![]() /**//// <summary>
 /**//// <summary>
![]() /// 用于数字签名的hash类
 /// 用于数字签名的hash类
![]() /// </summary>
 /// </summary>
![]() public class MACTripleDESEncrypt
 public class MACTripleDESEncrypt
![]()
![]() 
 ![]() {
{
![]() private MACTripleDES mact;
  private MACTripleDES mact;
![]() private string __key="ksn168ch";
  private string __key="ksn168ch";
![]() private byte[] __data=null;
  private byte[] __data=null;
![]() public MACTripleDESEncrypt()
  public MACTripleDESEncrypt()
![]()
![]() 
  ![]() {
{
![]() mact=new MACTripleDES();
   mact=new MACTripleDES();
![]() }
  }
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 获取或设置用于数字签名的密钥
  /// 获取或设置用于数字签名的密钥
![]() /// </summary>
  /// </summary>
![]() public string Key
  public string Key
![]()
![]() 
  ![]() {
{
![]()
![]() get
   get![]() {return this.__key;}
{return this.__key;}
![]() set
   set
![]()
![]() 
   ![]() {
{
![]() int keyLength=value.Length;
    int keyLength=value.Length;
![]()
![]() int[] keyAllowLengths=new int[]
    int[] keyAllowLengths=new int[]![]() {8,16,24};
{8,16,24};
![]() bool isRight=false;
    bool isRight=false;
![]() foreach(int i in keyAllowLengths)
    foreach(int i in keyAllowLengths)
![]()
![]() 
    ![]() {
{
![]() if(keyLength==keyAllowLengths[i])
     if(keyLength==keyAllowLengths[i])
![]()
![]() 
     ![]() {
{
![]() isRight=true;
      isRight=true;
![]() break;
      break;
![]() }
     }
![]() }
    }
![]() if(!isRight)
    if(!isRight)
![]() throw new ApplicationException("用于数字签名的密钥长度必须是8,16,24值之一");
     throw new ApplicationException("用于数字签名的密钥长度必须是8,16,24值之一");
![]() else
    else
![]() this.__key=value;
     this.__key=value;
![]() }
   }
![]() }
  }
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 获取或设置用于数字签名的用户数据
  /// 获取或设置用于数字签名的用户数据
![]() /// </summary>
  /// </summary>
![]() public byte[] Data
  public byte[] Data
![]()
![]() 
  ![]() {
{
![]()
![]() get
   get![]() {return this.__data;}
{return this.__data;}
![]()
![]() set
   set![]() {this.__data=value;}
{this.__data=value;}
![]() }
  }
![]()
![]() /**//// <summary>
  /**//// <summary>
![]() /// 得到签名后的hash值
  /// 得到签名后的hash值
![]() /// </summary>
  /// </summary>
![]() /// <returns></returns>
  /// <returns></returns>
![]() public string GetHashValue()
  public string GetHashValue()
![]()
![]() 
  ![]() {
{
![]() if(this.Data==null)
   if(this.Data==null)
![]() throw new NotSetSpecialPropertyException("没有设置要进行数字签名的用户"+
    throw new NotSetSpecialPropertyException("没有设置要进行数字签名的用户"+
![]() "数据(property:Data)");
                                         "数据(property:Data)");
![]() byte[] key=Encoding.ASCII.GetBytes(this.Key);
   byte[] key=Encoding.ASCII.GetBytes(this.Key);
![]() this.mact.Key=key;
   this.mact.Key=key;
![]() byte[] hash_b=this.mact.ComputeHash(this.mact.ComputeHash(this.Data));
   byte[] hash_b=this.mact.ComputeHash(this.mact.ComputeHash(this.Data));
![]() return Encoding.ASCII.GetString(hash_b);
   return Encoding.ASCII.GetString(hash_b);
![]() }
  }
![]() }
 }
![]() }
}
![]()
![]()
![]()
 
		 
		posted @ 
2007-11-07 09:26 
吾爱乐乐 
阅读(
454) 
评论() 
 
收藏 
举报