可对 XML 元素、XML 元素内容和任意数据(包括 XML 文档)进行 XML 加密。例如,假定需要对示例 1 中显示的 CreditCard 元素进行加密。
示例 1:样本 XML 文档
<PaymentInfo xmlns='http://example.org/paymentv2'> <Name>John Smith</Name> <CreditCard Limit='5,000' Currency='USD'> <Number>4019 2445 0277 5567</Number> <Issuer>Example Bank</Issuer> <Expiration>04/02</Expiration> </CreditCard> </PaymentInfo>
示例 2:带公用密钥的 XML 文档
示例 2 显示加密后的 XML 文档。EncryptedData 元素表示加密的 CreditCard 元素。EncryptionMethod 元素描述可用的加密算法,此示例中为三重 DES。KeyInfo 元素包含要检索解密密钥的信息,此示例中为 KeyName 元素。CipherValue 元素包含通过序列化和加密 CreditCard 元素获得的密文。
<PaymentInfo xmlns='http://example.org/paymentv2'> <Name>John Smith</Name> <EncryptedData Type='http://www.w3.org/2001/04/xmlenc#Element' xmlns='http://www.w3.org/2001/04/xmlenc#'> <EncryptionMethod Algorithm='http://www.w3.org/2001/04/xmlenc#tripledes-cbc'/> <KeyInfo xmlns='http://www.w3.org/2000/09/xmldsig#'> <KeyName>John Smith</KeyName> </KeyInfo> <CipherData> <CipherValue>ydUNqHkMrD...</CipherValue> </CipherData> </EncryptedData> </PaymentInfo>
示例 3:用接收方的公用密钥加密的 XML 文档
在示例 2 中,假设发送方和接收方都有公用密钥。类似于此示例,如果接收方具有公用和专用密钥对,则可以如示例 3 中所示加密 CreditCard 元素。EncryptedData 元素与示例 2 中找到的 EncryptedData 元素相同。但是 KeyInfo 元素包含 EncryptedKey。
<PaymentInfo xmlns='http://example.org/paymentv2'> <Name>John Smith</Name> <EncryptedData Type='http://www.w3.org/2001/04/xmlenc#Element' xmlns='http://www.w3.org/2001/04/xmlenc#'> <EncryptionMethod Algorithm='http://www.w3.org/2001/04/xmlenc#tripledes-cbc'/> <KeyInfo xmlns='http://www.w3.org/2000/09/xmldsig#'> <EncryptedKey xmlns='http://www.w3.org/2001/04/xmlenc#'> <EncryptionMethod Algorithm='http://www.w3.org/2001/04/xmlenc#rsa-1_5'/> <KeyInfo xmlns='http://www.w3.org/2000/09/xmldsig#'> <KeyName>Sally Doe</KeyName> </KeyInfo> <CipherData> <CipherValue>yMTEyOTA1M...</CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue>ydUNqHkMrD...</CipherValue> </CipherData> </EncryptedData> </PaymentInfo>
以 WSS-Core 加密 XML
WSS-Core 规范是由 Organization for the Advancement of Structured Information Standards(OASIS)制定的。此规范描述了对简单对象访问协议(SOAP)消息传递的增强,通过消息完整性、消息机密性和单个消息认证来提供保护质量。消息机密性由基于 XML 加密的加密实现。
WSS-Core 规范支持对主体块、头块、其子结构,以及 SOAP 消息附件的任何组合进行加密。此规范还需要您在加密部分 SOAP 消息时,将引用从安全头块添加到已加密的消息部件之前。该引用可以是供接收方识别消息的哪些加密部件要解密的线索。
引用的 XML 语法因所加密信息以及加密方式的不同而不同。例如,假设使用常见密钥或接收方的公用密钥加密示例4 中的 CreditCard 元素。
示例 4:样本 SOAP 消息
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'> <SOAP-ENV:Body> <PaymentInfo xmlns='http://example.org/paymentv2'> <Name>John Smith</Name> <CreditCard Limit='5,000' Currency='USD'> <Number>4019 2445 0277 5567</Number> <Issuer>Example Bank</Issuer> <Expiration>04/02</Expiration> </CreditCard> </PaymentInfo> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
示例 5 和 6 中显示产生的 SOAP 消息。在这些示例中,ReferenceList 和 EncryptedKey 元素分别用作引用。
示例 5:用公用密钥加密的 SOAP 消息
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'> <SOAP-ENV:Header> <Security SOAP-ENV:mustUnderstand='1' xmlns='http://schemas.xmlsoap.org/ws/2003/06/secext'> <ReferenceList xmlns='http://www.w3.org/2001/04/xmlenc#'> <DataReference URI='#ed1'/> </ReferenceList> </Security> </SOAP-ENV:Header> <SOAP-ENV:Body> <PaymentInfo xmlns='http://example.org/paymentv2'> <Name>John Smith</Name> <EncryptedData Id='ed1' Type='http://www.w3.org/2001/04/xmlenc#Element' xmlns='http://www.w3.org/2001/04/xmlenc#'> <EncryptionMethod Algorithm='http://www.w3.org/2001/04/xmlenc#tripledes-cbc'/> <KeyInfo xmlns='http://www.w3.org/2000/09/xmldsig#'> <KeyName>John Smith</KeyName> </KeyInfo> <CipherData> <CipherValue>ydUNqHkMrD...</CipherValue> </CipherData> </EncryptedData> </PaymentInfo> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
示例 6:使用接收方的公用密钥加密 SOAP 消息
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'> <SOAP-ENV:Header> <Security SOAP-ENV:mustUnderstand='1' xmlns='http://schemas.xmlsoap.org/ws/2003/06/secext'> <EncryptedKey xmlns='http://www.w3.org/2001/04/xmlenc#'> <EncryptionMethod Algorithm='http://www.w3.org/2001/04/xmlenc#rsa-1_5'/> <KeyInfo xmlns='http://www.w3.org/2000/09/xmldsig#'> <KeyName>Sally Doe</KeyName> </KeyInfo> <CipherData> <CipherValue>yMTEyOTA1M...</CipherValue> </CipherData> <ReferenceList> <DataReference URI='#ed1'/> </ReferenceList> </EncryptedKey> </Security> </SOAP-ENV:Header> <SOAP-ENV:Body> <PaymentInfo xmlns='http://example.org/paymentv2'> <Name>John Smith</Name> <EncryptedData Id='ed1' Type='http://www.w3.org/2001/04/xmlenc#Element' xmlns='http://www.w3.org/2001/04/xmlenc#'> <EncryptionMethod Algorithm='http://www.w3.org/2001/04/xmlenc#tripledes-cbc'/> <CipherData> <CipherValue>ydUNqHkMrD...</CipherValue> </CipherData> </EncryptedData> </PaymentInfo> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
与数字签名的关系
WSS-Core 规范还提供消息完整性,这根据 XML 签名规范,通过数字签名来实现。
公共数据的加密和数字签名的组合会暴露加密弱点。