加密 - PKCS5/7 填充竞争条件?

标签 encryption cryptography aes pkcs#7

我正在编写一些加密算法(已知算法 - 不是我自己的算法),但我找不到关于这种情况的任何具体文档。

一种填充方法(尽管存在问题,但它们中的任何一个都可能有相同的问题)的工作原理如下:

如果您的 block 小于 8 个字节,请用填充字节数填充末尾

所以 FF E2 B8 AA 变成 FF E2 B8 AA 04 04 04 04

这很棒,可以让你有一个非常明显的窗口,你可以在解密过程中删除填充,但我的问题是,我有这个而不是上面的例子 -

10 39 ff ef 09 64 aa(长度为 7 个字节)。现在在这种情况下,上面的算法会说将其转换为 10 39 ff ef 09 64 aa 01,但我的问题是在解密时你如何决定在解密消息末尾获得 01 字节你如何知道它是否意味着填充(并且应该被剥离)或者它是实际消息的一部分并且您应该保留它?

我能想到的最合理的解决方案是在加密中追加/前置实际消息的大小,或者添加一个奇偶校验 block 来说明是否有填充,这两种方法在我看来都有各自的问题。

我假设以前遇到过这个问题,但我想知道解决方案是什么。

最佳答案

PKCS #5/7 填充总是被添加——如果明文的长度是 block 大小的倍数,则添加整个填充 block 。这样就没有歧义,这是 PKCS #7 相对于零填充的主要优势。

引自PKCS #7 specification :

    2.   Some content-encryption algorithms assume the
         input length is a multiple of k octets, where k > 1, and
         let the application define a method for handling inputs
         whose lengths are not a multiple of k octets. For such
         algorithms, the method shall be to pad the input at the
         trailing end with k - (l mod k) octets all having value k -
         (l mod k), where l is the length of the input. In other
         words, the input is padded at the trailing end with one of
         the following strings:

                  01 -- if l mod k = k-1
                 02 02 -- if l mod k = k-2
                             .
                             .
                             .
               k k ... k k -- if l mod k = 0

         The padding can be removed unambiguously since all input is
         padded and no padding string is a suffix of another. This
         padding method is well-defined if and only if k < 256;
         methods for larger k are an open issue for further study.

关于加密 - PKCS5/7 填充竞争条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23813785/

相关文章:

android - 为什么调试证书不能安全地签署 Android APK?

android - 如何在Android中写一个类似于Python函数的hash函数?

python - 如何在不损坏文件的情况下使用 AES 和 pycrypto 加密 .docx 文件

Java Card Crypto 异常 RSA key 加密

java - BouncyCaSTLe 最后一个字节解密问题

security - 考虑摩尔定律的密码强度检查器?

encryption - Vim 加密 : how to break it?

java.security.InvalidKeyException : Key length not 128/192/256 bits

java - 给定最终 block 在 AES 解密时未正确填充

安卓AES加密/解密