用对称密钥为XML文件加密和解密
xml文档如下
1 <?xml version="1.0" encoding="utf-8" ?> 2 <items> 3 <name>wjl</name> 4 <age>22</age> 5 <pwd>wjl123456</pwd> 6 </items>
加密后文档
1 <?xml version="1.0" encoding="utf-8"?> 2 <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"><EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" /><CipherData><CipherValue>qKXdV+7yyVlzPoQSxUfw/KHIVqtnRYA2STTc81tx4c/x6qYmOluD29FJ8/V2HxRTcaKFFrvhWxQILlFRwwms/o89x49i0a8hn7cjw9K+JK3dNhKYZb/SdxwXDi8r59mi</CipherValue></CipherData></EncryptedData>
在这里先写下需要的引用吧
1 using System.Security.Cryptography; 2 using System.Security.Cryptography.Xml; 3 using System.Xml; 4 using System.Text;
加密方法:
//加密方法 public void Encrypt(XmlDocument Doc, string ElementName, SymmetricAlgorithm Key) { XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementName)[0] as XmlElement; EncryptedXml eXml = new EncryptedXml(); byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, Key, false); EncryptedData edElement = new EncryptedData(); edElement.Type = EncryptedXml.XmlEncElementUrl; string encryptionMethod = null; if (Key is TripleDES) { encryptionMethod = EncryptedXml.XmlEncTripleDESUrl; } else if (Key is DES) { encryptionMethod = EncryptedXml.XmlEncDESUrl; } if (Key is Rijndael) { switch (Key.KeySize) { case 128: encryptionMethod = EncryptedXml.XmlEncAES128Url; break; case 192: encryptionMethod = EncryptedXml.XmlEncAES192Url; break; case 256: encryptionMethod = EncryptedXml.XmlEncAES256Url; break; } } edElement.EncryptionMethod = new EncryptionMethod(encryptionMethod); edElement.CipherData.CipherValue = encryptedElement; EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false); }
解密方法:
1 public void Decrypt(XmlDocument Doc, SymmetricAlgorithm Alg) 2 { 3 XmlElement encryptedElement = Doc.GetElementsByTagName("EncryptedData")[0] as XmlElement; 4 EncryptedData edElement = new EncryptedData(); 5 edElement.LoadXml(encryptedElement); 6 EncryptedXml exml = new EncryptedXml(); 7 byte[] rgbOutput = exml.DecryptData(edElement, Alg); 8 exml.ReplaceData(encryptedElement, rgbOutput); 9 }
加密按钮方法:
1 protected void Button1_Click(object sender, EventArgs e) 2 { 3 RijndaelManaged key = new RijndaelManaged(); 4 //设置密钥:key为32位=数字或字母16个=汉字8个 5 byte[] byteKey = Encoding.Unicode.GetBytes("1111111111111111"); 6 key.Key = byteKey; 7 XmlDocument xmlDoc = new XmlDocument(); 8 xmlDoc.PreserveWhitespace = true; 9 xmlDoc.Load(url+"XMLFile.xml");//加载要加密的XML文件 10 Encrypt(xmlDoc, "items", key);//需要加密的节点 11 if (key != null) 12 { 13 key.Clear(); 14 } 15 xmlDoc.Save(url+"etest.xml");//生成加密后的XML文件 16 Response.Write("<script language=javascript>alert('加密OK');</script>"); 17 18 }
解密按钮方法:
1 protected void Button2_Click(object sender, EventArgs e) 2 { 3 RijndaelManaged key = new RijndaelManaged(); 4 //设置密钥:key为32位=数字或字母16个=汉字8个 5 byte[] byteKey = Encoding.Unicode.GetBytes("1111111111111111"); 6 key.Key = byteKey; 7 XmlDocument xmlDoc = new XmlDocument(); 8 xmlDoc.PreserveWhitespace = true; 9 xmlDoc.Load(url+"etest.xml");//加载要解密的XML文件 10 Decrypt(xmlDoc, key); 11 if (key != null) 12 { 13 key.Clear(); 14 } 15 xmlDoc.Save(url+"eotest.xml");//生成解密后的XML文件 16 Response.Write("<script language=javascript>alert('解密OK');</script>"); 17 18 }
代码中存在url是我自定义一个绝对路径,否则默认会到vs的收藏夹中去xml文件,找不到会报错。
1 private string url ="E:\\asp.net\\xml加密\\";
代码贴的已经够细了,在不能使用此种方法加密和解密就罪过了!