moooofly / MarkSomethingDownLLS

本仓库用于记录自 2017年10月16日起,在英语流利说任职期间的各方面知识汇总(以下内容不足以体现全部,一些敏感内容已移除)~
MIT License
72 stars 37 forks source link

PKCS5 与 PKCS7 #55

Open moooofly opened 5 years ago

moooofly commented 5 years ago

ref: https://dev-crypto.bouncycastle.narkive.com/6rvgeEVo/what-is-the-difference-between-pkcs5padding-and-pkcs7padding

It's really one of definition - the two mechanisms are the same.

Strictly speaking PKCS5Padding is only applicable to ciphers with a block size of 8 bytes as PKCS5 only talks about the mechanism as means of padding a block cipher with a block size of 8 bytes. In PKCS7 on the other hand the method was expanded to provide a general algorithm for introducing and removing padding in ciphers with a block size up to 255 bytes. So for example, if you want to be orthodox, while it makes sense to talk about DES with PKCS5Padding or PKCS7Padding, it only makes sense to talk about AES with PKCS7Padding.

Anything encrypted with PKCS5Padding should decrypt with PKCS7Padding. If you are seeing problems, it is not the padding that is causing them.

ref: https://stackoverflow.com/questions/20770072/aes-cbc-pkcs5padding-vs-aes-cbc-pkcs7padding-with-256-key-size-performance-java

The block size is a property of the used cipher algorithm. For AES it is always 16 bytes.

So strictly speaking, PKCS5Padding cannot be used with AES since it is defined only for a block size of 8 bytes. I assume, AES/CBC/PKCS5Padding is interpreted as AES/CBC/PKCS7Padding internally.

The only difference between these padding schemes is that PKCS7Padding has the block size as a parameter, while for PKCS5Padding it is fixed at 8 bytes. When the Block size is 8 bytes they do exactly the same.

ref: https://blog.csdn.net/Stewart/article/details/52462273

PKCS7Padding 和 PKCS5Padding 区别很简单,PKCS5Padding 的 blocksize 为 8 字节,而 PKCS7Padding 的 blocksize 可以为 1 到 255 字节;

ref: https://www.cnblogs.com/midea0978/articles/1437257.html

实际上 PKCS5Padding 与 PKCS7Padding 基本上也是可以通用的。

通过研读参考资料下面的参考资料可以发现两者定义的区别:

在 PKCS5Padding 中,明确定义 Block 的大小是 8 字节,而在 PKCS7Padding 定义中,对于块的大小是不确定的,可以在 1-255 之间(块长度超出 255 的尚待研究),填充值的算法都是一样的;