.NET加密技术应用

None.gif
None.gif
using System;
None.gif
using System.Text;
None.gif
using System.Security;
None.gif
using System.Security.Cryptography;
None.gif
using System.IO;
None.gif
namespace EncryptClasses
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary>
InBlock.gif 
/// 此处定义的是DES加密,为了便于今后的管理和维护
InBlock.gif 
/// 请不要随便改动密码,或者改变了密码后请一定要
InBlock.gif 
/// 牢记先前的密码,否则将会照成不可预料的损失
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class DESEncrypt
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif  
"member fields"#region "member fields"
InBlock.gif  
private string iv="12345678";
InBlock.gif  
private string key="12345678";
InBlock.gif  
private Encoding encoding=new UnicodeEncoding();
InBlock.gif  
private DES des;
ExpandedSubBlockEnd.gif  
#endregion

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 构造函数
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  public DESEncrypt()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   des
=new DESCryptoServiceProvider();
ExpandedSubBlockEnd.gif  }

ContractedSubBlock.gifExpandedSubBlockStart.gif  
"propertys"#region "propertys"
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 设置加密密钥
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  public string EncryptKey
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
getdot.gif{return this.key;}
InBlock.gif   
set
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
this.key=value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 要加密字符的编码模式
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  public Encoding EncodingMode
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
getdot.gif{return this.encoding;}
ExpandedSubBlockStart.gifContractedSubBlock.gif   
setdot.gif{this.encoding=value;}
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif  
"methods"#region "methods"
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 加密字符串并返回加密后的结果
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="str"></param>
ExpandedSubBlockEnd.gif  
/// <returns></returns>

InBlock.gif  public string EncryptString(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
InBlock.gif   
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);//得到加密密钥
InBlock.gif
   byte[] toEncrypt=this.EncodingMode.GetBytes(str);//得到要加密的内容
InBlock.gif
   byte[] encrypted;
InBlock.gif   ICryptoTransform encryptor
=des.CreateEncryptor(keyb,ivb);
InBlock.gif   MemoryStream msEncrypt
=new MemoryStream();
InBlock.gif   CryptoStream csEncrypt
=new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write);
InBlock.gif   csEncrypt.Write(toEncrypt,
0,toEncrypt.Length);
InBlock.gif   csEncrypt.FlushFinalBlock();
InBlock.gif   encrypted
=msEncrypt.ToArray();
InBlock.gif   csEncrypt.Close();
InBlock.gif   msEncrypt.Close();
InBlock.gif   
return this.EncodingMode.GetString(encrypted);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 加密指定的文件,如果成功返回True,否则false
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="filePath">要加密的文件路径</param>
ExpandedSubBlockEnd.gif  
/// <param name="outPath">加密后的文件输出路径</param>

InBlock.gif  public void EncryptFile(string filePath,string outPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
bool isExist=File.Exists(filePath);
InBlock.gif   
if(isExist)//如果存在
ExpandedSubBlockStart.gifContractedSubBlock.gif
   dot.gif{
InBlock.gif    
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
InBlock.gif    
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
InBlock.gif    
//得到要加密文件的字节流
InBlock.gif
    FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
InBlock.gif    StreamReader reader
=new StreamReader(fin,this.EncodingMode);
InBlock.gif    
string dataStr=reader.ReadToEnd();
InBlock.gif    
byte[] toEncrypt=this.EncodingMode.GetBytes(dataStr);
InBlock.gif    fin.Close();
InBlock.gif
InBlock.gif    FileStream fout
=new FileStream(outPath,FileMode.Create,FileAccess.Write);
InBlock.gif    ICryptoTransform encryptor
=des.CreateEncryptor(keyb,ivb);
InBlock.gif    CryptoStream csEncrypt
=