DES算法详细实例与实现过程解析

时间:2022-06-26 19:45:17

如有疑问欢迎留言

参考网络资源用PYTHON把DES的流程走了一遍,以下为代码与解析,更为具体的细节可参照http://orlingrabbe.com/des.htm或*


假设要加密的原文为a406753854abcdef密钥为a34457799bbcdff1(16进制)

s = 'a406753854abcdef'
key = 'a34457799bbcdff1'

第一步 密钥的生成

1.将原文转为2进制

__hex_bin = {  
        '0':'0000','1':'0001','2':'0010','3':'0011',
        '4':'0100','5':'0101','6':'0110','7':'0111', 
        '8':'1000','9':'1001','a':'1010','b':'1011', 
        'c':'1100','d':'1101','e':'1110','f':'1111', 
        ' ':'0000'  
    }
key = ''.join(__hex_bin[i] for i in key) 

2.P-1重新排列

排列矩阵为P1

__re = lambda t, s: ''.join(s[i-1] for i in t) 

# PC-1
__k1 = [  
        57,49,41,33,25,17, 9,  
        1 ,58,50,42,34,26,18,  
        10, 2,59,51,43,35,27,  
        19,11, 3,60,52,44,36,  
        63,55,47,39,31,23,15,  
        7 ,62,54,46,38,30,22,  
        14, 6,61,53,45,37,29,  
        21,13, 5,28,20,12, 4,  
]

密钥由64位变为56位

3.将其分为左右两组C0与D0,分别按照循环移位表移位,得到Cn Dn

移位表:

       
                      序数       左移位数

                          1          1
                          2          1
                          3          2
                          4          2
                          5          2
                          6          2
                          7          2
                          8          2
                          9          1
                         10          2
                         11          2
                         12          2
                         13          2
                         14          2
                         15          2
                         16          1
代码:

k0 = [  
        0,1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28,  
]
key = [key[k0[i]:28]+key[0:k0[i]] + key[k0[i]+28:56]+key[28:k0[i]+28] for i in range(17)]
4再次重排列

#PC-2
__k2 = [  
        14,17,11,24, 1, 5, 3,28,  
        15, 6,21,10,23,19,12, 4,  
        26, 8,16, 7,27,20,13, 2,  
        41,52,31,37,47,55,30,40,  
        51,45,33,48,44,49,39,56,  
        34,53,46,42,50,36,29,32,  
]
key = [__re(__k2,key[i]) for i in range(1,17)] 


运行结果:

key
a34457799bbcdff1
K
1010001101000100010101110111100110011011101111001101111111110001
K+
11110001110011101010100111110101010101100110011110001100
n       Cn                              Dn
0       1111000111001110101010011111    0101010101100110011110001100
1       1110001110011101010100111111    1010101011001100111100011000
2       1100011100111010101001111111    0101010110011001111000110001
3       0001110011101010100111111111    0101011001100111100011000101
4       0111001110101010011111111100    0101100110011110001100010101
5       1100111010101001111111110001    0110011001111000110001010101
6       0011101010100111111111000111    1001100111100011000101010101
7       1110101010011111111100011100    0110011110001100010101010110
8       1010101001111111110001110011    1001111000110001010101011001
9       0101010011111111100011100111    0011110001100010101010110011
10      0101001111111110001110011101    1111000110001010101011001100
11      0100111111111000111001110101    1100011000101010101100110011
12      0011111111100011100111010101    0001100010101010110011001111
13      1111111110001110011101010100    0110001010101011001100111100
14      1111111000111001110101010011    1000101010101100110011110001
15      1111100011100111010101001111    0010101010110011001111000110
16      1111000111001110101010011111    0101010101100110011110001100
K 1     100110110000101011111111111110000111000001110010
K 2     011110011100111011011011110110111100100110100101
K 3     011101011111100110001110000000100100111110011001
K 4     001100101010110111010111110110110011000100010101
K 5     011111010110110000110111111000110100001110101000
K 6     111001111010010110111100010100000011101100001111
K 7     110111101000011010110111111101100001000010111100
K 8     111111111001101000111010010000010011101111101011
K 9     111000011111101110101011011011001110011110000001
K 10    101100011011011111010111101110100100010001001111
K 11    011101010101111011010011110011101101001110000010
K 12    011101111111000111110100100101000110011101101001
K 13    100111101100010111010111111110101001101001000000
K 14    011111110100001100111111110100001110011100111010
K 15    101011111001000110101101001111010011111000001000
K 16    110110111011010111001011000010100001011011110101


