admin 管理员组文章数量: 1087652
2024年4月19日发(作者:homily)
xml文件加密
2008-05-02 10:23:50| 分类: .Net | 标签: |字号大中小 订阅
using System;
using ;
using graphy;
using ;
namespace CSCrypto
{
classProgram
{
static void Main(string[] args)
{
RijndaelManaged key = null;
try
{
// Create a new Rijndael key.
key = new RijndaelManaged();
// Load an XML document.
XmlDocument xmlDoc = new XmlDocument();
veWhitespace = true;
("");
// Encrypt the "creditcard" element.
Encrypt(xmlDoc, "creditcard", key);
ine("The element was encrypted");
ine(ml);
Decrypt(xmlDoc, key);
ine("The element was decrypted");
ine(ml);
}
catch (Exception e)
{
ine(e);
}
finally
{
// Clear the key.
if (key != null)
{
();
}
}
();
}
public static void Encrypt(XmlDocument Doc, string ElementName,SymmetricAlgorithm Key)
{
// Check the arguments.
if (Doc == null)
throw new ArgumentNullException("Doc");
if (ElementName == null)
throw new ArgumentNullException("ElementToEncrypt");
if (Key == null)
throw new ArgumentNullException("Alg");
////////////////////////////////////////////////
// Find the specified element in the XmlDocument
// object and create a new XmlElemnt object.
////////////////////////////////////////////////
XmlElement elementToEncrypt =mentsByTagName(ElementName)[0] as XmlElement;
// Throw an XmlException if the element was not found.
if (elementToEncrypt == null)
{
throw new XmlException("The specified element was not found");
}
//////////////////////////////////////////////////
// Create a new instance of the EncryptedXml class
// and use it to encrypt the XmlElement with the
// symmetric key.
//////////////////////////////////////////////////
EncryptedXml eXml = new EncryptedXml();
byte[] encryptedElement = tData(elementToEncrypt, Key,false);
////////////////////////////////////////////////
// Construct an EncryptedData object and populate
// it with the desired encryption information.
////////////////////////////////////////////////
EncryptedData edElement = new EncryptedData();
= ElementUrl;
// Create an EncryptionMethod element so that the
// receiver knows which algorithm to use for decryption.
// Determine what kind of algorithm is being used and
// supply the appropriate URL to the EncryptionMethod element.
string encryptionMethod = null;
if (Key is TripleDES)
{
encryptionMethod = TripleDESUrl;
}
else if (Key is DES)
{
encryptionMethod = DESUrl;
}
if (Key is Rijndael)
{
switch (e)
{
case 128:
encryptionMethod = AES128Url;
break;
case 192:
encryptionMethod = AES192Url;
break;
case 256:
encryptionMethod = AES256Url;
break;
}
}
else
{
// Throw an exception if the transform is not in the previouscategories
throw new CryptographicException("The specified algorithm is notsupported for XML
Encryption.");
}
tionMethod = newEncryptionMethod(encryptionMethod);
// Add the encrypted element data to the
// EncryptedData object.
Value = encryptedElement;
////////////////////////////////////////////////////
// Replace the element from the original XmlDocument
// object with the EncryptedData element.
////////////////////////////////////////////////////
eElement(elementToEncrypt, edElement,false);
}
public static void Decrypt(XmlDocument Doc, SymmetricAlgorithmAlg)
{
// Check the arguments.
if (Doc == null)
throw new ArgumentNullException("Doc");
if (Alg == null)
throw new ArgumentNullException("Alg");
// Find the EncryptedData element in the XmlDocument.
XmlElement encryptedElement =mentsByTagName("EncryptedData")[0] as
XmlElement;
// If the EncryptedData element was not found, throw anexception.
if (encryptedElement == null)
{
throw new XmlException("The EncryptedData element was notfound.");
}
// Create an EncryptedData object and populate it.
EncryptedData edElement = new EncryptedData();
l(encryptedElement);
// Create a new EncryptedXml object.
EncryptedXml exml = new EncryptedXml();
// Decrypt the element using the symmetric key.
byte[] rgbOutput = tData(edElement, Alg);
// Replace the encryptedData element with the plaintext XMLelement.
eData(encryptedElement, rgbOutput);
}
}
}
版权声明:本文标题:xml文件加密 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1713476224a636557.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论