首页 > 其他 > 详细

DES的几种填补方式

时间:2019-03-16 16:53:34      阅读:1161      评论:0      收藏:0      [点我收藏+]

 DES的几种填补方式 


   DES是对64位数据的加密算法,如数据位数不足64位的倍数,需要填充,补充到64位的倍数。 

  NoPadding 
   API或算法本身不对数据进行处理,加密数据由加密双方约定填补算法。例如若对字符串数据进行加解密,可以补充\0或者空格,然后trim 

  PKCS5Padding 
   加密前:数据字节长度对8取余,余数为m,若m>0,则补足8-m个字节,字节数值为8-m,即差几个字节就补几个字节,字节数值即为补充的字节数,若为0则补充8个字节的8 
   解密后:取最后一个字节,值为m,则从数据尾部删除m个字节,剩余数据即为加密前的原文 

因为DES是一种block cipher,一个block要8个字节,所以要加密的东西要分成8字节的整数倍,不足的就填充。
PKCS5Padding这种填充,填的字节代表所填字节的总数:

比如差三个字节的话填为 @@@@@333 差7个字节就填为 @7777777 没有差就填 88888888

 

下面两种摘自wiki:

ISO 10126[edit]

ISO 10126 (withdrawn, 2007[6][7]) specifies that the padding should be done at the end of that last block with random bytes, and the padding boundary should be specified by the last byte.

Example: In the following example the block size is 8 bytes and padding is required for 4 bytes

... | DD DD DD DD DD DD DD DD | DD DD DD DD 81 A6 23 04 |

Zero padding[edit]

All the bytes that are required to be padded are padded with zero. The zero padding scheme has not been standardized for encryption,[citation needed] although it is specified for hashes and MACs as Padding Method 1 in ISO/IEC 10118-1[9] and ISO/IEC 9797-1.[10]

Example: In the following example the block size is 8 bytes and padding is required for 4 bytes

... | DD DD DD DD DD DD DD DD | DD DD DD DD 00 00 00 00 |

Zero padding may not be reversible if the original file ends with one or more zero bytes, making it impossible to distinguish between plaintext data bytes and padding bytes. It may be used when the length of the message can be derived out-of-band. It is often applied to binary encoded strings as the null character can usually be stripped off as whitespace.

Zero padding is sometimes also referred to as "null padding" or "zero byte padding". Some implementations may add an additional block of zero bytes if the plaintext is already divisible by the block size.[citation needed]

DES的几种填补方式

原文:https://www.cnblogs.com/chenggang816/p/10542673.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!