利用WSE 加密SOAP报文(4)
2024-07-21 02:21:40
供稿:网友
 
加密对外发送的报文
这里我简单描述下如何创建一个可以返回个被加密的xml文档的web服务。第一步先用using指示符来添加必要的命名空间,如下:
using system.web.services;
using microsoft.web.services;
using microsoft.web.services.security;
using system.security.cryptography;
using system.security.cryptography.xml;
using system.xml;
getxmldocument方法使用了的.net框架实现的三元des算法,采用128位密钥和64位初始化向量(iv),能够生成对称密钥。这个密钥还将拥有一个名字,并被添加到应答报文的soapcontext元素上,之后被securityoutputfilter使用于加密简单的xml文档,这个方法最后将返回给客户端。更多关于.net框架的加密技术,请看.net框架开发者指南上的cryptography overview一文。
//返回由三元des对称算法加密后的数据
 [webmethod (description="返回一个由对称加密算法机密后的敏感xml文档", enablesession=false)]
 
public xmldocument getxmldocument()
{
//创建一个用于返回的简单的xml文档
 xmldocument mydoc = new xmldocument();
 mydoc.innerxml = 
 "<encryptedresponse>这里是敏感数据.</encryptedresponse>";
 
 //得到对外发送的回应报文的soapcontext
 soapcontext mycontext = httpsoapcontext.responsecontext;
 
 //创建一个用于加密的对称密钥,由于密钥是对称的,这些相同的数据必须存在有需求的客户端上。
 
 //定义共享的16字节数组,用来表示128位密钥
 byte[] keybytes = {48, 218, 89, 25, 222, 209, 227, 51, 50, 168, 146, 
 188, 250, 166, 5, 206};
 
 //定义共享的8字节(64位)数组,也就是初始化向量(iv)
 byte[] ivbytes = {16, 143, 111, 77, 233, 137, 12, 72};
 
//创建三元des算法的新实例
 symmetricalgorithm mysymalg = new tripledescryptoserviceprovider();
 
//设置好密钥和iv
 mysymalg.key = keybytes;
 mysymalg.iv = ivbytes;
 
 
 //创建一个新的wse对称加密密钥
 encryptionkey mykey = new symmetricencryptionkey(mysymalg);
 
 
 //给他取个名字j
 keyinfoname mykeyname = new keyinfoname();
 mykeyname.value = "http://example.com/symmetrictestkey";
 mykey.keyinfo.addclause(mykeyname);
 
 
 //使用对称密钥来创建一个新的encrypteddata元素
 encrypteddata myencdata = new encrypteddata(mykey);
 
 
 //将encrypteddata元素添加到soap回应上,告诉过滤器用指定的密钥来加密信息正文
 
 mycontext.security.elements.add(myencdata);
 
 return mydoc;
}
基于前面的方法,wse管道产生了下面有相应的安全头信息,密文和密钥信息的回应报文:
<?xml version="1.0" encoding="utf-8"?>
<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
 xmlns:xsd="http://www.w3.org/2001/xmlschema">
 <soap:header>
 <wsu:timestamp 
 xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility">
 <wsu:created>2003-02-11t02:07:23z</wsu:created>
 <wsu:expires>2003-02-11t02:12:23z</wsu:expires>
 </wsu:timestamp>
 <wsse:security soap:mustunderstand="1" 
 xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
 <xenc:referencelist 
 xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
 <xenc:datareference uri=
 "#encryptedcontent-f50076e3-5aea-435e-8493-5d7860191411" />
 </xenc:referencelist>
 </wsse:security>
 </soap:header>
 <soap:body xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" 
 wsu:id="id-d2f22e02-a052-4dcb-8fbc-8591a45b8a9f">
 <xenc:encrypteddata 
 id="encryptedcontent-f50076e3-5aea-435e-8493-5d7860191411" 
 type="http://www.w3.org/2001/04/xmlenc#content" 
 xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
 <xenc:encryptionmethod 
 algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
 <keyinfo xmlns="http://www.w3.org/2000/09/xmldsig#">
 <keyname>http://example.com/symmetrictestkey</keyname>
 </keyinfo>
 <xenc:cipherdata>
 <xenc:ciphervalue>0t5thogg14jmelph...qdjs=</xenc:ciphervalue>
 </xenc:cipherdata>
 </xenc:encrypteddata>
 </soap:body>
</soap:envelope>
注意,在报文正文中referencelist元素包含了一个到encrypteddata元素的引用,这个元素包含了密钥的名字,使用的加密算法和一个数据的密文形式。