赞
踩
编写DES算法实现程序,运行DES程序,演示DES加密与解密的过程。
每一步实现的过程参考:DES算法过程
- import binascii
- import os
-
-
- class DES():
- 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
- ] # IP置换表
- 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
- ] # IP逆置换表
- 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
- ] # E扩展置换表
- 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,
- ] # P盒置换表
- K = '0111011101101000011010010111001101101001011100110110100001110010'
- 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,
- ] # 密钥初始置换表
- 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,
- ]
- k0 = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, ] # 循环左移表
- S = [ # S盒的8个盒
- # S1
- [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],
- # S2
- [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],
- # S3
- [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],
- # S4
- [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],
- # S5
- [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],
- # S6
- [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],
- # S7
- [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],
- # S8
- [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 replace(self, string: str, self_table: list):
- """
- :param string: 需要进行置换的列表,是一个01字符串
- :param self_table: 置换表
- """
- rep_result = ""
- for i in self_table:
- rep_result += string[i - 1]
- return rep_result
-
- def bit_encode(self, string: str):
- """
- 将明文转为二进制字符串
- """
- plaintext_list = list(bytes(string, 'utf8')) # 将字符串转成bytes类型,再转成list
- result = []
- for num in plaintext_list:
- result.append(bin(num)[2:].zfill(8)) # 将列表的每个元素转成二进制字符串,8位宽度
- return "".join(result)
-
- def str_encode(self, binary: str):
- """
- 二进制字符串转成字符串
- """
- list_bin = [binary[i:i + 8] for i in range(0, len(binary), 8)] # 对二进制字符串进行切分,每8位为一组
- list_int = []
- for b in list_bin:
- list_int.append(int(b, 2)) # 二进制转成int
- result = bytes(list_int).decode() # 将列表转成bytes,解码,得到字符串
- return result
-
- def __bin2int(self, binary: str):
- """
- 将二进制字符串每8位转成int型号列表->bytes->hex
- """
- list_bin = [binary[i:i + 8] for i in range(0, len(binary), 8)] # 对二进制字符串进行分组,每8位为一组
- list_int = []
- for b in list_bin:
- list_int.append(int(b, 2))
- return list_int
-
- def __int2bin(self, list_int: list):
- result = []
- for num in list_int:
- result.append(bin(num)[2:].zfill(8))
- return ''.join(result)
-
- def group_by_64bit(self, binary: str):
- """
- 对01字符串进行分组,64位一组,不够的在末位补0
- """
- len_binary = len(binary)
- if len_binary % 64 != 0:
- binary_block = binary + ("0" * (64 - (len_binary % 64)))
- return [binary_block[i:i + 64] for i in range(0, len(binary_block), 64)]
- else:
- return [binary[j:j + 64] for j in range(0, len(binary), 64)]
-
- def modify_default_k(self):
- """
- 修改默认密钥函数
- """
- print(f'默认密钥的01字符串为:{self.K}', ' ', len(self.K), '位')
- print(f'默认密钥的字符串为:{self.str_encode(self.K)}')
- newkey = input('输入新的长度为8的密钥:')
- if len(newkey) != 8:
- print('密钥长度不合法,请重新输入:')
- self.modify_default_k()
- else:
- bin_key = self.bit_encode(newkey)
- self.K = bin_key
- print(f'当前密钥的01字符串为:{self.K}', ' ', len(self.K), '位')
-
- def f_func(self, right: str, key: str):
- """
- :param right: 明文二进制的字符串加密过程的右半部分
- :param key: 当前轮数的密钥
- """
- e_result = self.replace(right, self.E) # E扩展置换,对right
- xor_result = self.xor_func(e_result, key) # 与本轮子密钥key异或
- s_result = self.s_box(xor_result) # 进行S盒压缩置换
- p_result = self.replace(s_result, self.P) # P盒置换
- return p_result
-
- def get_key_list(self):
- key = self.replace(self.K, self.k1)
- left_key = key[0:28]
- right_key = key[28:56]
- keys = []
- for i in range(1, 17):
- move = self.k0[i - 1]
- move_left = left_key[move:28] + left_key[0:move]
- move_right = right_key[move:28] + right_key[0:move]
- left_key = move_left
- right_key = move_right
- move_key = left_key + right_key
- ki = self.replace(move_key, self.k2)
- keys.append(ki)
- # print(f'第{i}轮的子密钥为:{ki}')
- return keys
-
- def xor_func(self, xor1: str, xor2: str):
- """
- :return: 异或操作返回的结果
- """
- size = len(xor1)
- result = ""
- for i in range(0, size):
- result += '0' if xor1[i] == xor2[i] else '1'
- return result
-
- def s_box(self, xor_result: str):
- """
- :param xor_result: 48位01字符串
- :return: 48位比特串分为8组,每组6位,将6位变4位,每6位首尾做行号,其余4位做列号,在表中查找对应数值
- """
- result = ""
- for i in range(0, 8): # 将48位数据分为6组,循环进行
- block = xor_result[i * 6:(i + 1) * 6] # 切片
- line = int(block[0] + block[5], 2)
- colmn = int(block[1:4], 2)
- res = bin(self.S[i][line * 16 + colmn])[2:]
- if len(res) < 4:
- res = '0' * (4 - len(res)) + res
- result += res
- return result
-
- def iteration(self, bin_plaintext: str, key_list: list):
- """
- :param bin_plaintext: 待加密的64位01字符串
- :param key_list: 子密钥列表,共有16个48bit的
- """
- left = bin_plaintext[0:32]
- right = bin_plaintext[32:64]
- for i in range(0, 16):
- next_lift = right # 本轮的right为下一轮的left
- f_result = self.f_func(right, key_list[i])
- next_right = self.xor_func(left, f_result) # 下一轮的right由本轮的left与f函数结果异或所得
- left = next_lift
- right = next_right
- print(f'第{i + 1}轮加密/解密的结果为:{left + right}')
- bin_plaintext_result = left + right # 准备进行下一轮
- return bin_plaintext_result[32:] + bin_plaintext_result[:32]
-
- def encode(self, plaintext):
- """
- :param plaintext: 明文字符串
- :return: 密文字符串
- """
- bin_plaintext = self.bit_encode(plaintext)
- bin_plaintext_block = self.group_by_64bit(bin_plaintext)
- ciphertext_bin_list = []
- key_list = self.get_key_list()
- for i in range(16):
- print(f'第{i + 1}轮的加密子密钥为:{key_list[i]}', ' ', len(key_list[i]), '位')
- for block in bin_plaintext_block:
- # 初代ip置换
- sub_ip = self.replace(block, self.ip)
- ite_result = self.iteration(sub_ip, key_list)
- # ip逆置换
- sub_ip1 = self.replace(ite_result, self.ip1)
- ciphertext_bin_list.append(sub_ip1)
- ciphertext_bin = ''.join(ciphertext_bin_list)
- result = self.__bin2int(ciphertext_bin)
- return bytes(result).hex().upper()
-
- def decode(self, ciphertext):
- '''
- :param ciphertext: 密文字符串
- :return: 明文字符串
- '''
- b_ciphertext = binascii.a2b_hex(ciphertext)
- bin_ciphertext = self.__int2bin(list(b_ciphertext))
- bin_plaintext_list = []
- key_list = self.get_key_list()
- key_list = key_list[::-1]
- for i in range(16):
- print(f'第{i + 1}轮的解密子密钥为:{key_list[i]}', ' ', len(key_list[i]), '位')
- bin_ciphertext_block = [bin_ciphertext[i:i + 64] for i in range(0, len(bin_ciphertext), 64)]
- for block in bin_ciphertext_block:
- sub_ip = self.replace(block, self.ip)
- ite = self.iteration(sub_ip, key_list)
- sub_ip1 = self.replace(ite, self.ip1)
- bin_plaintext_list.append(sub_ip1)
- bin_plaintext = ''.join(bin_plaintext_list).replace('00000000', '')
- return self.str_encode(bin_plaintext)
-
- def main(self):
- select = input('请选择想要进行的操作:\n1、加密\t 2、解密\t 3、退出程序\n选择:')
- if select == '1':
- plaintext = input('请输入要加密的明文:')
- ciphertext = self.encode(plaintext)
- print(f'生成的密文为:{ciphertext}')
- elif select == '2':
- plaintext = input('请输入要解密的密文:')
- plaintext = self.decode(plaintext)
- print(f'对应的明文为:{plaintext}')
- # print(len(plaintext))
- elif select == '3':
- raise SystemExit(0) # 退出程序,返回状态码0
- else:
- input('输入不合法,请重新选择!')
- self.main()
-
-
- if __name__ == '__main__':
- MyDes = DES()
- MyDes.modify_default_k()
- while True:
- MyDes.main()
- print("")
-
可以改进的地方:DES算法加密和解密的过程是一样的,因此在encode()与decode()两个代码块中出现了大量的重合,可以把重合的部分单独写一个公共部分的函数,在加密解密时直接调用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。