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);

}

}

}


本文标签: 字号 文件 加密