首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加密-PKCS5 5/7填充竞争条件?

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

Stack Overflow用户
提问于 2014-05-22 17:41:52
回答 1查看 228关注 0票数 2

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

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

如果块小于8个字节,则用填充字节数填充结束。

于是FF E2 B8 AA变成FF E2 B8 AA 04 04 04

这是很棒的,并且允许你有一个很明显的窗口,你可以在解密的时候去掉填充,但是我的问题是,不是上面的例子,而是我有这个-

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

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-22 17:48:40

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

引用自PKCS #7规范

代码语言:javascript
复制
    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.
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23813785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档