跳到主要内容

5. Producing and Consuming JWEs (生成和消费 JWE)

5.1 Message Encryption (消息加密)

生成 JWE 时, producer 先根据 alg 确定 Key Management Mode. 对 Key Wrapping, Key Encryption 或 Key Agreement with Key Wrapping, producer 生成随机 CEK, 并把 CEK 加密或包裹给 recipient. 对 Direct Key Agreement 或 Direct Encryption, JWE Encrypted Key 为空 octet sequence.

随后 producer 生成所需 IV, 可按 zip 压缩 plaintext, 构造 JWE Protected/Unprotected Header, 并计算 Encoded Protected Header. AAD 通常为 ASCII(Encoded Protected Header). 如果 JWE JSON Serialization 使用额外 aad, AAD 为 ASCII(Encoded Protected Header || '.' || BASE64URL(JWE AAD)).

最后 producer 用 enc 指定的 content encryption algorithm, CEK, IV 和 AAD 对 plaintext 做 authenticated encryption, 得到 JWE Ciphertext 和 JWE Authentication Tag, 再按所选 serialization 输出.

5.2 Message Decryption (消息解密)

解密过程是加密过程的逆向. consumer 解析 serialization, base64url decode 各组件, 验证 JWE Protected Header 是合法 UTF-8 JSON object, 并合并 JOSE Header. Header Parameter name MUST NOT 重复.

consumer 必须确认实现理解并支持所有必需字段和 crit 指示的字段, 根据 alg 确定 key management mode, 选择 recipient key, 还原 CEK, 并使用 enc 指定的算法验证 Authentication Tag 和解密 ciphertext. 如果 tag 错误, MUST 拒绝输入且不能输出 decrypted plaintext.

多 recipient 的 JSON Serialization 中, 至少一个 recipient 的 encrypted content MUST 成功验证, 否则整个 JWE MUST 被视为 invalid. 即使 JWE 可成功解密, 如果算法不符合应用策略, 应用也 SHOULD 将其视为 invalid.

5.3 String Comparison Rules (字符串比较规则)

本规范的字符串比较规则与 JWS 第 5.3 节相同.