DES(Data Encryption Standard)采纳64位的分组长度和56位的密钥长度。它将64位的输入经由一系列变更获得64为的输出。解密运用雷同的步骤和雷同的密钥,唯一差别的是密钥递次与加密历程相反。
DES加密:
该算法的输入有须要加密的明文和加密运用的密钥,两者长度都为64位。个中密钥的第8,16,24,32,40,48,56,64位为奇偶校验位。
1、明文的处置惩罚
将明文读入顺序并将其化为二进制串
def inputText(filename): with open(filename,'r')as f: text = f.read() text = text.split('\n') text = [eval(x) for x in text] text = ['{:08b}'.format(x) for x in text] text = ''.join(text) return text
对明文举行IP置换,并划分为摆布两个子串
def IP_Transposition(plaintext): LR = [] for i in IP: LR.append(int(plaintext[i-1])) L = LR[:32] R = LR[32:] return L,R
2、对密钥的处置惩罚
将密钥读入顺序并以二进制串的情势存储
对密钥举行PC-1置换,并划分为两个子串
#密钥置换 def Key_Transposition(key): CD = [] for i in PC_1: CD.append(int(key[i-1])) C = CD[:28] D = CD[28:] return C,D
在生成迭代所须要的密钥之前须要对密钥举行置换紧缩
#密钥紧缩 def Key_Compress(C,D): key = C+D new_key = [] for i in PC_2: new_key.append(key[i-1]) return new_key
生成DES每轮迭代所须要的子密钥,以便加密解密时直接运用
def generateKset(key): key = inputKey(key) C,D = Key_Transposition(key) K = [] for i in LeftRotate: C = Key_LeftRotate(C,i) C = Key_LeftRotate(D,i) K.append(Key_Compress(C,D)) return K
3、F函数
在每轮变更中,悉数历程能够用以下公式示意:
$$ L_i = R_{i-1} $$
$$ R_i = L_{i-1}\bigoplus F(R_{i-1},K_i) $$
个中轮密钥 Ki长48位,R长32位,首先将R置换扩大为48位,这48位与Ki异或,获得的效果用替换函数作用发生32位的输出。这32位的输出经P置换后与L异或获得新的R
替代函数由8个S盒来构成,每一个S盒都有6位的输入和4位的输出。对每一个S盒,输入的第一名和末了一名构成一个2位的二进制数,用来挑选S盒4行替换值中的一行,中心4位用来挑选16列中的某一列。
#明文R扩大为48位 def R_expand(R): new_R = [] for i in E: new_R.append(R[i-1]) return new_R #将两列表元素异或 def xor(input1,input2): xor_result = [] for i in range(0,len(input1)): xor_result.append(int(input1[i])^int(input2[i])) return xor_result #将异或的效果举行S盒替代 def S_Substitution(xor_result): s_result = [] for i in range(0,8): tmp = xor_result[i*6:i*6+5] row = tmp[0]*2+tmp[-1] col = tmp[1]*8+tmp[2]*4+tmp[3]*2+tmp[4] s_result.append('{:04b}'.format(S[i][row][col])) s_result = ''.join(s_result) return s_result #F函数 def F(R,K): new_R = R_expand(R) R_Kxor= xor(new_R,K) s_result = S_Substitution(R_Kxor) p_result = P_Transposition(s_result) return p_result #将S盒替代的效果举行P置换 def P_Transposition(s_result): p_result = [] for i in P: p_result.append(int(s_result[i-1])) return p_result
4、加密历程
DES加密须要经由16轮迭代,前15轮迭代每次完毕须要交流L和R,第16次不交流
def DES_encrypt(filename,key,outputFile): #从文件中读取明文 plaintext = inputText(filename) #将明文举行置换星散 L,R = IP_Transposition(plaintext) #生成Kset K = generateKset(key) for i in range(0,15): oldR = R #F函数 p_result = F(R,K[i]) R = xor(L,p_result) L = oldR p_result = F(R,K[15]) L = xor(L,p_result) #IP逆置换 reversedP = IP_reverseTransp(L+R) #生成16进制示意的密文 Cipher = generateHex(reversedP) #将密文写入outputFile文件 writeFile(outputFile,Cipher) return Cipher
DES解密:
def DES_decrypt(filename,key,outputFile): #文件中读取密文 Ciphertext = inputText(filename) #将密文举行置换星散 L,R = IP_Transposition(Ciphertext) #生成Kset K = generateKset(key) for i in range(15,0,-1): oldR = R #F函数 p_result = F(R,K[i]) R = xor(L,p_result) L = oldR p_result = F(R,K[0]) L = xor(L,p_result) reversedP = IP_reverseTransp(L+R) plaintext = generateHex(reversedP) writeFile(outputFile,plaintext) return plaintext
源码地点 https://github.com/SuQinghang...
本篇文章到这里就已悉数完毕了,更多其他精彩内容能够关注ki4网的python视频教程栏目!
以上就是Python完成DES加密解密的要领引见(代码)的细致内容,更多请关注ki4网别的相干文章!