第二步 加密


1、同样转2进制

s = ''.join(__hex_bin[i] for i in s) 
2、重排列 排列矩阵为IP

__ip = [  
        58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,  
        62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,  
        57,49,41,33,25,17, 9,1,59,51,43,35,27,19,11,3,  
        61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7,  
]
s = __re(__ip,s)

3 F函数(在下一步用到)

F函数对于两个输入R和K

先对R根据E矩阵重排列得到E,再对E、K异或操作,再进行S-BOX

每六位进行一个S-BOX运算,首位与末位合做行标,中间四位做列标

最后仍有一个重新排列P

__p = [  
        16, 7,20,21,29,12,28,17,  
        1 ,15,23,26, 5,18,31,10,  
        2 ,8 ,24,14,32,27, 3, 9,  
        19,13,30, 6,22,11, 4,25,  
]


def __f(r,k):
	#E
	r = __re(__e,r)
	#K+E
	r = ''.join('0' if k[i]==r[i] else '1' for i in range(len(r)))
#sbox lookup row i col j
#first and last bit is i
#middle four is j
	r = ''.join(__hex_bin['%x'%__s[i][int(r[i*6]+r[i*6+5],2)*16+int(r[i*6+1:i*6+5],2)]] for i in range(8))
	r = __re(__p,r)
	return r
其中s-box矩阵为
__s = [  
        [  
        0xe,0x4,0xd,0x1,0x2,0xf,0xb,0x8,0x3,0xa,0x6,0xc,0x5,0x9,0x0,0x7,  
        0x0,0xf,0x7,0x4,0xe,0x2,0xd,0x1,0xa,0x6,0xc,0xb,0x9,0x5,0x3,0x8,  
        0x4,0x1,0xe,0x8,0xd,0x6,0x2,0xb,0xf,0xc,0x9,0x7,0x3,0xa,0x5,0x0,  
        0xf,0xc,0x8,0x2,0x4,0x9,0x1,0x7,0x5,0xb,0x3,0xe,0xa,0x0,0x6,0xd,  
        ],  
        [  
        0xf,0x1,0x8,0xe,0x6,0xb,0x3,0x4,0x9,0x7,0x2,0xd,0xc,0x0,0x5,0xa,  
        0x3,0xd,0x4,0x7,0xf,0x2,0x8,0xe,0xc,0x0,0x1,0xa,0x6,0x9,0xb,0x5,  
        0x0,0xe,0x7,0xb,0xa,0x4,0xd,0x1,0x5,0x8,0xc,0x6,0x9,0x3,0x2,0xf,  
        0xd,0x8,0xa,0x1,0x3,0xf,0x4,0x2,0xb,0x6,0x7,0xc,0x0,0x5,0xe,0x9,  
        ],  
        [  
        0xa,0x0,0x9,0xe,0x6,0x3,0xf,0x5,0x1,0xd,0xc,0x7,0xb,0x4,0x2,0x8,  
        0xd,0x7,0x0,0x9,0x3,0x4,0x6,0xa,0x2,0x8,0x5,0xe,0xc,0xb,0xf,0x1,  
        0xd,0x6,0x4,0x9,0x8,0xf,0x3,0x0,0xb,0x1,0x2,0xc,0x5,0xa,0xe,0x7,  
        0x1,0xa,0xd,0x0,0x6,0x9,0x8,0x7,0x4,0xf,0xe,0x3,0xb,0x5,0x2,0xc,  
        ],  
        [  
        0x7,0xd,0xe,0x3,0x0,0x6,0x9,0xa,0x1,0x2,0x8,0x5,0xb,0xc,0x4,0xf,  
        0xd,0x8,0xb,0x5,0x6,0xf,0x0,0x3,0x4,0x7,0x2,0xc,0x1,0xa,0xe,0x9,  
        0xa,0x6,0x9,0x0,0xc,0xb,0x7,0xd,0xf,0x1,0x3,0xe,0x5,0x2,0x8,0x4,  
        0x3,0xf,0x0,0x6,0xa,0x1,0xd,0x8,0x9,0x4,0x5,0xb,0xc,0x7,0x2,0xe,  
        ],  
        [  
        0x2,0xc,0x4,0x1,0x7,0xa,0xb,0x6,0x8,0x5,0x3,0xf,0xd,0x0,0xe,0x9,  
        0xe,0xb,0x2,0xc,0x4,0x7,0xd,0x1,0x5,0x0,0xf,0xa,0x3,0x9,0x8,0x6,  
        0x4,0x2,0x1,0xb,0xa,0xd,0x7,0x8,0xf,0x9,0xc,0x5,0x6,0x3,0x0,0xe,  
        0xb,0x8,0xc,0x7,0x1,0xe,0x2,0xd,0x6,0xf,0x0,0x9,0xa,0x4,0x5,0x3,  
        ],  
        [  
        0xc,0x1,0xa,0xf,0x9,0x2,0x6,0x8,0x0,0xd,0x3,0x4,0xe,0x7,0x5,0xb,  
        0xa,0xf,0x4,0x2,0x7,0xc,0x9,0x5,0x6,0x1,0xd,0xe,0x0,0xb,0x3,0x8,  
        0x9,0xe,0xf,0x5,0x2,0x8,0xc,0x3,0x7,0x0,0x4,0xa,0x1,0xd,0xb,0x6,  
        0x4,0x3,0x2,0xc,0x9,0x5,0xf,0xa,0xb,0xe,0x1,0x7,0x6,0x0,0x8,0xd,  
        ],  
        [  
        0x4,0xb,0x2,0xe,0xf,0x0,0x8,0xd,0x3,0xc,0x9,0x7,0x5,0xa,0x6,0x1,  
        0xd,0x0,0xb,0x7,0x4,0x9,0x1,0xa,0xe,0x3,0x5,0xc,0x2,0xf,0x8,0x6,  
        0x1,0x4,0xb,0xd,0xc,0x3,0x7,0xe,0xa,0xf,0x6,0x8,0x0,0x5,0x9,0x2,  
        0x6,0xb,0xd,0x8,0x1,0x4,0xa,0x7,0x9,0x5,0x0,0xf,0xe,0x2,0x3,0xc,  
        ],  
        [  
        0xd,0x2,0x8,0x4,0x6,0xf,0xb,0x1,0xa,0x9,0x3,0xe,0x5,0x0,0xc,0x7,  
        0x1,0xf,0xd,0x8,0xa,0x3,0x7,0x4,0xc,0x5,0x6,0xb,0x0,0xe,0x9,0x2,  
        0x7,0xb,0x4,0x1,0x9,0xc,0xe,0x2,0x0,0x6,0xa,0xd,0xf,0x3,0x5,0x8,  
        0x2,0x1,0xe,0x7,0x4,0xa,0x8,0xd,0xf,0xc,0x9,0x0,0x3,0x5,0x6,0xb,  
        ],  
]  


