LV020-AES加密模式
对于对称加密一般分为流加密(如OFB、CFB等)和块加密(如ECB、CBC等)。一般的加密通常都是块加密,对于块加密(或称分组加密),如果要加密超过块大小的数据,就需要涉及填充和链加密模式。
块加密和流加密的基本概念:
- 块加密:或者叫分组加密, 块 为单位进行一次加密,将明文按一定的位长分组成块,明文块加密后得到密文组。解密雷同,密文组解密还原成明文组。
- 流加密:或者交序列加密,以 位 为单位进行一次加密,利用少量的密钥通过某种复杂的密码算法进行运算产生大量的伪随机位流,用于对明文位流的加密。
一、AES填充方式

以aes128为例,这里的每个明文块都是16字节,一共是128位。这里有一个问题,就是,明文数据最后不够16字节怎么办?比如这段数据有43个字节,我们可以分成两个16字节的明文块再加一个11字节的明文块,这最后一个少了5个字节。这时候就需要对最后的这11字节明文块进行处理,常见的填充方案有如下几种:
- PKCS7(Padding Cryptography System 7)填充:在明文末尾添加填充字节,填充字节的值等于需要填充的字节数。
填充字符串由一个字节序列组成,每个字节填充该字节序列的长度。假定块长度为 8,数据长度为 9,则填充用八位字节数等于 7,数据如下:
FF FF FF FF FF FF FF FF FF
这样可以分为两块:
块0 FF FF FF FF FF FF FF FF
块1 FFPKCS7 填充后如下所示:
块0 FF FF FF FF FF FF FF FF
块1 FF 07 07 07 07 07 07 07PKCS5(Padding Cryptography System 5)填充:与PKCS7填充类似,只是该种填充方式用于8字节分组的加密算法。
NoPadding填充:不进行任何填充,要求明文长度必须是分组长度的整数倍。
也有一些其他的填充方式,比如全填0之类的,但一般,自己在使用的时候,没什么特殊要求的时候,最后不足16字节的不加密就可以了。
二、加密模式
aes加密是分组加密,所以不仅涉及到填充方式,还会涉及到加密的模式,一般有五种:
电码本模式(Electronic Codebook Book (ECB))
密码分组链接模式(Cipher Block Chaining (CBC))
计算器模式(Counter (CTR))
密码反馈模式(Cipher FeedBack (CFB))
输出反馈模式(Output FeedBack (OFB))
1. ECB 模式
The Electronic Codebook (ECB) mode is a typical block cipher mode of operation using block cipher algorithm. In this version, we provide Data Encryption Standard (DES) and Advanced Encryption Standard (AES) processing ability, the cipherkey length for DES should be 64 bits, and 128/192/256 bits for AES. Another limitation is that our working mode works on units of a fixed size (64 or 128 bits for 1 block), but text in the real world has a variety of lengths. So, the last block of the text provided to this primitive must be padded to 128 bits before encryption or decryption.
ECB模式是最早采用和最简单的模式,它将加密的数据分成若干组,每组的大小跟加密密钥长度相同,然后每组都用相同的密钥进行加密。
| plaintext | ciphertext | cipherkey | IV | |
|---|---|---|---|---|
| ECB-DES | 64 | 64 | 64 | 64 |
| ECB-AES128 | 128 | 128 | 128 | 128 |
| ECB-AES192 | 128 | 128 | 192 | 128 |
| ECB-AES256 | 128 | 128 | 256 | 128 |

优点:
- 简单,易实现,可并行缺点:
缺点:
- 分组进行加密,加密方式一致,那么可根据密文情况观察到明文情况,有被破解的风险,不安全。
- 加密消息块相互独立成为被攻击的弱点,不能提供严格的数据保密性,可以对明文进行主动攻击。
2. CBC 模式
The Cipher Block Chaining (CBC) mode is a typical block cipher mode of operation using block cipher algorithm. In this version, we provide Data Encryption Standard (DES) and Advanced Encryption Standard (AES) processing ability, the cipherkey length for DES should be 64 bits, and 128/192/256 bits for AES. Another limitation is that our working mode works on units of a fixed size (64 or 128 bits for 1 block), but text in the real world has a variety of lengths. So, the last block of the text provided to this primitive must be padded to 128 bits before encryption or decryption.
CBC模式由IBM发明与1976年,在CBC模式中,每个平文块先与前一个密文块进行异或后,再进行加密。在这种方法中,每个密文块都依赖于它前面的所有密文块。同时,为了保证每条消息的唯一性,在第一个块中需要使用初始化向量。
| plaintext | ciphertext | cipherkey | IV | |
|---|---|---|---|---|
| CBC-DES | 64 | 64 | 64 | 64 |
| CBC-AES128 | 128 | 128 | 128 | 128 |
| CBC-AES192 | 128 | 128 | 192 | 128 |
| CBC-AES256 | 128 | 128 | 256 | 128 |

