漫画:什么是AES算法?

2018-01-03  本文已影响104人  大胡子商人
image image image image

假设有一个发送方在向接收方发送消息。如果没有任何加密算法,接收方发送的是一个明文消息:“我是小灰”

image

如果消息被中间人截获到,即使中间人无法篡改消息,也可以窥探到消息的内容,从而暴露了通信双方的私密。

因此我们不再直接传送明文,而改用对称加密的方式传输密文,画风就变成了下面这样:

image

具体工作的步骤如下:

1.发送方利用密钥123456,加密明文“我是小灰”,加密结果为TNYRvx+SNjZwEK+ZXFEcDw==。

2.发送方把加密后的内容TNYRvx+SNjZwEK+ZXFEcDw==传输给接收方。

3.接收方收到密文TNYRvx+SNjZwEK+ZXFEcDw==,利用密钥123456还原为明文“我是小灰”。

image image image image image

1.密钥

密钥是AES算法实现加密和解密的根本。对称加密算法之所以对称,是因为这类算法对明文的加密和解密需要使用同一个密钥

AES支持三种长度的密钥:

128位,192位,256位

平时大家所说的AES128,AES192,AES256,实际上就是指的AES算法对不同长度密钥的使用。

image image

2.填充

要想了解填充的概念,我们先要了解AES的分组加密特性。

什么是分组加密呢?我们来看看下面这张图:

image

AES算法在对明文加密的时候,并不是把整个明文一股脑加密成一整段密文,而是把明文拆分成一个个独立的明文块,每一个铭文块长度128bit。

这些明文块经过AES加密器的复杂处理,生成一个个独立的密文块,这些密文块拼接在一起,就是最终的AES加密结果。

但是这里涉及到一个问题:

假如一段明文长度是196bit,如果按每128bit一个明文块来拆分的话,第二个明文块只有64bit,不足128bit。这时候怎么办呢?就需要对明文块进行填充(Padding)。

image image

<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(255, 255, 255); white-space: normal; line-height: 26px; color: rgb(51, 51, 51);">NoPadding:
不做任何填充,但是要求明文必须是16字节的整数倍。</pre>

<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; white-space: normal; font-size: 14px; background-color: rgb(255, 255, 255); line-height: 26px; color: rgb(51, 51, 51);">PKCS5Padding(默认)
如果明文块少于16个字节(128bit),在明文块末尾补足相应数量的字符,且每个字节的值等于缺少的字符数。

比如明文:{1,2,3,4,5,a,b,c,d,e},缺少6个字节,则补全为{1,2,3,4,5,a,b,c,d,e,6,6,6,6,6,6} </pre>

<pre style="margin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; word-wrap: break-word; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(255, 255, 255); white-space: normal; font-size: 14px; line-height: 26px; color: rgb(51, 51, 51);">ISO10126Padding:
如果明文块少于16个字节(128bit),在明文块末尾补足相应数量的字节,最后一个字符值等于缺少的字符数,其他字符填充随机数。

比如明文:{1,2,3,4,5,a,b,c,d,e},缺少6个字节,则可能补全为{1,2,3,4,5,a,b,c,d,e,5,c,3,G,$,6} </pre>

image

3.模式

AES的工作模式,体现在把明文块加密成密文块的处理过程中。AES加密算法提供了五种不同的工作模式:

CBC、ECB、CTR、CFB、OFB

模式之间的主题思想是近似的,在处理细节上有一些差别。我们这一期只介绍各个模式的基本定义。

CBC模式:

电码本模式 Electronic Codebook Book

ECB模式(默认):

密码分组链接模式 Cipher Block Chaining

CTR模式:

计算器模式 Counter

CFB模式:

密码反馈模式 Cipher FeedBack

OFB模式:

输出反馈模式 Output FeedBack

image image image image image image

1. kgen.init传入的第一个参数128决定了密钥的长度是128bit

2. Cipher.getInstance("AES/CBC/NoPadding")决定了AES选择的填充方式是NoPadding,工作模式是CBC模式。

image

几点补充:

1.我们在调用封装好的AES算法时,表面上使用的Key并不是真正用于AES加密解密的密钥,而是用于生成真正密钥的“种子”。

2.填充明文时,如果明文长度原本就是16字节的整数倍,那么除了NoPadding以外,其他的填充方式都会填充一组额外的16字节明文块。

上一篇 下一篇

猜你喜欢

热点阅读