【问题标题】:Encryption - PKCS5/7 padding race condition?加密 - PKCS5/7 填充竞争条件?
【发布时间】:2014-05-22 17:41:52
【问题描述】:

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

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

如果你的块

所以 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 字节你如何知道它是用于填充(并且应该被剥离)还是实际消息的一部分并且您应该保留它?

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

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

【问题讨论】:

    标签: encryption cryptography aes pkcs#7


    【解决方案1】:

    PKCS #5/7 填充总是添加 - 如果明文的长度是块大小的倍数,则添加整个填充块。这样就没有歧义,这是 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.
    

    【讨论】:

    • 那正盯着我的脸;感谢您的快速响应
    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 2022-01-23
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多