可以看到,这种方法的每个密文块都依赖于它前面的明文块,使加密的各段数据之间有了联系,保证了每条消息的唯一性。第一个块中需要使用始化向量IV。加密的时候,第一块后的每一块 需要先与前一个密文异或再加密
优点:
- 无法直接从密文中看出明文信息块的规律,所以安全性比较好
- 因密文块依赖于所有的信息块,加密时,若明文块有一个消息改变则影响所有密文块
- 加密解密过程都需要 初始化向量(而且要相同)
缺点:
因为具备依赖性,加密过程是串行的,无法被并行化,但是解密可以并行化,因为一个密文会影响到该明文与后一个明文,不会对其他明文产生影响
数据必须是块大小的整数倍,不够需要填充
3. CTR 模式
The Counter (CTR) mode is a typical block cipher mode of operation using block cipher algorithm. In this version, we provide Advanced Encryption Standard (AES) processing ability, the cipherkey length for AES should be 128/192/256 bits. Another limitation is that our working mode works on units of a fixed size (128 bits for 1 block), but text in the real world has a variety of lengths. So, the last block of the text provided to this primitive must be padded to 128 bits before encryption or decryption.
计算器模式不常见,在CTR模式中, 有一个自增的算子,这个算子用密钥加密之后的输出和明文异或的结果得到密文,相当于一次一密。这种加密方式简单快速,安全可靠,而且可以并行加密,但是在计算器不能维持很长的情况下,密钥只能使用一次。
| plaintext | ciphertext | cipherkey | IV | |
|---|---|---|---|---|
| CTR-AES128 | 128 | 128 | 128 | 128 |
| CTR-AES192 | 128 | 128 | 192 | 128 |
| CTR-AES256 | 128 | 128 | 256 | 128 |

优点:
- 允许并行计算、允许同时处理多块明文 / 密文
- 可随机访问,算法和加密盒的输出不依靠明文和密文的输入,因此如果有足够的保证安全的存储器,加密算法将仅仅是一系列异或运算,这将极大地提高吞吐量。
- 与CFB一样,无需填充
缺点:
4. CFB 模式
The Cipher Feedback (CFB) mode is a typical block cipher mode of operation using block cipher algorithm. In this version, we provide Data Encryption Standard (DES) and Advanced Encryption Standard (AES) processing ability, the cipherkey length for DES should be 64 bits, and 128/192/256 bits for AES. Another limitation is that our working mode works on units of a fixed size (64 or 128 bits for 1 block), but text in the real world has a variety of lengths. So, the last block of the text provided to this primitive must be padded to 128 bits before encryption or decryption. Although, CFB1 and CFB8 modes share the same interface with CFB128 mode, the plaintext and ciphertext is processed bit-by-bit or byte-by-byte not block-by-block for CFB1 and CFB8 modes respectively.
前一个密文使用秘钥Key再加密后,与明文异或,得到密文。第一个密文需要初始向量IV加密得到。解密也同样使用加密器进行解密。
| plaintext | ciphertext | cipherkey | IV | |
|---|---|---|---|---|
| CFB1-DES | 64 | 64 | 64 | 64 |
| CFB1-AES128 | 128 | 128 | 128 | 128 |
| CFB1-AES192 | 128 | 128 | 192 | 128 |
| CFB1-AES256 | 128 | 128 | 256 | 128 |
| CFB8-DES | 64 | 64 | 64 | 64 |
| CFB8-AES128 | 128 | 128 | 128 | 128 |
| CFB8-AES192 | 128 | 128 | 192 | 128 |
| CFB8-AES256 | 128 | 128 | 256 | 128 |
| CFB128-DES | 64 | 64 | 64 | 64 |
| CFB128-AES128 | 128 | 128 | 128 | 128 |
| CFB128-AES192 | 128 | 128 | 192 | 128 |
| CFB128-AES256 | 128 | 128 | 256 | 128 |

说明:
第一个块中都需要使用初始化向量IV
加密:通过密钥key加密后与明文进行异或得到密文,后面所有的加密都依赖于前一个加密后的输出,CFB是依赖前一个密文。
解密:通过密钥加密后,与密文进行异或得到明文,当前的 加密后的输出 会影响当前的明文与下一个明文,不会影响所有明文的生成,CFB是 密文会影响,而不是加密后的输出。
优点:
- 隐藏了明文模式
- 加密前后的数据位数是相同的
- 可以及时加密传送小于分组的数据
缺点:
- 与CBC类似,加密不利于并行计算,解密可以并行计算
- 加密时一个明文单元损坏影响多个单元
- 加密解密需要唯一的一个与块的大小相同的移位寄存器,并用初始化向量IV将寄存器初始化
5. OFB模式
The Output Feedback (OFB) mode is a typical block cipher mode of operation using block cipher algorithm. In this version, we provide Data Encryption Standard (DES) and Advanced Encryption Standard (AES) processing ability, the cipherkey length for DES should be 64 bits, and 128/192/256 bits for AES. Another limitation is that our working mode works on units of a fixed size (64 or 128 bits for 1 block), but text in the real world has a variety of lengths. So, the last block of the text provided to this primitive must be padded to 128 bits before encryption or decryption.
首先用 块加密器 生成 密钥流(Keystream),然后再将密钥流与明文流异或得到密文流;解密是先用块加密器生成密钥流,再将密钥流与密文流异或得到明文 (与CFB类似)
| plaintext | ciphertext | cipherkey | IV | |
|---|---|---|---|---|
| OFB-DES | 64 | 64 | 64 | 64 |
| OFB-AES128 | 128 | 128 | 128 | 128 |
| OFB-AES192 | 128 | 128 | 192 | 128 |
| OFB-AES256 | 128 | 128 | 256 | 128 |

参考资料:
关于PKCS5Padding与PKCS7Padding的区别 - 在路上... - 博客园 (cnblogs.com)
分组对称加密模式:ECB/CBC/CFB/OFB缺CTR - Ady Lee - 博客园 (cnblogs.com)