日期:2012-09-18  浏览次数:20501 次

加密对外发送的报文

我已经修改了前面的GetXmlDocument方法,让它可以使用由WSE实现的基于X.509非对称加密技术。加密回应报文,FindCertificateBySubjectString方法可以用来接收客户端证书的公开备份,一个来自本地机器账号的个人储存室给的客户端证书。这个证书然后被用来创建一个新的 X.509安全Token,这个Token将被加入到响应报文的SoapContext的安全Token集合里。另外,在对称加密例子中引用的命名空间,你应该再加一个using指示附来引用一个Microsoft.WebServices.Security.X509命名空间。GetXmlDocument方法代码如下:

//创建一个用于返回的简单XML文档

XmlDocument myDoc = new XmlDocument();

myDoc.InnerXml =

"<EncryptedResponse>This is sensitive data.</EncryptedResponse>";

"<EncryptedResponse>这里是敏感数据.</EncryptedResponse>";



//得到响应报文的SoapContext

SoapContext myContext = HttpSoapContext.ResponseContext;



//打开并读取本地机器帐号的个人证书储存室

X509CertificateStore myStore =

X509CertificateStore.LocalMachineStore(

X509CertificateStore.MyStore);

myStore.OpenRead();



//查找所有名为”我的证书”的证书,然后将所有匹配的证书添加到证书集合中

X509CertificateCollection myCerts =

myStore.FindCertificateBySubjectString("My Certificate");

X509Certificate myCert = null;



//查找在集合中中的第一个证书

if (myCerts.Count > 0)

{

myCert = myCerts[0];

}



//确定我们有一个可以用于加密的证书

if (myCert == null || !myCert.SupportsDataEncryption)

{

throw new ApplicationException("Service is not able to

encrypt the response");



return null;

}

else

{

//使用有效的证书来创建一个安全Token

X509SecurityToken myToken = new X509SecurityToken(myCert);

//WSE将使用这个标记来加密报文正文的

//WSE产生一个KeyInfo元素,用来请求客户端上曾用于给报文解密的证书



EncryptedData myEncData = new EncryptedData(myToken);

//将已加密数据元素添加到响应报文的SoapContext上

myContext.Security.Elements.Add(myEncData);



return myDoc;

}

基于前面的方法,WSE管道产生了下面的有相应Security头、密文和密钥信息的元素:

<?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-11T01:34:01Z</wsu:Created>

<wsu:Expires>2003-02-11T01:39:01Z</wsu:Expires>

</wsu:Timestamp>

<wsse:Security soap:mustUnderstand="1"

xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">

<xenc:EncryptedKey

Type="http://www.w3.org/2001/04/xmlenc#EncryptedKey"

xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">

<xenc:EncryptionMethod

Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />

<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">

<wsse:SecurityTokenReference>

<wsse:KeyIdentifier ValueType="wsse:X509v3">

YmlKVwXYD8vuGuYliuIYdEAQQPw=

</wsse:KeyIdentifier>

</wsse:SecurityTokenReference>

</KeyInfo>

<xenc:CipherData>

<xenc:CipherValue>UJ64Addf3Fd59XsaQ=…</xenc:CipherValue>

</xenc:CipherData>

<xenc:ReferenceList>

<xenc:DataReference URI=

"#EncryptedContent-608eef8b-4104-4469-95b6-7cb4703cfa03" />

</xenc:ReferenceList&g