DES详解

DES概述

DES是一种对称加密算法(对称加密即加密和解密用的是同一个秘钥),又称为美国数据加密标准。DES的密钥长64位,实际上只有54位密钥参与了DES运算(第8/16/24/32/40/48/56/64位是校验位)。明文按64位进行分组,分组后的明文块和密钥通过DES加密后形成一个密文块,所有的密文块拼到一起输出就是密文。由于DES是对称加密,就要求发送方和接收方使用相同的密钥进行加密和解密,所以在传输数据之前双方得约定好密钥。

DES加密解密过程详解

首先我在B站上找了一个视频,可以很好的解释下面的部分过程。

DES的加密过程

首先是DES的加密过程:我们输入明文,DES会把明文分组,每组64bits,然后DES算法对每个明文块进行处理,得到64bits的密文,再将所有密文块拼接就成为了密文。

在这里插入图片描述

我们来看看具体的加密过程:

在这里插入图片描述

文字解释:

  1. 首先将64位明文经过初始置换
  2. 第二步又分为16个循环步骤,其中每一步都要用到一个不同key,第一个Round用k1,第二个用k2…
  3. 将经过16个Round的结果进行终止置换,得到这个明文块对应的密文块。

在上面这个过程中,有三个步骤要展开来讲:

1. 初始置换和终止置换

在这里插入图片描述

所谓初始置换就是将64位二进制数按照初始置换表上的顺序重新排序组成一个新的二进制数字。比如按照图中的初始置换表,我们需要将输入二进制数的第58位换到第一位上,第50位换到第二位上…

所谓终止置换的方法也是一样的,只是表的顺序不同罢了。还有要注意一点:DES加密过程中所有的Permutation(置换)都是按照对应表的顺序来改变二进制数的。

初始置换和终止置换的表排列是互逆的,也就是说对一个64位二进制数经过一次初始置换和终止置换,这个数就还原了。

2. Round

每一个round的算法都是一样的,图解如下:

在这里插入图片描述

首先是将输入的64位二进制数分为两部分,左半拉L1 32位,右半拉R1 32位,选择其中任意一部分进行 下面的操作,我们假设选择R1,然后对R1进行EP、XOR(key)、S-box、P-box操作,再与未进行任何操作的L1进行XOR得到这一轮Round的输出部分的右半拉,而输出部分的左半拉是未经过处理前的R1。

这里也有很多要拓展开讲的部分:

2.1 EP 拓展置换

所谓拓展置换,置换不用说,上面已经讲了如何操作,只是这个拓展要讲讲,我们输入拓展置换前是一个32位的为二进制数字,拓展置换后将得到一个48位的数字,所以就会多出来一些数字,DES的处理是将32位的某些数字重复使用,具体规则看下图的拓展置换表,这里面有一些重复的数字,我们只要将输入的32位二进制数按照下表的顺序来处理就可以得到一个48位的二进制数。

在这里插入图片描述

2.2 XOR 异或运算

所谓异或运算,就是将输入的两个二进制数字进行按位对比,如果两个数字相同则输出0,否则输出1;每一个Round中会用到两次异或运算。异或运算的图解:

在这里插入图片描述

2.3 S-box S盒压缩

一共有8个不同的S盒,每个对应一张4行16列的表格。每个S盒的作用是将输入的6位二进制数压缩成4位二进制数。

如图:

在这里插入图片描述

48位明文和48位密钥进行异或运算后得到一个48位的新数字,将这个数字按顺序分成8组,每一组6位二进制数,将第1组放入s-box1,第2组放入s-box2…

那每个s-box压缩的本质是什么?取原始数据的第一位和最后一位二进制数组成一个新的二进制数,并且将其转化为10进制数,这个数即为加密数所在的行数;中间四位二进制组成的十进制数,即为加密数所在的列数,根据行数和列数在压缩表中找到对应的压缩数,再把该压缩数转为二进制输出,完成了6位到4位的压缩。

在这里插入图片描述

拿第一张s-box表举例,我们输入的6位数字是011010,取第一位和最后一位二进制数组成的数对应十进制的0,中间四位二进制数对应十进制的13;在s-box1中找第0行,第13列的数字是9,也就是1001,将这个1001作为结果输出。

