-
大小: 22KB文件類型: .zip金幣: 2下載: 1 次發(fā)布日期: 2021-05-24
- 語言: 其他
- 標簽: XML??Encryption??Decryption??
資源簡介
xml加密(XML Encryption)是w3c加密xml的標準。這個加密過程包括加密xml文檔的元素及其子元素,通過加密,xml的初始內(nèi)容將被替換,但其xml格式仍然被完好的保留。
介紹
我們有3個加密xml的方法
1、僅僅使用對稱加密的方法加密xml
這種加密方法只使用一個密鑰,也就是說無論是加密xml還是解密xml都使用一個相同的密鑰。因為這個密鑰不會在被加密的xml中保存,所以我們需要在加密和解密的過程中加載這個密鑰并保護它不被竊取。
2、使用對稱加密和非對稱加密相結合的方法來加密xml
這種方法需要一個用于加密數(shù)據(jù)的對稱密鑰和一個用于保護這個對稱密鑰的非對稱密鑰。被加密的對稱密鑰和被加密的數(shù)據(jù)一起保存在xml文檔中。當用私有非對稱密鑰解密密鑰的時候要用公開非對稱密鑰對密鑰進行加密。
本文就將使用這種方法。想學到其他更多的方法請參看MSDN等到更多的信息。
(譯者注:非對稱加密算法需要兩個密鑰:公開密鑰(publickey)和私有密鑰(privatekey)。公開密鑰與私有密鑰是一對,如果用公開密鑰對數(shù)據(jù)進行加密,只有用對應的私有密鑰才能解密;如果用私有密鑰對數(shù)據(jù)進行加密,那么只有用對應的公開密鑰才能解密。因為加密和解密使用的是兩個不同的密鑰,所以這種算法叫作非對稱加密算法。)
3、使用X.509加密xml,這種方法是用X.509作為非對稱密鑰,它由諸如VeriSign之類的第三方提供。
方法
不管xml加密是如何完成的,保存加密數(shù)據(jù)總是用兩種方法之一。
1、加密后所有的元素都被命名為
2、加密后只有數(shù)據(jù)被替換,而元素名稱仍然是可讀的,不會發(fā)生變化。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
//required?for?xmlDocument
using?System.xml;
//required?for?performing?RSA?encryption
using?System.Security.Cryptography;
//required?to?use?Encryptedxml?object?requires?the?System.Security?assembly?to?be?referenced
using?System.Security.Cryptography.xml;
/*
?*?
?*?
?*?
?*?
?*?
?*?
?*?
?*/?
namespace?xmlEncryption
{
????///?
????///?
????///?
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????///?
????????///?
????????///?RSA?asymmetric?algorithim?used?to?encrypt?the?session?key?key?is?generated?during?the?encryption?process
????????///?in?this?example?a?key?is?generated?however?in?an?actual?application?you?will?need?to?load?and?save?the?
????????///?keys?as?appropriate
????????///?
????????///?
????????RSACryptoServiceProvider?rsa;
????????///?
????????///?
????????///?Loads?the?xml?into?an?xml?document?and?encrypts?the?full?document?from?the?root
????????///?it?does?not?cover?selecting?and?encrypting?individual?nodes?in?the?document
????????///?however?the?place?where?this?code?would?be?placed?is?highlighted.
????????///?
????????///?It?also?doesn‘t?cover?loading?or?saving?the?asymmetric?keys?
????????///?the?key?is?created?in?the?encrypt?method?and?because?of?the?way?the?application?works?the?same?key?is?used?to?decrypt
????????///?a?new?key?is?generated?every?time?an?encyption?is?ran
????????///?
????????///?
????????///?
????????///?
????????private?void?encryptToolStripMenuItem_Click(object?sender?EventArgs?e)
????????{
????????????//check?there?is?xml?to?encrypt
????????????if?(string.IsNullOrEmpty(this.txtxml.Text)?==?false)
????????????{
????????????????try
????????????????{
????????????????????//1.?load?the?xml?in?to?a?document?full?document?will?be?encrypted
????????????????????xmlDocument?xmlDoc?=?new?xmlDocument();
????????????????????xmlDoc.Loadxml(this.txtxml.Text);
????????????????????//2.?Generate?an?asymmetric?key?(RSA?is?used)?rsa?object?needs?to?have?class?level
????????????????????//as?it‘s?used?to?decrypt?the?encrypted?xml?later?in?the?class
????????????????????rsa?=?new?RSACryptoServiceProvider();
????????????????????
????????????????????//3.?Select?the?element?that?will?be?encrypted?
????????????????????xmlElement?xmlElemt;
????????????????????xmlElemt?=?xmlDoc.DocumentElement;??//at?this?point?you?could?use?SelectSingleNode
????????????????????//to?select?a?specific?node?to?encrypt
????????????????????//4.?Encrypt?the?element?in?the?document?using?the?Encryptedxml?object
????????????????????//the?Encryptedxml?object?is?found?in?the?System.Security.Cryptography.xml?namespace
???
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2006-10-15?01:34??xm
?????文件????????5742??2006-10-15?05:18??xm
?????文件????????8403??2006-10-15?02:09??xm
?????文件????????6213??2006-10-15?02:09??xm
?????目錄???????????0??2006-10-15?01:34??xm
?????目錄???????????0??2006-10-15?01:34??xm
?????目錄???????????0??2006-10-15?01:42??xm
?????目錄???????????0??2006-10-15?01:34??xm
?????文件?????????842??2006-10-15?02:09??xm
?????文件???????24576??2006-10-15?02:22??xm
?????文件?????????180??2006-10-15?02:09??xm
?????文件???????26112??2006-10-15?02:22??xm
?????文件?????????180??2006-10-15?01:38??xm
?????文件?????????310??2006-10-15?02:22??xm
?????文件?????????480??2006-10-15?01:34??xm
?????目錄???????????0??2006-10-15?01:34??xm
?????文件????????1274??2006-10-15?01:34??xm
?????文件????????2853??2006-10-15?01:34??xm
?????文件????????5612??2006-10-15?01:34??xm
?????文件????????1096??2006-10-15?01:34??xm
?????文件?????????249??2006-10-15?01:34??xm
?????文件????????3278??2006-10-15?05:18??xm
?????目錄???????????0??2006-10-15?01:34??xm
評論
共有 條評論