![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 此处定义的是DES加密
/// </summary>
public class StringSetGet
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string iv="11111111";
private string key="00000000";
private Encoding encoding=new UnicodeEncoding();
private DES des;
![](/Images/OutliningIndicators/InBlock.gif)
public StringSetGet()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
des=new DESCryptoServiceProvider();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
"propertys"#region "propertys"
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设置加密密钥
/// </summary>
public string EncryptKey
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.key;}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.key=value;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 要加密字符的编码模式
/// </summary>
public Encoding EncodingMode
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this.encoding;}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this.encoding=value;}
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
"methods"#region "methods"
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 加密字符串并返回加密后的结果
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string SetString(string str)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);//得到加密密钥
byte[] toEncrypt=this.EncodingMode.GetBytes(str);//得到要加密的内容
byte[] encrypted;
ICryptoTransform encryptor=des.CreateEncryptor(keyb,ivb);
MemoryStream msEncrypt=new MemoryStream();
CryptoStream csEncrypt=new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write);
csEncrypt.Write(toEncrypt,0,toEncrypt.Length);
csEncrypt.FlushFinalBlock();
encrypted=msEncrypt.ToArray();
csEncrypt.Close();
msEncrypt.Close();
return this.EncodingMode.GetString(encrypted);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 加密指定的文件,如果成功返回True,否则false
/// </summary>
/// <param name="filePath">要加密的文件路径</param>
/// <param name="outPath">加密后的文件输出路径</param>
// public void EncryptFile(string filePath,string outPath)
// {
// bool isExist=File.Exists(filePath);
// if(isExist)//如果存在
// {
// byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
// byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
// //得到要加密文件的字节流
// FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
// StreamReader reader=new StreamReader(fin,this.EncodingMode);
// string dataStr=reader.ReadToEnd();
// byte[] toEncrypt=this.EncodingMode.GetBytes(dataStr);
// fin.Close();
//
// FileStream fout=new FileStream(outPath,FileMode.Create,FileAccess.Write);
// ICryptoTransform encryptor=des.CreateEncryptor(keyb,ivb);
// CryptoStream csEncrypt=new CryptoStream(fout,encryptor,CryptoStreamMode.Write);
// try
// {
// //加密得到的文件字节流
// csEncrypt.Write(toEncrypt,0,toEncrypt.Length);
// csEncrypt.FlushFinalBlock();
// }
// catch(Exception err)
// {
// throw new ApplicationException(err.Message);
// }
// finally
// {
// try
// {
// fout.Close();
// csEncrypt.Close();
// }
// catch
// {
// ;
// }
// }
// }
// else
// {
// throw new FileNotFoundException("没有找到指定的文件");
// }
// }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 文件加密函数的重载版本,如果不指定输出路径,
/// 那么原来的文件将被加密后的文件覆盖
/// </summary>
/// <param name="filePath"></param>
// public void EncryptFile(string filePath)
// {
// this.EncryptFile(filePath,filePath);
// }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 解密给定的字符串
/// </summary>
/// <param name="str">要解密的字符</param>
/// <returns></returns>
public string GetString(string str)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
byte[] toDecrypt=this.EncodingMode.GetBytes(str);
byte[] deCrypted=new byte[toDecrypt.Length];
ICryptoTransform deCryptor=des.CreateDecryptor(keyb,ivb);
MemoryStream msDecrypt=new MemoryStream(toDecrypt);
CryptoStream csDecrypt=new CryptoStream(msDecrypt,deCryptor,CryptoStreamMode.Read);
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
csDecrypt.Read(deCrypted,0,deCrypted.Length);
}
catch(Exception err)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ApplicationException(err.Message);
}
finally
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
msDecrypt.Close();
csDecrypt.Close();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch
{;}
}
return this.EncodingMode.GetString(deCrypted);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 解密指定的文件
/// </summary>
/// <param name="filePath">要解密的文件路径</param>
/// <param name="outPath">解密后的文件输出路径</param>
// public void DecryptFile(string filePath,string outPath)
// {
// bool isExist=File.Exists(filePath);
// if(isExist)//如果存在
// {
// byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
// byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
// FileInfo file=new FileInfo(filePath);
// byte[] deCrypted=new byte[file.Length];
// //得到要解密文件的字节流
// FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
// //解密文件
// try
// {
// ICryptoTransform decryptor=des.CreateDecryptor(keyb,ivb);
// CryptoStream csDecrypt=new CryptoStream(fin,decryptor,CryptoStreamMode.Read);
// csDecrypt.Read(deCrypted,0,deCrypted.Length);
// }
// catch(Exception err)
// {
// throw new ApplicationException(err.Message);
// }
// finally
// {
// try
// {
// fin.Close();
// }
// catch{;}
// }
// FileStream fout=new FileStream(outPath,FileMode.Create,FileAccess.Write);
// fout.Write(deCrypted,0,deCrypted.Length);
// fout.Close();
// }
// else
// {
// throw new FileNotFoundException("指定的解密文件没有找到");
// }
// }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 解密文件的重载版本,如果没有给出解密后文件的输出路径,
/// 则解密后的文件将覆盖先前的文件
/// </summary>
/// <param name="filePath"></param>
// public void DecryptFile(string filePath)
// {
// this.DecryptFile(filePath,filePath);
// }
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)