要求:
1 實現指定秘鑰的凱撒密碼加密
2 已知秘鑰解密
3 未知秘鑰,采用暴力破解
4 對破解出的密碼,與單詞詞典比對,找出明文的那一條,得到加密的秘鑰
采用python實現
報告+代碼鏈接
鏈接:https://pan.baidu.com/s/1aOIhnLFIy36Y05AnrCz8pw
提取碼:zulz
Caesar_encryption.py
MAX_KEY_SIZE = 26 def getMode(): while True: print('請選擇加密或解密模式,或者選擇暴力破解:') print('加密:encrypt(e)') print('解密:decrypt(d)') print('暴力破解:brute(b)') mode = input().lower() if mode in 'encrypt e decrypt d brute b'.split(): return mode else: print('請輸入"encrypt"或"e"或"decrypt"或"d"或"brute"或"b"!') def getMessage(): print('請輸入你的信息:') return input() def getKey(): key = 0 while True: print('請輸入密鑰數字(1-25)') key = int(input()) if (key >=1 and key <= MAX_KEY_SIZE): return key def getTranslatedMessage(mode, message, key): if mode[0] == 'd': key = -key #如果是解密的 就反過來 translated = '' for symbol in message: if symbol.isalpha(): #是否都是字母 num = ord(symbol) #得到字符symbol對應的ASCII碼 num += key #解密出對應的ASCII碼 if symbol.isupper(): if num > ord('Z'): num -= 26 elif num < ord('A'): num += 26 elif symbol.islower(): if num > ord('z'): num -= 26 elif num < ord('a'): num += 26 translated += chr(num) #與ord對應 將碼轉字符 else: translated += symbol #不是字母的位置就直接添加了 write_file(translated,"cipher.txt") return translated def write_file(str,filename): with open(filename,'w') as fp: fp.write(str) mode = getMode() message = getMessage() if mode[0] != 'b': key = getKey() print('你要翻譯的信息是:') if mode[0] != 'b': print(getTranslatedMessage(mode, message, key)) else: for key in range(1, MAX_KEY_SIZE + 1): print(key, getTranslatedMessage('decrypt', message, key))
Caesar_ Decrypt.py
########################功能####################### #1、文件讀入4萬個常用單詞、密文 #2、解密后存文件 #3、cipher 是存的原始密文 cipher_no 是把原始密文的標點符號過濾后的密文 ########################程序########################### import time #存放100常用單詞的文本 WORDS_FILE_NAME = 'diction.txt' #WORDS_FILE_NAME = 'words.txt' #加密所用的對照表 KEY_FILE_NAME = 'duizhao.txt' #存放密文的文本 CIPHER_FILE_NAME = 'cipher.txt' #存放明文的文本 PLAIN_FILE_NAME = 'plain.txt' letters = 'abcdefghijklmnopqrstuvwxyz' #讀入一個文本 def read_file(filename): lines = '' with open(filename,'r') as fp: for line in fp: lines += line return lines #寫入字符串到文本 def write_file(str,filename): with open(filename,'w') as fp: fp.write(str) #根據秘鑰加密 def encrypt(key,textFileString): lines = '' #讀入待加密文本 lines = read_file(textFileString) lines.lower() #讀入對應轉換表 letters1 = '' #選擇對應的轉換表 with open(KEY_FILE_NAME, 'r') as fp1: for line in fp1: key -= 1 if key == -1: #找到key那一行 letters1 += line #進行加密 cstring = '' length = len(lines) for i in range(0, length): if lines[i] >= 'a' and lines[i] <= 'z': for j in range(0, 26): if lines[i] == letters[j]: #letters = 'abcdefghijklmnopqrstuvwxyz' cstring += letters1[j] #進行相應位的替換 else: cstring += lines[i] return cstring #根據指定的秘鑰解密 def decipher1(key,textFileString): #從文件讀入數據 lines = '' cstring = ''#密文結果 lines = read_file(textFileString) #全部轉換為小寫字母 lines.lower() #根據key進行解密 letters1 = '' with open(KEY_FILE_NAME,'r') as fp1: for line in fp1: key += 1 if key == 27: letters1+=line #開始解密 length = len(lines) for i in range(0,length): if lines[i]>='a' and lines[i] <= 'z': for j in range(0, 26): if lines[i] == letters[j]: cstring+=letters1[j] else: cstring += lines[i] return cstring #根據常用詞匯表解密 def decipher2(textFileString): #讀入一百個單詞 words = read_file(WORDS_FILE_NAME).split('\n') #讀取文件所有內容 並以回車分詞 max = 0; index = 0; #暴力破解選擇一個秘鑰 filename_no = read_file(textFileString).replace(',','') filename_no = filename_no.replace('.','') filename_no = filename_no.replace(':','') filename_no = filename_no.replace('"','') filename_no = filename_no.replace('?','') write_file(filename_no,'cipher_no.txt') #符號替換完 存入_no print("暴力破解的所有結果:") for i in range(1,26): pstring = decipher1(i,'cipher_no.txt') print("秘鑰為%d"%i,"時,解密的結果:",pstring) plainWords = pstring.split(' ') #對比 length = len(plainWords) temp = 0 for j in range(0,length): if plainWords[j] in words: temp += 1 # 在詞庫里面的詞語數量 if temp > max: max = temp index = i print("經過詞典比對,最終得出正確的明文:") print('破解秘鑰是%d'%index) # print('單詞個數%d',length) print('單詞的匹配程度{}%'.format(float('%.2f'%(max*100/length)))) #寫入文件 str = decipher1(index,textFileString) write_file(str,PLAIN_FILE_NAME) return decipher1(index,textFileString) def main(): print('主程序入口') start_time = time.time() print('解密后的結果:\n'+decipher2('cipher.txt')) #解密 #print('加密后的結果:\n'+encrypt(3, 'D:\\2018\Python_Learning\pythonlearning\\plain.txt')) #加密 end_time = time.time() run_time = end_time-start_time print('程序的運行時間為:{}'.format(run_time)) if __name__ == '__main__': main()
有一些txt見網盤鏈接