我正在编写一些密码(已知的算法-不是滚动我自己的),但我找不到任何关于这个案例的具体文档。
填充的一种方法(尽管存在问题,其中任何一个都可能有相同的问题)工作如下:
如果块小于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字节,如何知道它是否意味着填充(并且应该被删除),或者它是实际消息的一部分,您应该保留它?
我能想到的最合理的解决方案是在加密中添加/预置实际消息的大小,或者添加一个奇偶校验块来说明是否存在填充,这两者在我的脑海中都有自己的问题。
我假设这个问题以前就遇到过,但我想知道解决方案是什么。
发布于 2014-05-22 17:48:40
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.https://stackoverflow.com/questions/23813785
复制相似问题