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

Few*_*itz 2 encryption cryptography aes pkcs#7

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

填充的一种方法(虽然问题是它们中的任何一个可能有相同的问题)的工作方式如下:

如果块是<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字节你怎么办?知道它是否意味着填充(并且应该被剥离)或它是实际消息的一部分,你应该保留它吗?

我能想到的最合理的解决方案是在加密中附加/预先添加实际消息的大小,或者添加一个奇偶校验块来说明是否存在填充,这两者都有我自己的问题.

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

nto*_*rnl 6

始终添加PKCS#5/7填充- 如果明文的长度是块大小的倍数,则添加整个填充块.这种方式没有歧义,这是PKCS#7的主要好处,比如零填充.

引自PKCS#7规范:

    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.
Run Code Online (Sandbox Code Playgroud)