XML 加密、解密
对XML或是TXT进行加解密
#region 对文件进行加密解密
static string iv = "password";
static string key = "password";
/// <summary>
/// DES加密偏移量,必须是>=8位长的字符串
/// </summary>
public static string IV
{
get { return iv; }
set { iv = value; }
}
/// <summary>
/// DES加密的私钥,必须是8位长的字符串
/// </summary>
public static string Key
{
get { return key; }
set { key = value; }
}
/// <summary>
/// 对文件内容进行DES加密
/// </summary>
/// <param name="sourceFile">待加密的文件绝对路径</param>
/// <param name="destFile">加密后的文件保存的绝对路径</param>
public static void EncryptFile(string sourceFile, string destFile)
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);
byte[] btKey = Encoding.Default.GetBytes(key);
byte[] btIV = Encoding.Default.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] btFile = File.ReadAllBytes(sourceFile);
using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
{
try
{
using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(btFile, 0, btFile.Length);
cs.FlushFinalBlock();
}
}
catch
{
throw;
}
finally
{
fs.Close();
}
}
}
/// <summary>
/// 对文件内容进行DES加密,加密后覆盖掉原来的文件
/// </summary>
/// <param name="sourceFile">待加密的文件的绝对路径</param>
public void EncryptFile(string sourceFile)
{
EncryptFile(sourceFile, sourceFile);
}
/// <summary>
/// 对文件内容进行DES解密
/// </summary>
/// <param name="sourceFile">待解密的文件绝对路径</param>
/// <param name="destFile">解密后的文件保存的绝对路径</param>
public static void DecryptFile(string sourceFile, string destFile)
{
if (!File.Exists(sourceFile))
throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);
byte[] btKey = Encoding.Default.GetBytes(key);
byte[] btIV = Encoding.Default.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] btFile = File.ReadAllBytes(sourceFile);
using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
{
try
{
using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(btFile, 0, btFile.Length);
cs.FlushFinalBlock();
}
}
catch
{
throw;
}
finally
{
fs.Close();
}
}
}
/// <summary>
/// 对文件内容进行DES解密,解密后覆盖掉原来的文件
/// </summary>
/// <param name="sourceFile">待解密的文件的绝对路径</param>
public static void DecryptFile(string sourceFile)
{
DecryptFile(sourceFile, sourceFile);
}
#endregion
读写加密的XML或TXT
#region 读取及操作assembly(XML文件)
public static void SaveXml(string ConnenctionString, string strKey)//写入动态的数据库配置信息
{
CommonClass.DecryptFile(FilePath);
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = Application.StartupPath+"\\assembly.xml";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == strKey)
{
//对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];
att.Value = ConnenctionString;
break;
}
}
doc.Save(strFileName);
CommonClass.EncryptFile(FilePath, FilePath);
}
public static string ReadXml(string strKey)//写入动态的数据库配置信息
{
CommonClass.DecryptFile(FilePath);
string TempValues = "";
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = Application.StartupPath + "\\assembly.xml";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == strKey)
{
att = nodes[i].Attributes["value"];
TempValues = att.Value;
break;
}
}
doc.Save(strFileName);
CommonClass.EncryptFile(FilePath, FilePath);
return TempValues;
}
public static SqlConnection GetXmlConn()//写入动态的数据库配置信息
{
CommonClass.DecryptFile(FilePath);
SqlConnection Conn;
string tempDataBase = "", tempServerIP = "", tempUser = "", tempPassword = "", tempStr = "" ;
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = Application.StartupPath + "\\assembly.xml";
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == "ServerIP")
{
tempServerIP = nodes[i].Attributes["value"].Value;
}
else if (att.Value == "DataBase")
{
tempDataBase = nodes[i].Attributes["value"].Value;
}
else if (att.Value == "User")
{
tempUser = nodes[i].Attributes["value"].Value;
}
else if (att.Value == "Password")
{
tempPassword = nodes[i].Attributes["value"].Value;
}
}
doc.Save(strFileName);
tempStr = "uid=" + tempUser + ";pwd=" + tempPassword;
tempStr += ";initial catalog=" + tempDataBase + ";Server=" + tempServerIP + ";";
tempStr += "Connect Timeout=30";
Conn = new SqlConnection(tempStr);
CommonClass.EncryptFile(FilePath, FilePath);
return Conn;
}
#endregion