加密和解密:xml的加密和解密



xml加密(XML Encryption)是w3c加密xml标准这个加密过程包括加密xml文档元素及其子元素通过加密xml内容将被替换但其xml格式仍然被完好保留


介绍
我们有3个加密xml思路方法

1、仅仅使用对称加密思路方法加密xml
这种加密思路方法只使用个密钥也就是说无论是加密xml还是解密xml都使用个相同密钥这个密钥不会在被加密xml中保存所以我们需要在加密和解密过程中加载这个密钥并保护它不被窃取

2、使用对称加密和非对称加密相结合思路方法来加密xml
这种思路方法需要个用于加密数据对称密钥和个用于保护这个对称密钥非对称密钥被加密对称密钥和被加密数据起保存在xml文档中当用私有非对称密钥解密密钥时候要用公开非对称密钥对密钥进行加密

本文就将使用这种思路方法想学到其他更多思路方法请参看MSDN等到更多信息

(译者注:非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey)公开密钥和私有密钥是如果用公开密钥对数据进行加密只有用对应私有密钥才能解密;如果用私有密钥对数据进行加密那么只有用对应公开密钥才能解密加密和解密使用是两个区别密钥所以这种算法叫作非对称加密算法)

3、使用X.509加密xml这种思路方法是用X.509作为非对称密钥它由诸如VeriSign的类第 3方提供


思路方法
不管xml加密是如何完成保存加密数据总是用两种思路方法的
1、加密后所有元素都被命名为<EncryptedData>
2、加密后只有数据被替换而元素名称仍然是可读不会发生变化

这种微妙变化是非常重要例如:

如果你xml文档中包括被称为<employee>根元素该根元素有个下存储了段详细信息被称做<WrittenWarning>子元素如果你发送这个xml并且想<WrittenWarning>这个元素被保护起来那么使用第1中思路方法话<WrittenWarning>将被替换为<EncryptedData>你不会从加密后文档中获取到任何可读信息

如果使用第2种思路方法那么<WrittenWarning>元素仍然被保留只用数据会被加密任何得到这个文档人虽然不能知道该元素下详细信息但仍然知道有些事情发生在这个雇员身上另外<WrittenWarning>元素所有属性也不会被加密

所以如果没有特殊需求我们般都用第1种思路方法在.net 2.0中你可以通过修改个Boolean值属性便可以非常简单选择使用哪种思路方法

xml加密例子
下面这个xml加密例子使用是非对称加密法把xml文档author元素下内容加密并把author元素用<EncryptedData>给替换掉

xml文档
\" align=top><?xmlversion=\"1.0\"standalone=\"no\"?>
\" align=top>
<article>
\" align=top>
<articleinfo>
\" align=top>
<title>XPathQueriesonXmlDocumentobjectsin.NET1.1</title>
\" align=top>
<abstract>
\" align=top>
<para>Thisarticlecoversthebasics.</para>
\" align=top>
</abstract>
\" align=top>
<

author>
\" align=top>
<honoric>Mr.</honoric>
\" align=top>
<firstname>George</firstname>
\" align=top>
<surname>James</surname>
\" align=top>
<email>[email protected]</email>
\" align=top>
</author>
\" align=top>
</articleinfo>
\" align=top>
</article>
\" align=top>


XPath表达式为/article/articleinfo/author[Page]

被加密后xml文档
\" align=top><?xmlversion=\"1.0\"standalone=\"no\"?>
\" align=top>
<article>
\" align=top>
<articleinfo>
\" align=top>
<title>XPathQueriesonXmlDocumentobjectsin.NET1.1</title>
\" align=top>
<abstract>
\" align=top>
<para>

