1977年1月美国政府采纳IBM公司设计的方案作为非机密数据的数据加密标准DES。DES自公布以来,一直是国际商用通信和计算机通信最常用的加密算法。原定使用期10年,超期服役到2000年。DES对其后出现的大量分组密码加密算法的设计都产生了很大的影响,他们从中借鉴和吸取了宝贵的经验。
def yihuo(s1,s2):#返回s1与s2异或运算后的字符串
strlen=len(s1)
s3=[]
for i in range(0,strlen):
s3.extend(str(int(s1[i])^int(s2[i])))
s4="".join(s3)
return s4
输入s1与s2是等长的二进制字符串,输出s4是s1于s2异或后的二进制字符串,由于python中不能向字符串动态添加元素,这里将异或后的元素存在一个列表(s3)里,之后将列表转换回字符串(s4)。
def zhihuan(s,tab):#通过置换表置换s
#strlen=len(s)#返回的字符串长度以置换表为准
strlen = len(tab)
list1=[]
for i in range(0,strlen):
j=tab[i]
list1.extend(str(s[j-1]))
s1="".join(list1)
return s1
这里的输入参数s是一个二进制字符串,tab是一个置换表,即将s第i位的字符置换位s的tab[i]位上的字符。
def zuoyi(s, n):#s循环左移n位
strlen = len(s)
n = n % strlen
list1 = list(s)
for i in range(0, strlen):
if i < n:
list1.extend(list1[0])
del list1[0]
else:
break
s1 = "".join(list1)
return s1
循环的方法是将s前n位字符(这里从1开始计数,python里下标从0开始,因此函数中退出条件为i=n)循环移到后面,同时将前面的字符删除;
今天老师讲了三种方法,分别是:
令字符串s=s+s,取新字符串的第n+1位到len(s)+n位,这种方法用含有指针的语言比较好实现
def bytetobit(n):#字节转比特
a=[]
for i in range(0,4):
a.insert(0,str(n%2))
n=int(n/2)
s1="".join(a)
return s1
字节转比特,即16进制转2进制
def yihuo(s1,s2):#返回s1与s2异或运算后的字符串
strlen=len(s1)
s3=[]
for i in range(0,strlen):
s3.extend(str(int(s1[i])^int(s2[i])))
s4="".join(s3)
return s4
def zhihuan(s,tab):#通过置换表置换s
#strlen=len(s)#返回的字符串长度以置换表为准
strlen = len(tab)
list1=[]
for i in range(0,strlen):
j=tab[i]
list1.extend(str(s[j-1]))
s1="".join(list1)
return s1
def zuoyi(s, n):#s循环左移n位
strlen = len(s)
n = n % strlen
list1 = list(s)
for i in range(0, strlen):
if i < n:
list1.extend(list1[0])
del list1[0]
else:
break
s1 = "".join(list1)
return s1
def bytetobit(n):#字节转比特
a=[]
for i in range(0,4):
a.insert(0,str(n%2))
n=int(n/2)
s1="".join(a)
return s1
M="0000000100100011010001010110011110001001101010111100110111101111"#明文
K="0001001100110100010101110111100110011011101111001101111111110001"#密钥
IPtab=[58,50,42,34,26,18,10,2,#初始置换IP表
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]
tIPtable=[40,8,48,16,56,24,64,32,#逆初始置换IP-1表
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]
PC_1tab=[57,49,41,33,25,17,9,#选择置换1表
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]
PC_2tab=[14,17,11,24,1,5,#选择置换2表
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]
E=[32,1,2,3,4,5,#表拓展置换
4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,1]
P=[16,7,20,21,#置换p表
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]
S=[[14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,#des算法s盒
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13,],
[15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,10,5,14,9,],
[10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12],
[7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14,],
[2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3],
[12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13,],
[4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12],
[13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11]]
def lunf(r,k,n):#轮函数f
s=[]
re=zhihuan(r,E)#Re表置换
print('E(R'+str(n)+')='+re)
rek=yihuo(re,k)#rek异或
print('E(R'+str(n)+')^K'+str(n+1)+'='+rek)
for i in range(0, 8):
row=int(rek[i*6])*2+int(rek[i*6+5])#行
col=int(rek[i*6+1])*8+int(rek[i*6+2])*4+int(rek[i*6+3])*2+int(rek[i*6+4])#列
s.extend(bytetobit(S[i][row*16+col]))
rkf1="".join(s)
print(str(n+1)+'轮s盒输出:'+rkf1)
rkf2=zhihuan(rkf1, P)
#print('---------------'+rkf2)
#print('rkf:'+rkf)
return rkf2
#产生子密钥
Kkid=[]#子密钥存放列表
movwei=[1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1]
Kzhihuan1=zhihuan(K,PC_1tab)#置换选择1
C=Kzhihuan1[0:28]
D=Kzhihuan1[28:56]
for Kkidi in range(0,16):
C = zuoyi(C, movwei[Kkidi])#左移对应的位数
D = zuoyi(D, movwei[Kkidi])
#print('C:'+C)
#print('D:'+D)
Kkid.append(zhihuan(C+D,PC_2tab))#置换选择2
for i in range(0,16):
print('k'+str(i+1)+'='+Kkid[i])
IPzhihuan=zhihuan(M,IPtab)#初始置换ip
print('IP置换:'+IPzhihuan)
L = [] #L列表
R = [] #R列表
L.append(IPzhihuan[0:32])
R.append(IPzhihuan[32:64])
i = 0
while i < 16:
print('R'+str(i)+'='+R[i])
lunr=lunf(R[i], Kkid[i], i)
print('f(R'+str(i)+','+'K'+str(i+1)+')='+lunr)
Rtemp = yihuo(L[i], lunr)
L.append(R[i])
R.append(Rtemp)
#print('L'+str(i)+'='+L[i])
i = i + 1
print('R16'+'='+R[16])
#print(len(L))
result=R[16]+L[16]
miwen=zhihuan(result, tIPtable)
print('密文:'+miwen)
其中输入的明文的二进制字符串为:
M="0000000100100011010001010110011110001001101010111100110111101111"#明文
密钥为:
K="0001001100110100010101110111100110011011101111001101111111110001"#密钥
k1=000110110000001011101111111111000111000001110010
k2=011110011010111011011001110110111100100111100101
k3=010101011111110010001010010000101100111110011001
k4=011100101010110111010110110110110011010100011101
k5=011111001110110000000111111010110101001110101000
k6=011000111010010100111110010100000111101100101111
k7=111011001000010010110111111101100001100010111100
k8=111101111000101000111010110000010011101111111011
k9=111000001101101111101011111011011110011110000001
k10=101100011111001101000111101110100100011001001111
k11=001000010101111111010011110111101101001110000110
k12=011101010111000111110101100101000110011111101001
k13=100101111100010111010001111110101011101001000001
k14=010111110100001110110111111100101110011100111010
k15=101111111001000110001101001111010011111100001010
k16=110010110011110110001011000011100001011111110101
IP置换:1100110000000000110011001111111111110000101010101111000010101010
R0=11110000101010101111000010101010
E(R0)=011110100001010101010101011110100001010101010101
E(R0)^K1=011000010001011110111010100001100110010100100111
1轮s盒输出:01011100100000101011010110010111
f(R0,K1)=00100011010010101010100110111011
R1=11101111010010100110010101000100
E(R1)=011101011110101001010100001100001010101000001001
E(R1)^K2=000011000100010010001101111010110110001111101100
2轮s盒输出:11111000110100000011101010101110
f(R1,K2)=00111100101010111000011110100011
R2=11001100000000010111011100001001
E(R2)=111001011000000000000010101110101110100001010011
E(R2)^K3=101100000111110010001000111110000010011111001010
3轮s盒输出:00100111000100001110000101101111
f(R2,K3)=01001101000101100110111010110000
R3=10100010010111000000101111110100
E(R3)=010100000100001011111000000001010111111110101001
E(R3)^K4=001000101110111100101110110111100100101010110100
4轮s盒输出:00100001111011011001111100111010
f(R3,K4)=10111011001000110111011101001100
R4=01110111001000100000000001000101
E(R4)=101110101110100100000100000000000000001000001010
E(R4)^K5=110001100000010100000011111010110101000110100010
5轮s盒输出:01010000110010000011000111101011
f(R4,K5)=00101000000100111010110111000011
R5=10001010010011111010011000110111
E(R5)=110001010100001001011111110100001100000110101111
E(R5)^K6=101001101110011101100001100000001011101010000000
6轮s盒输出:01000001111100110100110000111101
f(R5,K6)=10011110010001011100110100101100
R6=11101001011001111100110101101001
E(R6)=111101010010101100001111111001011010101101010011
E(R6)^K7=000110011010111110111000000100111011001111101111
7轮s盒输出:00010000011101010100000010101101
f(R6,K7)=10001100000001010001110000100111
R7=00000110010010101011101000010000
E(R7)=000000001100001001010101010111110100000010100000
E(R7)^K8=111101110100100001101111100111100111101101011011
8轮s盒输出:01101100000110000111110010101110
f(R7,K8)=00111100000011101000011011111001
R8=11010101011010010100101110010000
E(R8)=011010101010101101010010101001010111110010100001
E(R8)^K9=100010100111000010111001010010001001101100100000
9轮s盒输出:00010001000011000101011101110111
f(R8,K9)=00100010001101100111110001101010
R9=00100100011111001100011001111010
E(R9)=000100001000001111111001011000001100001111110100
E(R9)^K10=101000010111000010111110110110101000010110111011
10轮s盒输出:11011010000001000101001001110101
f(R9,K10)=01100010101111001001110000100010
R10=10110111110101011101011110110010
E(R10)=010110101111111010101011111010101111110110100101
E(R10)^K11=011110111010000101111000001101000010111000100011
11轮s盒输出:01110011000001011101000100000001
f(R10,K11)=11100001000001001111101000000010
R11=11000101011110000011110001111000
E(R11)=011000001010101111110000000111111000001111110001
E(R11)^K12=000101011101101000000101100010111110010000011000
12轮s盒输出:01111011100010110010011000110101
f(R11,K12)=11000010011010001100111111101010
R12=01110101101111010001100001011000
E(R12)=001110101011110111111010100011110000001011110000
E(R12)^K13=101011010111100000101011011101011011100010110001
13轮s盒输出:10011010110100011000101101001111
f(R12,K13)=11011101101110110010100100100010
R13=00011000110000110001010101011010
E(R13)=000011110001011000000110100010101010101011110100
E(R13)^K14=010100000101010110110001011110000100110111001110
14轮s盒输出:01100100011110011001101011110001
f(R13,K14)=10110111001100011000111001010101
R14=11000010100011001001011000001101
E(R14)=111000000101010001011001010010101100000001011011
E(R14)^K15=010111111100010111010100011101111111111101010001
15轮s盒输出:10110010111010001000110100111100
f(R14,K15)=01011011100000010010011101101110
R15=01000011010000100011001000110100
E(R15)=001000000110101000000100000110100100000110101000
E(R15)^K16=111010110101011110001111000101000101011001011101
16轮s盒输出:10100111100000110010010000101001
f(R15,K16)=11001000110000000100111110011000
R16=00001010010011001101100110010101
密文:1000010111101000000100110101010000001111000010101011010000000101