每个s-box输出一个4位数字,最后按顺序合并到一起输入到P盒置换中。

2.4 P-box P盒置换

P盒置换就是按照P盒置换表来进行操作。

在这里插入图片描述

在这里,我要强调一下,首先 P盒置换输出的结果output并不是密文,而是下一轮循环的input;其次第16Round输出的结果也不是密文,还要进行一次终止置换才可以。

3. Key

这一部分主要是说说秘钥的处理。

在这里插入图片描述

首先是将64位的密钥进行置换处理,得到一个56位的二进制数,再将这个数字一分为二,并对这两部分都进行循环左移操作后再合并起来,最后再进行一个压缩置换就得到一个48比特的密码之一了。

为什么是密码之一,这是因为秘钥的处理也是要经过16个循环的,上述过程只是一次循环,不过和明文的处理不同,密钥每次循环最后的结果都是要用的,而明文每次循环的结果只是下一次循环的输入。而且明文每一轮循环的用到的密文都不相同,对应着密文每一次循环输出的结果,完整过程如下图:
在这里插入图片描述

3.1 置换处理:(按表来,没什么要说的)

在这里插入图片描述

3.2 循环左移

在这里插入图片描述

所谓循环左移,解释一下,其实就是字面的意思,将28位二进制循环左移,但是移动几位呢?DES是这么规定的:第一轮循环,第二轮循环,第9轮循环,第16轮循环移动一位,其他的移动两位。

3.3 压缩置换(按表来,没什么要说的)

在这里插入图片描述

DES解密

DES的解密过程和加密过程几乎完全相同:

  • 有着相同的算法和密钥
  • 区别在于:1. Subkey16用在第一轮;2. Subkey15用在第二轮…
  • 12
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Electronic Codebook Mode (ECB),Cipher Block Chaining Mode (CBC).Cipher Feedback Mode (CFB) Only blocks of j <= 64 bits are enciphered at a time. A small j requires more cycles through the encipherment algorithm per unit of plaintext and thus greater processing overhead. A plaintext block always produces the same ciphertext block for the same key and starting variable. Blocks cannot be rearranged. Each ciphertext block depends on the current and all preceding plaintext blocks. Different starting variables are used to prevent the same plaintext enciphering to the same ciphertext. The strength of this mode depends on the size of the key k (best if j == k). An error will affect the current and the following ciphertext blocks. Output Feedback Mode (OFB) Only blocks of j <= 64 bits are enciphered at a time. A small j requires more cycles through the encipherment algorithm per unit of plaintext and thus greater processing overhead. A plaintext block always produces the same ciphertext block for the same key and starting variable. Different starting variables are used to prevent the same plaintext enciphering to the same ciphertext. Absence of chaining makes this mode vulnerable to specific attacks. Different start variable values prevent the same plaintext enciphering to the same ciphertext, by producing different key streams. An error bit in the ciphertext causes only one bit to be in error in the deciphered plaintext. It is not self-synchronizing. Triple-DES ECB Mode Encrypt with key1, decrypt with key2 and encrypt with key3 again. As for ECB encryption but increases the key length to 168 bits. If all keys are the same it is equivalent to encrypting once with just one key. If the first and last key are the same, the key length is 112 bits. If all 3 keys are the same, this is effectively the same as normal ECB mode. Triple-DES CBC Mode Encrypt with key1, decrypt with key2 and then encrypt with key3. As for CBC encryption but increases the key length to 168 bits with the same restrictions as the Triple-DES ESB mode Our first example shows how to use the basic DES encryption routine, DES_ecb_encrypt(), to encrypt or decrypt a single 64-bit block of plaintext to electronic code book (ECB) mode. If the encrypt argument is DES_ENCRYPT, the input (plaintext) is encrypted into the output (ciphertext) using the specified key_schedule. If the encrypt argument is DES_DECRYPT, the input (ciphertext) is decrypted into the output (plaintext). Note that input and output may overlap. Read more: http://blog.fpmurphy.com/2010/04/openssl-des-api.html#ixzz1n2Qte3UG
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值