using System;
using System.IO;
using System.Security.Cryptography;
using System.Xml;
using System.Data;
namespace Hxh.Api
{
/// <summary>
/// CryptoFile 的摘要说明。
/// </summary>
public class CryptoFile
{
private byte[] key;
private byte[] iv;
SymmetricAlgorithm symm;
public CryptoFile()
{
//
// TODO: 在此处添加构造函数逻辑
//
symm = new RijndaelManaged();
this.key=new byte[]{。。。。。};
this.iv=new byte[]{。。。};
//Get the key and IV.
}
/// <summary>
/// 读取加密文件到流
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public XmlDocument Read(string file)
{
FileStream fsIn = File.Open(file,FileMode.Open,FileAccess.Read);
XmlDocument Xdoc=new XmlDocument();
ICryptoTransform transform = symm.CreateDecryptor(key,iv);
CryptoStream cstream = new CryptoStream(fsIn,transform,CryptoStreamMode.Read);
StreamReader sr=new StreamReader(cstream,System.Text.Encoding.UTF8);
Xdoc.Load(sr);
fsIn.Close();
sr.Close();
return Xdoc;
}
public DataSet ReadXml(string file)
{
FileStream fsIn = File.Open(file,FileMode.Open,FileAccess.Read);
DataSet ds = new DataSet();
ICryptoTransform transform = symm.CreateDecryptor(key,iv);
CryptoStream cstream = new CryptoStream(fsIn,transform,CryptoStreamMode.Read);
StreamReader sr=new StreamReader(cstream,System.Text.Encoding.UTF8);
ds.ReadXml(sr,XmlReadMode.Auto);
fsIn.Close();
sr.Close();
return ds;
}
/// <summary>
/// 将流加密写入文件
/// </summary>
/// <param name="fsIn"></param>
/// <param name="fileName"></param>
public void Write(ref XmlDocument Xdoc,string fileName)
{
FileStream fsOut = File.Open(fileName, FileMode.OpenOrCreate,FileAccess.Write);
MemoryStream ms = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(ms,System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
Xdoc.Save(writer);
ms.Seek(0,SeekOrigin.Begin);
ICryptoTransform transform = symm.CreateEncryptor(key,iv);
CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write);
BinaryReader br = new BinaryReader(ms);
// 读取源文件到cryptostream
cstream.Write(br.ReadBytes((int)ms.Length),0,(int)ms.Length);
cstream.FlushFinalBlock();
cstream.Close();
ms.Close();
fsOut.Close();
}
public void WriteXml(DataSet ds,string fileName)
{
FileStream fsOut = File.Open(fileName, FileMode.OpenOrCreate,FileAccess.Write);
MemoryStream ms = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(ms,System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
ds.WriteXml(writer,XmlWriteMode.WriteSchema);
ms.Seek(0,SeekOrigin.Begin);
ICryptoTransform transform = symm.CreateEncryptor(key,iv);
CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write);
BinaryReader br = new BinaryReader(ms);
// 读取源文件到cryptostream
cstream.Write(br.ReadBytes((int)ms.Length),0,(int)ms.Length);
cstream.FlushFinalBlock();
cstream.Close();
ms.Close();
fsOut.Close();
}
/// <summary>
/// 加密一个存在的文件
/// </summary>
/// <param name="file"></param>
public void Encrypto(string file)
{
string tempfile = Path.GetTempFileName();
//打开指定的文件
FileStream fsIn = File.Open(file,FileMode.Open,
FileAccess.Read);
FileStream fsOut = File.Open(tempfile, FileMode.Open,
FileAccess.Write);
//定义对称算法对象实例和接口
ICryptoTransform transform = symm.CreateEncryptor(key,iv);
CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write);
BinaryReader br = new BinaryReader(fsIn);
// 读取源文件到cryptostream
cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length);
cstream.FlushFinalBlock();
cstream.Close();
fsIn.Close();
fsOut.Close();
File.Delete(file);
File.Move(tempfile,file);
}
/// <summary>
/// 解密一个存在的文件
/// </summary>
/// <param name="file"></param>
public void Decrypto(string file)
{
string tempfile = Path.GetTempFileName();
//打开指定的文件
FileStream fsIn = File.Open(file,FileMode.Open,FileAccess.Read);
FileStream fsOut = File.Open(tempfile, FileMode.Open,FileAccess.Write);
//定义对称算法对象实例和接口
ICryptoTransform transform = symm.CreateDecryptor(key,iv);
CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write);
BinaryReader br = new BinaryReader(fsIn);
// 读取源文件到cryptostream
cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length);
cstream.FlushFinalBlock();
cstream.Close();
fsIn.Close();
fsOut.Close();
File.Delete(file);
File.Move(tempfile,file);
}
/// <summary>
/// 设置加密或解密的初始化向量
/// </summary>
/// <param name="s">长度等于8的ASCII字符集的字符串</param>
public void SetIV(string s)
{
if(s.Length != 8)
{
this.iv =null;
return;
}
try
{
this.iv =System.Text.Encoding.Default.GetBytes(s);
}
catch(System.Exception)
{
this.iv = null;
}
}
/// <summary>
/// 设置加密或解密的密匙
/// </summary>
/// <param name="s">长度等于16的ASCII字符集的字符串</param>
public void SetKey(string s)
{
if(s.Length != 16)
{
this.key = null;
return;
}
try
{
this.key = System.Text.Encoding.Default.GetBytes(s);
}
catch(System.Exception)
{
this.key = null;
}
}
}
}