The difference between the PKCS#5 and PKCS#7 padding mechanisms is the block size; PKCS#5 padding is defined for 8-byte block sizes, PKCS#7 padding would work for any block size from 1 to 255 bytes.

This is the definition of PKCS#5 padding (6.2) as defined in the RFC:

The padding string PS shall consist of 8 - (||M|| mod 8) octets all having value 8 - (||M|| mod 8).

The RFC that contains the PKCS#7 standard is the same except that it allows block sizes up to 255 bytes in size (10.3 note 2):

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.

So fundamentally PKCS#5 padding is a subset of PKCS#7 padding for 8 byte block sizes. Hence, PKCS#5 padding can not be used for AES. PKCS#5 padding was only defined with (triple) DES operation in mind.

Many cryptographic libraries use an identifier indicating PKCS#5 or PKCS#7 to define the same padding mechanism. The identifier should indicate PKCS#7 if block sizes other than 8 are used within the calculation. Some cryptographic libraries such as the SUN provider in Java indicate PKCS#5 where PKCS#7 should be used - "PKCS5Padding" should have been "PKCS7Padding". This is a legacy from the time that only 8 byte block ciphers such as (triple) DES symmetric cipher were available.

Note that neither PKCS#5 nor PKCS#7 is a standard created to describe a padding mechanism. The padding part is only a small subset of the defined functionality. PKCS#5 is a standard for Password Based Encryption or PBE, and PKCS#7 defines the Cryptographic Message Syntax or CMS.

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

value=k - (l mod k)  ,K=块大小,l=数据长度,如果l=8, 则需要填充额外的8个byte的8

在.net中,例如TripleDESCryptoServiceProvider ,默认BlockSize=64bits=8bytes,所以在这种情况下在PKCS5Padding=PKCS7Padding。

如果在C#中自己定义了一个不是64bits的加密块大小,同时使用PKCS7Padding,那么在java中使用JDK标准的PKCS5Padding就不能解密了。

https://crypto.stackexchange.com/questions/9043/what-is-the-difference-between-pkcs5-padding-and-pkcs7-padding

What is the difference between PKCS#5 padding and PKCS#7 padding的更多相关文章

  1. Padding Oracle Attack的一些细节与实现

    Padding Oracle Attack还是颇具威力的,ASP.NET的Padding Oracle Attack被Pwnie评为2010年最佳服务端漏洞之一.还是看 Juliano Rizzo a ...

  2. openssl AES加密以及padding

    好习惯,先上代码再说事 加密 void AesEncrypt(unsigned char* pchIn, int nInLen, unsigned char *ciphertext, int & ...

  3. pkcs#5和pkcs#7填充的区别

    最近做到了关于加密和解密的部分. 使用算法AES的时候,涉及到数据填充的部分,数据的填充有很多种方案,用的比较多的有pkcs#5,pkcs#7, 下面的都是从网上转来的.结论就是在AES 的使用中,p ...

  4. 【转载】 Tensorflow中padding的两种类型SAME和VALID

    原文地址: https://blog.csdn.net/jasonzzj/article/details/53930074 -------------------------------------- ...

  5. css padding 填充

    语法: padding:[ <length> | <percentage> ]{1,4} 默认值:看每个独立属性 适用于:所有元素,除 table-row-group | ta ...

  6. W3School-CSS 内边距 (padding) 实例

    CSS 内边距 (padding) 实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) 实例 CS ...

  7. 元素间距属性(scrollLeft,scrollWidth,clientWidth,offsetWidth,padding,margin)

    scrollHeight: 获取对象的滚动高度.scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端 ...

  8. 不让padding影响元素的宽度

    CSS3 新增了 box-sizing 属性. 以前,如果指定 div 的宽度为 div { width: 100px; height: 100px; padding: 10px; } 则包含 pad ...

  9. CSS知识点-- Padding

    The CSS padding properties define the space between the element border and the element content. Padd ...

随机推荐

  1. 【codeforces 796D】Police Stations

    [题目链接]:http://codeforces.com/contest/796/problem/D [题意] 在一棵树上,保证每个点在距离d之内都有一个警察局; 让你删掉最多的边,使得剩下的森林仍然 ...

  2. [HDU1403]Longest Common Substring(后缀数组)

    传送门 求两个串的公共子串(注意,这个公共子串是连续的一段) 把两个串连在一起,中间再加上一个原字符串中不存在的字符,避免过度匹配. 求一遍height,再从height中找满足条件的最大值即可. 为 ...

  3. printf()参数的处理

    下面程序的输出为? #include <stdio.h> int main(void) { ,b=,c=; printf(),(c = c*)); ; } 答案是110..40..60 这 ...

  4. nyoj_448_寻求最大数_201402261424

    寻找最大数 时间限制:1000 ms  |           内存限制:65535 KB 难度:2   描述 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大, 比如当n=920 ...

  5. HDU 4598

    这道题其实不需要考虑具体数值,但可以肯定的是,相连边的两端点必定有一正一负,至于谁正谁负,并不重要,这是可以思考的,很明显的一个二分图性质,如果不满足此条件,是不可能满足题目第二个条件的.所以首先对题 ...

  6. USACO money packageDP

    裸0/1背包,就是从各种币种里面拿来凑足N元,求最多有多种方案.用dp[i][j]表示选前i个币种凑成j的方案数量 状态转移方程: dp[i][j] = dp[i- 1][j]    j < c ...

  7. luogu2161 [SHOI2009]会场预约

    题目大意 随着时间的推移这里有几个任务对应着一段区间.每次要将任务安到时间线上时,要把时间线上已有的与该任务对应区间有交集的区间对应的任务删去.求每次删去的区间个数,以及整个时间线上有几个任务.时间线 ...

  8. 托管在IIS上的wcf,在启动的时候,写log

    https://blogs.msdn.microsoft.com/wenlong/2006/01/11/how-to-initialize-hosted-wcf-services/ Using App ...

  9. Android 6.0 中TimePicker显示为滚动样式的方法

    在Android6.0中,TimePicker控件的默认样式为转盘的样式,就像这个样子: 如果想要显示为之前的滚动样式的话也很简单,只要在布局文件中设置TimePicker的timePickerMod ...

  10. bzoj 1034 [ ZJOI 2008 ] 泡泡堂BNB —— 贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1034 一开始想了个很麻烦的贪心做法,对于每个 a[i],找第一个大于它的 b 匹配…… 然后 ...