本篇代码是在完成老师应用密码学实验时完成的,有一些想法还可以,可以留给自己以及后辈参考,关于DES加密的原理不多讲述。
编程语言:python3
def jiami(m,k):#m 为明文 k为密钥 均为二进制转换来的字符串.摘掉了0b ki=getSubKey(k)#得到子密钥序列 #轮密钥加密 m_after_ip=IP(m) l0=m_after_ip[:32]#完成左右交换 r0=m_after_ip[32:] for i in range(16): l1=r0 r1=str(bin(int(l0,2)^int(f(r0,ki[i]),2)))[2:]#经典的二进制/十六进制数字型与字符型转换 r1=fill(r1,32) l0=l1 r0=r1 t=r0+l0 c=NIP(t)#进行IP逆置换(但是似乎有点多余) return c def getSubKey(k):#得到子密钥分组,一个字符串组成的列表 k56=PC1(k) c=k56[:28] d=k56[28:] sk=[]#列表 ls=[1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,] for i in range(16): c1=LS(c,ls[i]) d1=LS(d,ls[i]) sk.append(PC2(c1+d1)) c=c1 d=d1 return sk def IP(m):#得到第一次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,] MAI='' for i in range(64): MAI+=m[ip[i]-1] return MAI def JIP(m):#解密得到第一次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,] MAI='' for i in range(64): MAI+=m[ip.index(i+1)] return MAI def NIP(t):#逆IP置换 nip=[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,] NAI='' for i in range(64): NAI+=t[nip[i]-1] return NAI def JNIP(t):#解密逆IP置换 nip=[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,] NAI='' for i in range(64): NAI+=t[nip.index(i+1)] return NAI def PC1(k):#返回字符串 pc1=[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,] k56='' for i in range(56): k56+=k[pc1[i]-1] return k56 def PC2(k):#返回字符串 pc2=[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] k48='' for i in range(48): k48+=k[pc2[i]-1] return k48 def LS(c,n):#返回字符串,用来进行移位操作 head=c[:n] tail=c[n:] return tail+head def P(s):#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,] p32='' for i in range(32): p32+=s[p[i]-1] return p32 def S(s0):#S替换 sbox=[ [14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7, 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,0,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,],] s='' for i in range(8): s+=fill((str(bin(sbox[i] [int ((s0[i*6]+s0[i*6+5]),2)*16+int((s0[i*6+1]+s0[i*6+2]+s0[i*6+3]+s0[i*6+4]),2) ]))[2:]),4) return s def fill(s,n):#填充0,防止转换时位数缺失 if len(s)==n: return s count=n-len(s) zero='0' for i in range(count): s=zero+s return s def f(r0,k): r=E(r0) s0=str(bin(int(r,2)^int(k,2)))[2:] s0=fill(s0,48) s=S(s0) c=P(s) return c def E(r0): r='' for i in range(8): r+=r0[((i*4+31)%32)] r+=r0[i*4] r+=r0[i*4+1] r+=r0[i*4+2] r+=r0[i*4+3] r+=r0[(i+1)*4%32] return r #####################################中文转换为十六进制 def ChtoHex(t): data=bytearray(t.encode()) s='' for char in data: s+=hex(char)[2:].rjust(2,'0') return s ################################################十六进制换回中文 def HextoCh(s): a=bytearray.fromhex(s) b=b'' for char in a: b+=bytes.fromhex(fill(str(hex(char))[2:],2)) c=b.decode() return c ###############################################33 def jiemi(c,k): ki=getSubKey(k) ki.reverse() c_after_NIP=JNIP(c) l0=c_after_NIP[32:] r0=c_after_NIP[:32] for i in range(16): r1=l0 l1=str(bin(int(r0,2)^int(f(l0,ki[i]),2)))[2:] l1=fill(l1,32) r0=r1 l0=l1 t=l0+r0 m=JIP(t) return m def ECBjiami(): fm=open('des_messages.txt','r',encoding='ansi') fk=open('des_key.txt','r') m=str(bin(int(ChtoHex(fm.read()),16)))[2:] if len(m)%4!=0: m=fill(m,(len(m)//4+1)*4) k=str(bin(int(fk.read(),16)))[2:] k=fill(k,64) long=len(m) if (long%64!=0): count=64-long%64 for i in range(count): m+='0' n=len(m)//64 c='' print("明文为:",hex(int(m,2))) print("密钥为:",hex(int(k,2))) for i in range(n): c+=jiami(m[i*64:(i+1)*64],k) fm.close() fk.close() return hex(int(c,2))[2:] def ECBjiemi(): fc=open('des_secret.txt','r') fk=open('des_key.txt','r') c=str(bin(int(fc.read(),16)))[2:] if len(c)%64!=0: c=fill(c,(len(c)//64+1)*64) k=str(bin(int(fk.read(),16)))[2:] k=fill(k,64) n=len(c)//64 m='' print("密文为:",hex(int(c,2))) print("密钥为:",hex(int(k,2))) for i in range(n): m+=jiemi(c[i*64:(i+1)*64],k) fc.close() fk.close() return HextoCh(hex(int(m,2))[2:]) def CBCjiami(): fm=open('des_messages.txt','r',encoding='ansi') fk=open('des_key.txt','r') fiv=open('des_iv.txt','r') m=str(bin(int(ChtoHex(fm.read()),16)))[2:] if len(m)%4!=0: m=fill(m,(len(m)//4+1)*4) k=str(bin(int(fk.read(),16)))[2:] k=fill(k,64) iv=str(bin(int(fiv.read(),16)))[2:] iv=fill(iv,64) long=len(m) if (long%64!=0): count=64-long%64 for i in range(count): m+='0' n=len(m)//64 c='' print("明文为:",hex(int(m,2))) print("密钥为:",hex(int(k,2))) print("向量为:",hex(int(iv,2))) for i in range(n): m0=m[i*64:(i+1)*64] mi=str(bin(int(m0,2)^int(iv,2)))[2:] mi=fill(mi,64) iv=jiami(mi,k) c+=iv fm.close() fk.close() fiv.close() return hex(int(c,2))[2:] def CBCjiemi(): fc=open('des_secret.txt','r') fk=open('des_key.txt','r') fiv=open('des_iv.txt','r') c=str(bin(int(fc.read(),16)))[2:] if len(c)%64!=0: c=fill(c,(len(c)//64+1)*64) k=str(bin(int(fk.read(),16)))[2:] k=fill(k,64) iv=str(bin(int(fiv.read(),16)))[2:] iv=fill(iv,64) n=len(c)//64 m='' print("密文为:",hex(int(c,2))) print("密钥为:",hex(int(k,2))) print("向量为:",hex(int(iv,2))) for i in range(n): c0=c[i*64:(i+1)*64] m0=str(bin(int(jiemi(c0,k),2)^int(iv,2)))[2:] m0=fill(m0,64) m+=m0 iv=c0 fc.close() fk.close() fiv.close() return HextoCh(hex(int(m,2))[2:]) def main(): print("This is based on py3 input 1 to use ECB and input 2 to use CBC and input 0 to exit ") while(1): i=input() if i=='0' : break elif (i=='1'): print("ECB:input 1 for jiami or 2 for jiemi ") j=input() if(j=='1'): print("加密结果为"+str(ECBjiami())) else: print("解密结果为"+str(ECBjiemi())) else: print("CBC:input 1 for jiami or 2 for jiemi ") j=input() if(j=='1'): print("加密结果为"+str(CBCjiami())) else: print("解密结果为"+str(CBCjiemi())) main()