1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
import re import string import easygui as ezgui def simple_replace(password, replace_word1, replace_word2, replace_word3): count = 0 new_pass = '' ori_table = 'abcdefghijklmnopqrstuvwxyz' for obj in password: table1 = str.maketrans(ori_table, replace_word1) table2 = str.maketrans(ori_table, replace_word2) table3 = str.maketrans(ori_table, replace_word3) new_obj = str.translate(obj, table1) new_obj = str.translate(new_obj, table2) new_obj = str.translate(new_obj, table3) new_obj = reverse_word(new_obj) reverse_table1 = str.maketrans(replace_word1, ori_table) reverse_table2 = str.maketrans(replace_word2, ori_table) reverse_table3 = str.maketrans(replace_word3, ori_table) new_obj = str.translate(new_obj, reverse_table3) new_obj = str.translate(new_obj, reverse_table2) new_obj = str.translate(new_obj, reverse_table1) new_pass += new_obj replace_word1 = rotors(replace_word1) count += 1 if count % 676 == 0: replace_word3 = rotors(replace_word3) elif count % 26 == 0: replace_word2 = rotors(replace_word2) return new_pass def is_str(password, replace_word1, replace_word2, replace_word3): an = re.match('[a-z]+$', password) if not type(password) == type(replace_word1) == type(replace_word2) == type(replace_word3) == type('a'): print('密码必须是字符串!') return False elif not an: print('字符串只能包含小写字母!') return False elif not len(replace_word1) == len(replace_word2) == len(replace_word3) == 26: print('替换码必须为26个字母!') return False else: return True def rotors(replace_word): return replace_word[1:] + replace_word[0]
def reverse_word(word): dic = {'a': 'n', 'b': 'o', 'c': 'p', 'd': 'q', 'e': 'r', 'f': 's', 'g': 't', 'h': 'u', 'i': 'v', 'j': 'w', 'k': 'x', 'l': 'y', 'm': 'z', 'n': 'a', 'o': 'b', 'p': 'c', 'q': 'd', 'r': 'e', 's': 'f', 't': 'g', 'u': 'h', 'v': 'i', 'w': 'j', 'x': 'k', 'y': 'l', 'z': 'm'} return dic[word] while True: title = 'by 石光k一5' ezgui.msgbox('这是恩尼格码密码机',title) a_password = ezgui.enterbox('请输入明文加密或密文解密:',title) r_password1 = 'qwertyuiopasdfghjklzxcvbnm' r_password2 = 'asdfqwerzxcvtyuiopghjklbnm' r_password3 = 'poiuytrewqasdfghjklmnbvcxz' if is_str(a_password, r_password1, r_password2, r_password3): msg=f'密文/明文如下:{simple_replace(a_password, r_password1, r_password2, r_password3)}' ezgui.textbox(msg,title)
|