利用WSE 加密SOAP报文(6)

加密对外发送的报文

我已经修改了前面的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>

      </xenc:EncryptedKey>

    </wsse:Security>

  </soap:Header>

  <soap:Body xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility"

    wsu:Id="Id-70179c5b-4975-4932-9ecd-a58feb34b0d3">

    <xenc:EncryptedData

      Id="EncryptedContent-608eef8b-4104-4469-95b6-7cb4703cfa03"

      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" />

      <xenc:CipherData>

        <xenc:CipherValue>

          4o1b4befwBJu6tzuaygfrAaX0UGtaYKcw2klIbuZPjLi...z8i2ypHN4+w==

        </xenc:CipherValue>

      </xenc:CipherData>

    </xenc:EncryptedData>

  </soap:Body>

</soap:Envelope>

注意在这个已加密的报文里面,由非对称加密过的EncryptedKey元素包含了用于给报文正文加密的对称加密密钥。ReferenceList元素引用了报文正文的EncryptedData元素的Id属性。虽然我在我的例子中没这样做,标记这个消息以便能让容器验证发送者其实是个不错的想法。关于使用WSE来标记报文的详细信息,看WS-Security Authentication and Digital Signatures with Web Services Enhancements

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值