# XML 加密

XML 加密是万维网联盟制定的一项标准，规定了对数据进行加密并将结果用 XML 表示的过程，其中数据可以是任意数据（包括整个 XML 文档）、一个 XML 元素或 XML 元素内容；结果是包含或引用密文数据的 `EncryptedData` 元素<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup>。原始规范于 2002 年 12 月 10 日成为 W3C 推荐标准，修订版 1.1 于 2013 年 4 月 11 日成为推荐标准<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup><sup> • </sup><sup>[2](https://www.w3.org/standards/history/xmlenc-core1/)</sup>。它与 XML 签名配合使用，使用户能够选择性地对 XML 数据的部分内容进行签名和加密，而不必仅保护整个数据对象或通信会话<sup>[3](https://www.w3.org/press-releases/2002/xenc/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 标准 | XML 加密语法与处理，W3C 推荐标准 2002 年 12 月 10 日；版本 1.1 推荐标准 2013 年 4 月 11 日<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup><sup> • </sup><sup>[2](https://www.w3.org/standards/history/xmlenc-core1/)</sup> |
| 命名空间 | `http://www.w3.org/2001/04/xmlenc#`，该命名空间也用作规范中算法标识符的前缀<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup> |
| 粒度 | 对 XML 文档的部分或全部进行加密，限于一个元素（包括起始和结束标签）或元素内容<sup>[4](https://www.w3.org/TR/xml-encryption-req/)</sup> |
| 核心结构 | `EncryptedData` 包含 `EncryptionMethod`、可选的 `ds:KeyInfo`、`CipherData`（封装 `CipherValue` 或通过 `CipherReference` 引用），以及可选的 `EncryptionProperties`<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup> |
| 密钥传输 | `EncryptedKey` 携带为接收方加密的密钥值，通常使用非对称算法如 RSA<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup> |
| 1.1 算法变更 | 新增椭圆曲线 Diffie-Hellman 密钥协商，将 AES-128 GCM 设为强制，将 RSA v1.5 改为可选，并新增可选的 AES-192 GCM 和 RSA-OAEP 变体<sup>[5](https://www.w3.org/TR/2013/REC-xmlenc-core1-20130411/Overview.src.html)</sup> |
| 主要部署 | WS-Security 将 `xenc:EncryptedData` 或 `xenc:EncryptedKey` 元素放入 SOAP `Security` 头块中<sup>[6](https://schemas.xmlsoap.org/specs/ws-security/ws-security.htm)</sup> |

## 标准涵盖的内容

该规范同时定义了语法和处理方式：如何序列化和加密数据、如何将结果表示为 XML，以及接收方如何定位并应用解密密钥。当加密一个 XML 元素或元素内容时，`EncryptedData` 元素在文档的加密版本中替换该元素或内容；当加密任意数据或整个文档时，`EncryptedData` 可以成为新文档的根元素，或成为应用程序所选文档的子元素<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup>。

`CipherData` 元素封装或引用原始加密数据。在封装形式中，密文是 `CipherValue` 元素的 base64 编码内容；在引用形式中，`CipherReference` URI 指向加密八位组所在的位置<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup>。`EncryptedKey` 元素在结构上与 `EncryptedData` 相似，但始终加密一个密钥值，其 `ReferenceList` 标识用它所携带的密钥加密的数据和密钥对象<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup>。

## 加密粒度

需求文档将粒度限制为一个元素或元素内容<sup>[4](https://www.w3.org/TR/xml-encryption-req/)</sup>，规范的示例通过一个包含 `CreditCard` 元素的支付文档展示了实际差异<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup>：

- **加密一个元素。** 加密整个 `CreditCard` 元素（从起始标签到结束标签）会隐藏元素本身的身份，因此窃听者无法判断使用的是信用卡还是转账<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup>。
- **加密元素内容。** 仅加密内容会使 `CreditCard` 元素及其属性（如额度和货币）仍对中间方可见，同时隐藏卡号、发卡机构和有效期<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup>。
- **加密字符数据。** 更细粒度地，可以加密 `Number` 元素内的字符数据，而元素名称及其同级元素保持明文<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup>。
- **加密任意数据。** 整个文档或任何非 XML 数据都作为八位组序列加密<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup>。

**超级加密。** 一个文档可以包含零个或多个 `EncryptedData` 元素，且一个 `EncryptedData` 元素不能是另一个 `EncryptedData` 元素的父元素或子元素。已加密的数据本身可以被再次加密，但在超级加密过程中，必须加密整个 `EncryptedData` 或 `EncryptedKey` 元素；仅加密其内容或选定的子元素在模式（schema）下是无效实例<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup>。

## 算法与 1.1 修订版

算法标识符以 URI 形式出现在 `EncryptionMethod` 元素的 `Algorithm` 属性中<sup>[7](https://www.w3.org/TR/xmlenc-core1/)</sup>。2002 年推荐标准的示例使用三重 DES CBC 和 AES-128-CBC 进行数据加密，使用 RSA 进行密钥传输<sup>[1](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)</sup>。

由 XML 安全工作组开发的版本 1.1 改变了强制实现算法集：新增椭圆曲线 Diffie-Hellman 密钥协商，将 GCM 模式下的 AES-128 设为强制，将 RSA v1.5 从强制改为可选，并新增可选的 AES-192 GCM 和 RSA-OAEP 算法变体<sup>[5](https://www.w3.org/TR/2013/REC-xmlenc-core1-20130411/Overview.src.html)</sup>。该修订还扩展了数据模型，指出加密数据可以是八位组流和其他非结构化数据，也可以是 XML 文档、元素和元素内容等结构化格式<sup>[8](https://www.w3.org/news/2013/three-xml-security-working-group-recommendations-and-thirteen-notes/)</sup>。原始版本的互操作性报告显示，有四个实现，且每个特性至少有两个可互操作的实现<sup>[5](https://www.w3.org/TR/2013/REC-xmlenc-core1-20130411/Overview.src.html)</sup>。

## 与 XML 签名的关系

XML 加密与“XML 签名解密变换”一同发布，后者也是 2002 年 12 月 10 日的推荐标准。该变换使 XML 签名应用能够区分签名之前加密的 XML 加密结构（不得解密）和签名之后加密的结构（必须解密才能使签名验证通过）<sup>[9](https://www.w3.org/groups/wg/xmlsec/publications/)</sup>。万维网联盟宣布这两项标准的新闻稿强调了它们的组合：已部署的技术可以保护完整的数据对象或会话，但只有 XML 签名与 XML 加密结合，才能实现对 XML 部分内容的选择性签名和加密，例如在 XForms 支付中加密信用卡号，同时对支付授权进行签名<sup>[3](https://www.w3.org/press-releases/2002/xenc/)</sup>。

## 在 Web 服务中的部署

XML 加密是基于 SOAP 的 Web 服务中消息级安全的基础。WS-Security 指示实现根据加密类型在 SOAP `Security` 头块中创建 `xenc:ReferenceList`、`xenc:EncryptedKey` 或 `xenc:EncryptedData` 子元素，并按照 XML 加密处理规则对每个目标 XML 元素或元素内容进行加密<sup>[6](https://schemas.xmlsoap.org/specs/ws-security/ws-security.htm)</sup>。在典型配置中，消息数据使用对称密码如 AES 加密，而会话密钥使用 RSA 非对称算法加密<sup>[10](https://docs.oracle.com/cd/E24191_01/common/tutorials/encryption_encrypt_settings.html)</sup>。该标准还构成了其他 Web 服务安全协议的基础，包括 SAML 和 WS-Federation<sup>[11](https://www.net.in.tum.de/fileadmin/TUM/teaching/netzsicherheit/ws1213/10_ApplicationLayerSecurity.pdf)</sup>。

## 已知攻击

XML 加密实现中广泛使用的 CBC 块加密模式被证明容易受到填充预言攻击，攻击者通过观察解密是否成功来逐位恢复明文；于尔根·索莫罗夫斯基在其波鸿鲁尔大学的博士论文中分析了 XML 加密框架中的解密机制，并开发了一种利用 XML 解析机制作为额外侧信道的实用攻击<sup>[12](https://www.nds.rub.de/media/nds/veroeffentlichungen/2013/11/19/diss_somorovsky.pdf)</sup>。Duong 和 Rizzo 表明，填充预言不仅可以解密密文，还可以获得任意明文的有效加密<sup>[12](https://www.nds.rub.de/media/nds/veroeffentlichungen/2013/11/19/diss_somorovsky.pdf)</sup>。作为回应，1.1 规范要求实现者支持认证模式 AES-128 GCM，并建议在采用 CBC 块加密时权衡额外的防护措施，例如通过 TLS 等安全信道传输加密数据。<sup>[5](https://www.w3.org/TR/2013/REC-xmlenc-core1-20130411/Overview.src.html)</sup><sup> • </sup><sup>[13](https://www.w3.org/TR/2013/PR-xmlenc-core1-20130124/)</sup>

## 状态

交付 XML 加密 1.1 的 XML 安全工作组已于 2017 年 1 月 4 日关闭<sup>[9](https://www.w3.org/groups/wg/xmlsec/publications/)</sup>。1.1 推荐标准仍是该标准的当前版本，ITU-T 推荐标准 Y.4500.3 将万维网联盟 XMLENC（2013）列为规范性引用<sup>[14](https://www.itu.int/ITU-T/workprog/wp_a5_out.aspx?isn=7956)</sup>。

## References

1. [XML Encryption Syntax and Processing, W3C Recommendation 10 December 2002](https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/)
2. [XML Encryption Syntax and Processing Version 1.1 publication history, W3C](https://www.w3.org/standards/history/xmlenc-core1/)
3. [World Wide Web Consortium Issues XML Encryption and Decryption Transform as W3C Recommendations, W3C press release, 10 December 2002](https://www.w3.org/press-releases/2002/xenc/)
4. [XML Encryption Requirements, W3C Note 4 March 2002](https://www.w3.org/TR/xml-encryption-req/)
5. [XML Encryption Syntax and Processing Version 1.1, W3C Recommendation 11 April 2013](https://www.w3.org/TR/2013/REC-xmlenc-core1-20130411/Overview.src.html)
6. [WS-Security specification, xmlsoap.org](https://schemas.xmlsoap.org/specs/ws-security/ws-security.htm)
7. [XML Encryption Syntax and Processing Version 1.1, W3C (current Recension)](https://www.w3.org/TR/xmlenc-core1/)
8. [XML Signature and XML Encryption are W3C Recommendations, W3C news, 16 April 2013](https://www.w3.org/news/2013/three-xml-security-working-group-recommendations-and-thirteen-notes/)
9. [XML Security Working Group publications, W3C](https://www.w3.org/groups/wg/xmlsec/publications/)
10. [XML-Encryption Settings, Oracle documentation](https://docs.oracle.com/cd/E24191_01/common/tutorials/encryption_encrypt_settings.html)
11. [Application Layer Security, TU München lecture notes, WS 2012/13](https://www.net.in.tum.de/fileadmin/TUM/teaching/netzsicherheit/ws1213/10_ApplicationLayerSecurity.pdf)
12. [Jürgen Somorovsky, "How to Break XML Encryption", dissertation, Ruhr University Bochum, 2013](https://www.nds.rub.de/media/nds/veroeffentlichungen/2013/11/19/diss_somorovsky.pdf)
13. [XML Encryption Syntax and Processing Version 1.1, Proposed Recommendation, 24 January 2013](https://www.w3.org/TR/2013/PR-xmlenc-core1-20130124/)
14. [ITU-T A.5 reference justification for W3C XMLENC (2013) in draft Y.4500.3](https://www.itu.int/ITU-T/workprog/wp_a5_out.aspx?isn=7956)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Database security, privacy, and law › Database encryption and cryptographic protection

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/xml-encryption

English original: https://www.edgepedia.org/xml-encryption
