关于PKCS5Padding与PKCS7Padding的理解

前言

在采用AES、DES等块加密时,有时需要对不满足一个整块(block)的部分需要进行填充,我们常用的填充的方式就包括ZeroPadding、PKCS5Padding与PKCS7Padding,这里面有什么区别呢。

填充方式的区别

ZeroPadding,数据长度不对齐时使用0填充,否则不填充。使用0填充有个缺点,当元数据尾部也存在0时,在unpadding时可能会存在问题。

我们这里主要讨论PKCS7Padding与PKCS5Padding。

这是RFC中关于PKCS7的具体填充过程:

Some content-encryption algorithms assume the input length is a multiple of k octets, where k is greater than one. For such algorithms, the input shall be padded at the trailing end with k-(lth mod k) octets all having value k-(lth mod k), where lth 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 lth mod k = k-1
           02 02 -- if lth mod k = k-2
               .
               .
               .
     k k ... k k -- if lth mod k = 0 

The padding can be removed unambiguously since all input is padded,including input values that are already a multiple of the block size,and no padding string is a suffix of another. This padding method is well defined if and only if k is less than 256.

(1)PKCS7Padding,

  假设每个区块大小为blockSize

  <1>已对齐,填充一个长度为blockSize且每个字节均为blockSize的数据。

  <2>未对齐,需要补充的字节个数为n,则填充一个长度为n且每个字节均为n的数据。

(2)PKCS5Padding,PKCS7Padding的子集,只是块大小固定为8字节。

两者的区别在于PKCS5Padding是限制块大小的PKCS7Padding

具体代码实现

按照以上的定义,我们用golang实现如下:

func PKCS7Padding(cipherText []byte, blockSize int) []byte {
   padding := blockSize - len(cipherText)%blockSize
   padText := bytes.Repeat([]byte{byte(padding)}, padding)
   return append(cipherText, padText...)
}

func PKCS5Padding(cipherText []byte) []byte {
   return PKCS7Padding(cipherText, 8)
}

在解密后,需要将填充的字符去掉,取最后一位即知道存在多少个补充位,实现如下:

func unpadding(src []byte) []byte {
    n := len(src)
    if n == 0 {
        return src
    }
    paddingNum := int(src[n-1])
    return src[:n-paddingNum]
}

公众号

鄙人刚刚开通了公众号,专注于分享Go开发相关内容,望大家感兴趣的支持一下,在此特别感谢。
gzh

  • 7
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
AES/ECB/PKCS7Padding和AES/ECB/PKCS5Padding是两种常见的AES加密模式和填充方式。它们的区别在于填充方式的不同。 PKCS5PaddingPKCS7Padding都是用于填充数据块的,以确保数据块的长度满足加密算法的要求。它们的主要区别在于对于数据块长度不满足加密算法要求时的处理方式。 PKCS5Padding是针对8字节数据块的填充方式,当数据块长度不满8字节时,会使用特定的字节填充数据块,填充的字节值等于需要填充的字节数。例如,如果数据块长度为6字节,则会填充2个字节的值为0x02的字节。 PKCS7Padding是通用的填充方式,可以用于任意长度的数据块。当数据块长度不满足加密算法要求时,会使用特定的字节填充数据块,填充的字节值等于需要填充的字节数。例如,如果数据块长度为6字节,则会填充2个字节的值为0x02的字节。 因此,PKCS5PaddingPKCS7Padding区别在于对于数据块长度不满足加密算法要求时的处理方式不同。 下面是一个示例代码,演示了AES/ECB/PKCS5Padding和AES/ECB/PKCS7Padding的使用: ```java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; public class AESExample { public static void main(String[] args) throws Exception { String key = "0123456789abcdef"; String plaintext = "Hello World"; // AES/ECB/PKCS5Padding Cipher cipher1 = Cipher.getInstance("AES/ECB/PKCS5Padding"); SecretKeySpec keySpec1 = new SecretKeySpec(key.getBytes(), "AES"); cipher1.init(Cipher.ENCRYPT_MODE, keySpec1); byte[] encrypted1 = cipher1.doFinal(plaintext.getBytes()); System.out.println("AES/ECB/PKCS5Padding Encrypted: " + new String(encrypted1)); // AES/ECB/PKCS7Padding Cipher cipher2 = Cipher.getInstance("AES/ECB/PKCS7Padding"); SecretKeySpec keySpec2 = new SecretKeySpec(key.getBytes(), "AES"); cipher2.init(Cipher.ENCRYPT_MODE, keySpec2); byte[] encrypted2 = cipher2.doFinal(plaintext.getBytes()); System.out.println("AES/ECB/PKCS7Padding Encrypted: " + new String(encrypted2)); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值