Thisarticlecoversthebasics.</para>
\" align=top>
<para>Thisarticledoesnotcover.</para>
\" align=top>
</abstract>
\" align=top>
<EncryptedDataType=\"http://www.w3.org/2001/04/xmlenc#Element\"
\" align=top>xmlns
=\"http://www.w3.org/2001/04/xmlenc#\">
\" align=top>
<EncryptionMethodAlgorithm=\"http://www.w3.org/2001/04/xmlenc#aes256-cbc\"/>
\" align=top>
<KeyInfoxmlns=\"http://www.w3.org/2000/09/xmldsig#\">
\" align=top>
<EncryptedKeyxmlns=\"http://www.w3.org/2001/04/xmlenc#\">
\" align=top>
<EncryptionMethodAlgorithm=\"http://www.w3.org/2001/04/xmlenc#rsa-1_5\"/>
\" align=top>
<KeyInfoxmlns=\"http://www.w3.org/2000/09/xmldsig#\">
\" align=top>
<KeyName>session</KeyName>
\" align=top>
</KeyInfo>
\" align=top>
<

CipherData>
\" align=top>
<CipherValue>r4f7SI1aZKSvibb…</CipherValue>
\" align=top>
</CipherData>
\" align=top>
</EncryptedKey>
\" align=top>
</KeyInfo>
\" align=top>
<CipherData>
\" align=top>
<CipherValue>sGNhKqcSovipJdOFCFKYEEMRFd…</CipherValue>
\" align=top>
</CipherData>
\" align=top>
</EncryptedData>
\" align=top>
</articleinfo>
\" align=top>
</article>

author元素及其子元素都将被<EncryptedData>给替换掉另外还包括其他些元素如加密算法密钥等[Page]

<EncryptedData>元素
仔细看看<EncryptedData>元素树形结构你会发现<EncryptedData>元素下分解出了很多子元素其中<KeyInfo>元素和xml数字签名中<KeyInfo>元素是相同
\"\"

EncryptedData元素被包含在“http://www.w3.org/2001/04/xmlenc#”命名空间中它是被加密数据根元素

EncryptionMethod元素指定加密数据对称思路方法做这件事需要使用个包含了w3 url算法属性 - “http://www.w3.org/2001/04/xmlenc#aes256-cbc”它指出数据是用AES(Rijndael)以256k密钥加密

KeyInfo元素来自xml数字签名它保存着对称密钥信息除此的外该元素还能保存更多信息

KeyInfo元素下EncryptedKey元素及其子元素包含着有关被保存密钥信息

KeyInfo下EncryptionMethod元素包含非对称加密思路方法用来加密对称密钥做这件事需要把个算法属性设置给w3 url例如:“http://www.w3.org/2001/04/xmlenc#rsa-1_5”介绍说明使用了RSA非对称算法来加密对称密钥

KeyName元素是个标识符用来发现密钥稍后在我们编程时候你将会发现它重要性

CipherData元素和CipherValue元素出现在EncryptedKey元素和EncryptedData元素下它们包含着密码数据事实上密码数据保存在CipherValue元素下EncryptedKey元素下保存是被加密密钥EncryptedData元素下CipherValue保存是被加密数据


非对称xml加密步骤
xml加密过程可以概括为以下 5步:
1、选择xml文档中个元素(选择根元素话将加密整个文档)
2、使用个对称密钥加密元素
3、使用非对称加密来加密上面那个对称密钥(使用公开密钥)
4、创建个EncryptedData元素该元素下将包含被加密数据和被加密密钥


5、用加密后元素替换掉元素
这些步骤大部分都可以使用.net 2.0中类自动完成


非对称xml解密步骤
xml解密过程可以概括为以下 4步:
1、在xml文档中选择个EncryptedData元素
2、使用个非对称密钥来解密密钥(使用私有密钥)
3、使用未加密密钥来解密数据
4、把EncryptedData元素替换成未加密元素
这些步骤大部分都可以使用.net 2.0中类自动完成


命名空间
完成xml加密我们需要引入 3个命名空间
.Xml - 包含操作xml
.Security.Cryptography - 包含生成加密密钥
.Security.Cryptography.Xml - 包含完成加密任务


