// 要在工程里添加 System.Security 的引用集
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml; //XML Namespace
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;//xml encryption namespace
//using System.Windows.Forms; //Application namespace
namespace ConsoleApplication1
{
class mySignXML
{
//xml加密方法
private 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);
}
//xml解密方法
private bool Decrypt(XmlDocument Doc, SymmetricAlgorithm Alg)
{
try
{
XmlElement encryptedElement = Doc.GetElementsByTagName("EncryptedData")[0] as XmlElement;
EncryptedData edElement = new EncryptedData();
edElement.LoadXml(encryptedElement);
EncryptedXml exml = new EncryptedXml();
byte[] rgbOutput = exml.DecryptData(edElement, Alg);
exml.ReplaceData(encryptedElement, rgbOutput);
return true;
}
catch (Exception e)
{
return false;
}
}
//对相应xml文件 加密
public void fileEncryption()
{
RijndaelManaged key = new RijndaelManaged();
//设置密钥:key为32位=数字或字母16个=汉字8个
byte[] byteKey = Encoding.Unicode.GetBytes("BFEBFBFF000106E5");//every load get time as key
key.Key = byteKey;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load("test.xml");//加载要加密的XML文件
Encrypt(xmlDoc, "cpuid", key);//需要加密的节点
if (key != null)
{
key.Clear();
}
xmlDoc.Save("test1.xml");//生成加密后的XML文件
//MessageBox.Show("OK");
}
//对相应xml文件 解密
public void fileDcryption()
{
RijndaelManaged key = new RijndaelManaged();
//设置密钥:key为32位=数字或字母16个=汉字8个
byte[] byteKey = Encoding.Unicode.GetBytes("2222222222222222");
key.Key = byteKey;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load("test1.xml");//加载要解密的XML文件
Decrypt(xmlDoc, key);
if (key != null)
{
key.Clear();
}
xmlDoc.Save("test2.xml");//生成解密后的XML文件
// MessageBox.Show("OK");
}
}
}
MSDN上提供的XML加密方法:
MSDN关于SignXML类的解释
using System;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Xml;
namespace ConsoleApplication1
{
public class SignXML
{
public static void Main(String[] args)
{
mySignXML mysignxml = new mySignXML();
mysignxml.fileEncryption();
mysignxml.fileDcryption();
try
{
// Create a new CspParameters object to specify
// a key container.
CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = "XML_DSIG_RSA_KEY";
// Create a new RSA signing key and save it in the container.
RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);
// Create a new XML document.
XmlDocument xmlDoc = new XmlDocument();
// Load an XML file into the XmlDocument object.
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load("test.xml");
// Sign the XML document.
SignXml(xmlDoc, rsaKey);
Console.WriteLine("XML file signed.");
// Save the document.
xmlDoc.Save("test.xml");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
// Sign an XML file.
// This document cannot be verified unless the verifying
// code has the key with which it was signed.
public static void SignXml(XmlDocument Doc, RSA Key)
{
// Check arguments.
if (Doc == null)
throw new ArgumentException("Doc");
if (Key == null)
throw new ArgumentException("Key");
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(Doc);
// Add the key to the SignedXml document.
signedXml.SigningKey = Key;
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Compute the signature.
signedXml.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document.
Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature, true));
}
}
}