上传者: greatpass
|
上传时间: 2021-05-13 16:02:23
|
文件大小: 18.68MB
|
文件类型: ZIP
主要对XML文件进行加解密。保护重要信息(数据库连接账户和密码等配置信息)
//解密XML文件
public static XmlDocument ReadFileXML(string mFilePath)
{
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(mFilePath);
}
catch (Exception err)
{
return xmlDoc;
}
// Create a new TripleDES key.
TripleDESCryptoServiceProvider tDESkey = new TripleDESCryptoServiceProvider();
tDESkey.Key = Convert.FromBase64String(Common.customKey);
tDESkey.IV = Convert.FromBase64String(Common.customIV);
tDESkey.Mode = CipherMode.ECB;
tDESkey.Padding = PaddingMode.PKCS7;
try
{
// Encrypt the "creditcard" element.
// Common.Encrypt(xmlDoc, "Config", tDESkey);
//Console.WriteLine("Encrypted XML:");
//Console.WriteLine(xmlDoc.OuterXml);
//xmlDoc.Save(filePath);
Common.Decrypt(xmlDoc, tDESkey);
Console.WriteLine("Decrypted XML:");
Console.WriteLine(xmlDoc.OuterXml);
//xmlDoc.Save(mFilePath); //读XML文件
}
catch (Exception err)
{
//Console.WriteLine(e.Message);
}
finally
{
tDESkey.Clear();
}
return xmlDoc;
}
//加密XML
public static bool WriteFileXML(string mFilePath)
{
bool success = true;
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(mFilePath);
}
catch (Exception err)
{
success = false;
return false;
}