使用.net加密xml
本文提供了个简单加密、解密xml应用下面我们起来看看相关代码这个举例只有些基本功能你可以再额外加些如选择节点的类功能

首先加载非对称公开密钥来加密密钥
\" align=top>//创建个用于加密密钥非对称密钥
\" align=top>
RSACryptoServiceProviderrsa=RSACryptoServiceProvider;
\" align=top>
//加载个公开密钥
\" align=top>
XmlDocumentpubKeys=XmlDocument;
\" align=top>pubKeys.Load(Application.StartupPath
+\"\\\\xml.dev.keys.public\");
\" align=top>
//使用公开密钥加密密钥
\" align=top>
rsa.FromXmlString(pubKeys.OuterXml);

接下来加载xml文档并选择个需要加密节点下面代码举例了如何使用个XPath表达式来选择节点如果不选择节点则整个xml文档都将被加密[Page]
\" align=top>//xml文档
\" align=top>
this.xmlEncDoc=XmlDocument;
\" align=top>
\" align=top>
//给xml文档加载些节点和数据(省略)
\" align=top>

\" align=top>XmlElementencElement;
\" align=top>
//如果没有xpath则
\" align=top>
(xpath

.Empty)
\">{
\" align=top>encElement
=this.xmlEncDoc.DocumentElement;
\" align=top>}

\" align=top>

\">{
\" align=top>XmlNamespaceManagerxmlns
=this.xmlCntrlr.xmlnsManager;
\" align=top>
//通过xpath选择出需要加密元素
\" align=top>
encElement=this.xmlEncDoc.SelectSingleNode(xpath,xmlns)asXmlElement;
\" align=top>}


使用EncryptedXml类去加密数据和密钥
\" align=top>//完成加密xml
\" align=top>
EncryptedXmlxmlEnc=EncryptedXml(this.xmlEncDoc);
\" align=top>

//增加个“session”密钥使用rsa编码
\" align=top>
xmlEnc.AddKeyNameMapping(\"session\",rsa);
\" align=top>
//使用“session”密钥来加密数据
\" align=top>
//这些信息被保存在KeyInfo元素下
\" align=top>
EncryptedDataencData=xmlEnc.Encrypt(encElement,\"session\");
\" align=top>
\" align=top>用加密后元素替换元素
\" align=top>
//用加密后元素替换元素
\" align=top>
EncryptedXml.ReplaceElement(encElement,encData,false);


使用.net解密xml
首先加载私有非对称密钥来解密密钥
\" align=top>//创建个用于解密密钥非对称密钥
\" align=top>
RSACryptoServiceProviderrsa=RSACryptoServiceProvider;
\" align=top>
//加载私有密钥
\" align=top>
XmlDocumentprivKeys=XmlDocument;
\" align=top>privKeys.Load(Application.StartupPath
+\"\\\\xml.dev.keys.private\");
\" align=top>
//使用私有密钥来解密密钥
\" align=top>
rsa.FromXmlString(privKeys.OuterXml);

增加个密钥名称并映射到被加密文档中
\" align=top>//增加个密钥名称并映射到被加密文档中
\" align=top>
EncryptedXmlencXml=

EncryptedXml(xmlEncDoc);
\" align=top>encXml.AddKeyNameMapping(
\"session\",rsa);

通过指定密钥来解密文档个EncryptedData元素
\" align=top>//解密所有<EncryptedData>元素
\" align=top>
encXml.DecryptDocument;


整理总结
xml加密(XML Encryption)是w3c加密xml标准加密后文档仍然是xml格式我们使用非对称和对称算法来加密xml对称算法用于加密数据非对称算法用于加密对称算法中密钥加密后数据被保存在EncryptedData元素下EncryptedData元素包含着些列用于描述算法子元素同时也包含着密钥信息


参考
W3C Encryption Standard


下载
[原文源码下载]


Tags:  加密与解密第三版 加密与解密 加密解密 加密和解密

延伸阅读

最新评论

发表评论