4

循环16轮进行以下步骤

Ln = Rn-1
Rn
= Ln-1 异或 f(Rn-1,Kn)

f为上述的f函数

for i in range(16):
	l_n = r
	print '-'*30,'Round',i+1,'-'*30
	r_n = __f(r,key[i])
	r_n = ''.join('0' if r_n[i]==l[i] else '1' for i in range(len(l)))
	print 'r\t',r_n
	print 'l\t',l_n
	l,r = l_n,r_n

运行结果为

M
1010010000000110011101010011100001010100101010111100110111101111
IP
1101010000011100110101111110010011100001101011011110100010100010
------------------------------ Round 1 ------------------------------
E       011100000011110101011011111101010001010100000101
K+E     111010110011011110100100000011010110010101110111
S-Box   10100110100010011011010001010000
P       11100011100100000000001111011000
r       00110111100011001101010000111100
l       11100001101011011110100010100010
------------------------------ Round 2 ------------------------------
E       000110101111110001011001011010101000000111111000
K+E     011000110011001010000010101100010100100001011101
S-Box   01010110001111010111001101101001
P       11101100001101001011110011010110
r       00001101100110010101010001110100
l       00110111100011001101010000111100
------------------------------ Round 3 ------------------------------
E       000001011011110011110010101010101000001110101000
K+E     011100000100010101111100101010001100110000110001
S-Box   00001000010110001101011010101111
P       00101101001011110000110001101001
r       00011010101000111101100001010101
l       00001101100110010101010001110100
------------------------------ Round 4 ------------------------------
E       100011110101010100000111111011110000001010101010
K+E     101111011111100011010000001101000011001110111111
S-Box   01110101101000011101111111011011
P       10111011001101101110101100011111
r       10110110101011111011111101101011
l       00011010101000111101100001010101
------------------------------ Round 5 ------------------------------
E       110110101101010101011111110111111110101101010111
K+E     101001111011100101101000001111001010100011111111
S-Box   01000101110111000001001010111011
P       00101110001000111101110101010001
r       00110100100000000000010100000100
l       10110110101011111011111101101011
------------------------------ Round 6 ------------------------------
E       000110101001010000000000000000001010100000001000
K+E     111111010011000110111100010100001001001100000111
S-Box   11010000111010000011011110001000
P       00101000101000011010000111001111
r       10011110000011100001111010100100
l       00110100100000000000010100000100
------------------------------ Round 7 ------------------------------
E       010011111100000001011100000011111101010100001001
K+E     100100010100011011101011111110011100010110110101
S-Box   11100010101100011110010101111001
P       11001111100101001010111110001100
r       11111011000101001010101010001000
l       10011110000011100001111010100100
------------------------------ Round 8 ------------------------------
E       011111110110100010101001010101010101010001010001
K+E     100000001111001010010011000101000110111110111010
S-Box   01001110001101110010111100100011
P       11010100011010101011110010011100
r       01001010011001001010001000111000
l       11111011000101001010101010001000
------------------------------ Round 9 ------------------------------
E       001001010100001100001001010100000100000111110000
K+E     110001001011100010100010001111001010011001110001
S-Box   01010010011001100001001000101111
P       01101000011000111001110000100110
r       10010011011101110011011010101110
l       01001010011001001010001000111000
------------------------------ Round 10 ------------------------------
E       010010100110101110101110100110101101010101011101
K+E     111110111101110001111001001000001001000100010010
S-Box   00001110010011000111011100101001
P       01101000001011010011110011011000
r       00100010010010011001111011100000
l       10010011011101110011011010101110
------------------------------ Round 11 ------------------------------
E       000100000100001001010011110011111101011100000000
K+E     011001010001110010000000000000010000010010000010
S-Box   10011100000101110010000011000010
P       10000100110110100001000010010011
r       00010111101011010010011000111101
l       00100010010010011001111011100000
------------------------------ Round 12 ------------------------------
E       100010101111110101011010100100001100000111111010
K+E     111111010000110010101110000001001010011010010011
S-Box   11011001000111011110001010100101
P       10000101101011001101110011100011
r       10100111111001010100001000000011
l       00010111101011010010011000111101
------------------------------ Round 13 ------------------------------
E       110100001111111100001010101000000100000000000111
K+E     010011100011101011011101010110101101101001000111
S-Box   01101000100111101111111100011000
P       00111111011011001011001111001000
r       00101000110000011001010111110101
l       10100111111001010100001000000011
------------------------------ Round 14 ------------------------------
E       100101010001011000000011110010101011111110101010
K+E     111010100101010100111100000110100101100010010000
S-Box   10101010110010000001001001001010
P       01101000101110110000001101000000
r       11001111010111100100000101000011
l       00101000110000011001010111110101
------------------------------ Round 15 ------------------------------
E       111001011110101011111100001000000010101000000111
K+E     010010100111101101010001000111010001010000001111
S-Box   10100001100001001100011000110100
P       00000011101001000101011100101000
r       00101011011001011100001011011101
l       11001111010111100100000101000011
------------------------------ Round 16 ------------------------------
E       100101010110101100001011111000000101011011111010
K+E     010011101101111011000000111010100100000000001111
S-Box   01100100010101110011111101000100
P       10110100011100011011001010111000
r       01111011001011111111001111111011
l       00101011011001011100001011011101

将第16轮后的L和R合并,进行IP-1的重排列

s = r_n+l_n
s = __re(__ip1,s)
print ' '*25,'%x'%int(s,2)
IP-1排列表为

__ip1 = [  
        40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,  
        38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,  
        36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,  
        34,2,42,10,50,18,58,26,33,1,41, 9,49,17,57,25,  
]


得到最终结果

RL
0111101100101111111100111111101100101011011001011100001011011101
IP-1
1111011111011101001100101101001101000111111101010110111100001111
------------------------------ Hex Form ------------------------------
                          f7dd32d347f56f0f