龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

使用XML-Encryption实现安全SOAP消息

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
自1977年以来,使用最为广泛的加密算法是数据加密标准(Data Encryption Standard,DES)。但是事实表明,由于近几年计算能力的极大提高,DES可以在一天之内被攻破。所以2001年,联邦政府

  自1977年以来,使用最为广泛的加密算法是数据加密标准(Data Encryption Standard,DES)。但是事实表明,由于近几年计算能力的极大提高,DES可以在一天之内被攻破。所以2001年,联邦政府引入了一个新的标准:高级加密标准(Advanced Encryption Standard,AES)。DES和AES使用的都是对称密钥加密算法。顾名思义,其加密和解密是使用同一个密码块进行的。在一个客户-服务器环境中,如何创建、分发用于加密和解密消息的密钥并对其达成协议是首要的问题。

  XML-Encryption指定EncryptedKey机制,使用RSA――一种公钥加密体制――来加密密钥。我们需要记住,在非对称加密中,我们使用公钥来加密,而使用私钥来解密。所以,密钥由消息发送方创建,并使用接收方的公钥进行加密。然后用于加密的密钥会包含在消息中。接收方根据KeyInfo元素得出解密密钥(私钥)。

  让我们来具体看一下由WebLogic Server 9.0所生成的示例消息:
  
  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header>
      <wsse:Security
        xmlns:wsse="…/oasis-200401-wss-wssecurity-secext-1.0.xsd"
        soapenv:mustUnderstand="1">
         <ns1:EncryptedKey Id="encrypt"
             xmlns:ns1="http://www.w3.org/2001/04/xmlenc#">
         <ns1:EncryptionMethod Algorithm="…#rsa-1_5"/>
         <ns2:KeyInfo xmlns:ns2="…/xmldsig#">
           <wsse:SecurityTokenReference wsu:Id="Dk8Xm"
               xmlns:wsu="…/oasis-200401-wss-wssecurity-utility-1.0.xsd"
               xmlns:wsse="…/oasis-200401-wss-wssecurity-secext1.0.xsd">
               <ns2:X509Data>
                  <ns2:X509IssuerSerial>
                     <ns2:X509IssuerName>CN=CertGenCAB…C=US</ns2:X509IssuerName>
                     <ns2:X509SerialNumber>-</ns2:X509SerialNumber>
                  </ns2:X509IssuerSerial>
               </ns2:X509Data>
            </wsse:SecurityTokenReference>
         </ns2:KeyInfo>
         <ns1:CipherData>
              <ns1:CipherValue>aSMjPEHitl/2doflGwDQ==</ns1:CipherValue>
         </ns1:CipherData>
         <ns1:ReferenceList>
             <ns1:DataReference URI="#Dk8Zw31"/>
         </ns1:ReferenceList>
       </ns1:EncryptedKey>
      </wsse:Security>
     </soapenv:Header>
     <soapenv:Body>
        <ns1:EncryptedData Id=" Dk8Zw31" Type="…#Content"
           MimeType="text/xml" Encoding="UTF-8" xmlns:ns1="…/xmlenc#">
           <ns1:EncryptionMethod Algorithm="…#tripledescbc"/>
           <ns1:CipherData>
              <ns1:CipherValue>2HIkjvUdSL9qpqhP</ns1:CipherValue>
           </ns1:CipherData>
         </ns1:EncryptedData>
     </soapenv:Body>
  </soapenv:Envelope>
  
  SecurityTokenReference元素指定接收方的X509证书的X509IssuerSerial。接收方应该能够从密钥库查找相应的私钥来执行解密操作。除了X509IssuerSerial,X509证书的Subject Key Identifier (SKID)也可以用来描述用于加密的密钥。
  
  <wsse:SecurityTokenReference>
    <wsse:KeyIdentifier
      ValueType="...#X509SubjectKeyIdentifier">
        Xeg55vRyK3ZhAEhEf+YT0z986L0=
    </wsse:KeyIdentifier>
  </wsse:SecurityTokenReference>
  
  假如X509证书中包含SKID,WebLogic Server 9.0就总会在SecurityTokenReference中生成KeyIdentifier。而假如X509证书中没有SKID,那么就会在SecurityTokenReference中使用X509IssuerSerial。

  
  

精彩图集

赞助商链接