加密类

加密类操作[@more@]

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

///
/// 此处定义的是DES加密,为了便于今后的管理和维护
/// 请不要随便改动密码,或者改变了密码后请一定要
/// 牢记先前的密码,否则将会照成不可预料的损失
///
public class Encrypt
{
#region "member fields"
private string iv="12345678"; //偏移量8位
private string key="12345678"; //密匙8位
private Encoding encoding=new UnicodeEncoding();
private DES des;
#endregion

///
/// 构造函数
///
public Encrypt()
{
des=new DESCryptoServiceProvider();
}
#region "propertys"
///
/// 设置加密密钥
///
public string EncryptKey
{
get{return this.key;}
set{this.key=value;}
}
///
/// 要加密字符的编码模式
///
public Encoding EncodingMode
{
get{return this.encoding;}
set{this.encoding=value;}
}
#endregion

#region "methods"
///
/// 加密字符串并返回加密后的结果
///
/// 返回加密后密码
public string EncryptString(string str)
{
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);
}
///
/// 加密指定的文件,如果成功返回True,否则false
///
/// 要加密的文件路径
/// 加密后的文件输出路径
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("没有找到指定的文件");
}
}
///
/// 文件加密函数的重载版本,如果不指定输出路径,
/// 那么原来的文件将被加密后的文件覆盖
///
///
public void EncryptFile(string filePath)
{
this.EncryptFile(filePath,filePath);
}
///
/// 解密给定的字符串
///
/// 要解密的字符
///
public string DecryptString(string str)
{
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
{
csDecrypt.Read(deCrypted,0,deCrypted.Length);
}
catch(Exception err)
{
throw new ApplicationException(err.Message);
}
finally
{
try
{
msDecrypt.Close();
csDecrypt.Close();
}
catch{;}
}
return this.EncodingMode.GetString(deCrypted);
}
///
/// 解密指定的文件
///
/// 要解密的文件路径
/// 解密后的文件输出路径
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("指定的解密文件没有找到");
}
}
///
/// 解密文件的重载版本,如果没有给出解密后文件的输出路径,
/// 则解密后的文件将覆盖先前的文件
///
///
public void DecryptFile(string filePath)
{
this.DecryptFile(filePath,filePath);
}
#endregion
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7436820/viewspace-926423/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7436820/viewspace-926423/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值