跳到主要内容
uencoder

PKCS#7 Padding

PKCS#7 填充在明文末尾追加 n 个值为 n 的字节(1 ≤ n ≤ block_size),可逆且自校验,是 CBC 模式的事实标准填充。

用途

提供可逆且可校验的填充,使解密方能确定明文真实长度并检测填充错误。

机制

若明文长度恰为分组整数倍,仍追加一整块填充(值为 block_size)以保持可逆性。解密时读取最后一字节值 n,移除末尾 n 字节并验证它们均为 n。验证失败表明密文损坏或遭篡改。

用例

  • AES-CBC 默认填充
  • CMS/PKCS#7 消息格式
  • Java PKCS5Padding(等价于 PKCS7)
  • OpenSSL 默认填充

示例

  • 输入
    block=16, data=13 bytes
    输出
    appends 03 03 03

    Pads with three 0x03 bytes

常见错误

Non-constant-time padding verification falls to Padding Oracle attacks
Use AEAD, or compare padding in constant time

相关词条

相关工具

参考链接