先看XML文件的加解密,例如在桌面有个Student.xml文档,包含学生信息,需进行加密处理:
开始代码,添加命名空间引用:
using System.Security.Cryptography; using System.Security.Cryptography.Xml;
建立一个静态类,并声明变量:
const string path = @"C:\Users\HKL\Desktop\Student.xml";//文件路径 static XmlDocument doc; static XMLEncypt() { doc = new XmlDocument(); }
编写创建算法函数,用以解加密:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 返回加密算法及类型 /// </summary> /// <param name="method"></param> /// <returns></returns> static SymmetricAlgorithm Create(out string method) { RijndaelManaged TDESKey = new RijndaelManaged(); TDESKey.Key = Encoding.Unicode.GetBytes("0123456789012345"); TDESKey.Mode = CipherMode.ECB; TDESKey.Padding = PaddingMode.PKCS7; switch (TDESKey.KeySize) { case 128: method = EncryptedXml.XmlEncAES128Url; break; case 256: method = EncryptedXml.XmlEncAES256Url; break; case 192: method = EncryptedXml.XmlEncAES192Url; break; default: method = EncryptedXml.XmlEncAES128Url; break; } return TDESKey; }View Code
文件加密方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static void Encrypt() 2 { 3 doc.Load(path); 4 XmlElement element = (doc.FirstChild is XmlDeclaration ? doc.FirstChild.NextSibling : doc.FirstChild) as XmlElement; 5 if (element == null || element.Name == "EncryptedData") 6 return; 7 EncryptedXml eXML = new EncryptedXml(doc); 8 string method; 9 byte[] outPut = eXML.EncryptData(element, Create(out method), false); 10 EncryptedData eData = new EncryptedData() 11 { 12 Type = EncryptedXml.XmlEncElementUrl, 13 EncryptionMethod = new EncryptionMethod(method), 14 CipherData = new CipherData() 15 { 16 CipherValue = outPut, 17 } 18 }; 19 EncryptedXml.ReplaceElement(element, eData, false); 20 doc.Save(path); 21 }Encrypt()
文件解密方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static void Decrypt() 2 { 3 doc.Load(path); 4 XmlElement element = doc.GetElementsByTagName("EncryptedData")[0] as XmlElement; 5 if (element == null) 6 return; 7 EncryptedData eData = new EncryptedData(); 8 eData.LoadXml(element); 9 EncryptedXml eXML = new EncryptedXml(); 10 string method; 11 byte[] outPut = eXML.DecryptData(eData, Create(out method)); 12 eXML.ReplaceData(element, outPut); 13 doc.Save(path); 14 }Decrypt()
*若只是读取加密文件,不对文件进行修改,则解密函数中不要执行doc.Save(path)即可;
*若要对加密文件进行修改,需先Load文件->解密内容->修改内容->加密内容->保存;
*若新增加密文件,先新增文件,再对文件进行加密。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static void Modify() { //*****************************************解密 doc.Load(path); XmlElement element = doc.GetElementsByTagName("EncryptedData")[0] as XmlElement; if (element == null) return; EncryptedData eData = new EncryptedData(); eData.LoadXml(element); EncryptedXml eXML = new EncryptedXml(); string method; byte[] outPut = eXML.DecryptData(eData, Create(out method)); eXML.ReplaceData(element, outPut); //******************************************修改 XmlNodeList nodes = doc.SelectNodes("Students/Student"); foreach (XmlNode node in nodes) { foreach (XmlAttribute attr in node.Attributes) { if (attr.Name == "Age") attr.Value = (Convert.ToSingle(attr.Value) + 1).ToString(); } } //*****************************************加密 element = (doc.FirstChild is XmlDeclaration ? doc.FirstChild.NextSibling : doc.FirstChild) as XmlElement; if (element == null || element.Name == "EncryptedData") return; eXML = new EncryptedXml(doc); outPut = eXML.EncryptData(element, Create(out method), false); eData = new EncryptedData() { Type = EncryptedXml.XmlEncElementUrl, EncryptionMethod = new EncryptionMethod(method), CipherData = new CipherData() { CipherValue = outPut, } }; EncryptedXml.ReplaceElement(element, eData, false); doc.Save(path); }Modify()
效果:
下面记录字符串文本的加解密功能,ReadStr函数将txt文件中文本读出:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
static string ReadStr() { sr = new StreamReader(path); StringBuilder sb = new StringBuilder(); while (true) { string str = sr.ReadLine(); if (str == null) break; sb.Append(str + "\r\n"); } sr.Close(); return sb.ToString(); }ReadStr()
加密解密方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static void Encrypt()//加密 2 { 3 string val = ReadStr(); 4 ICryptoTransform crypt = Create().CreateEncryptor(); 5 byte[] old = Encoding.Default.GetBytes(val); 6 byte[] cur = crypt.TransformFinalBlock(old, 0, old.Length); 7 string curStr = Convert.ToBase64String(cur); 8 sw = new StreamWriter(path, false); 9 sw.Write(curStr); 10 sw.Close(); 11 }Encrypt()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static void Decrypt()//解密 2 { 3 string val = ReadStr(); 4 ICryptoTransform crypt = Create().CreateDecryptor(); 5 byte[] old = Convert.FromBase64String(val); 6 byte[] cur = crypt.TransformFinalBlock(old, 0, old.Length); 7 string curStr = Encoding.Default.GetString(cur); 8 sw = new StreamWriter(path, false); 9 sw.Write(curStr); 10 sw.Close(); 11 }Decrypt()
标签:XML,string,C#,doc,解密,element,new,EncryptedXml,method From: https://www.cnblogs.com/cfsl/p/17035000.html