轉發注明出處:http://www.cnblogs.com/0zcl/p/6106513.html
前言:
hill密碼算法我打算簡要介紹就好,加密矩陣我用教材上的3*3矩陣,只做了加密,解密沒有做,不過我覺得會加密就會解密的~~

一、hill算法原理
hill密碼是一種多字母替代密碼,由數學學Leste Hill於1929年研制成功。該密碼算法取m個連續的明文字母,並用m個密文字母代替,用向量或矩陣表示為(這里取m=3,C和P是長度為3的列向量,K是3*3矩陣):

即:C=KP (C為密文,P為明文,K為密鑰矩陣)
PS:加密操作要執行模26運算
二、加密規則
加密規則也不難的,就是有個矩陣運算(忘了可以谷哥一下,和線代有關~)
- 對明文進行分組,每3個字母一組,不足則字母Z(我設定的,下面編程也是補Z)
- 進行矩陣運算,對每組字母求密文
舉例:
對pay more money進行加密,明文的前3個字母表示為:pay=(15 0 24)T
計算密文的過程:K(15 0 24)T=(375 819 486)Tmod 26=(11 13 18)T=LNS
依此類推,可得密文為LNS HDL EWM TRW
三、編程與思路
思路請看我畫的流程圖,網址http://processon.com/diagraming/583aff30e4b086d1e7d3b617

源代碼
1 #加密密鑰矩陣 2 K_LIST = [[17, 17, 5], 3 [21, 18, 21], 4 [2, 2, 19]] 5 6 #26個字母列表:方便找出對應下標 7 ALPHABET = ["A","B","C","D","E","F","G", 8 "H","I","J","K","L","M","N", 9 "O","P","Q","R","S","T","U", 10 "V","W","X","Y","Z"] 11 12 13 def get_index(alphabet): 14 """ 15 獲得字母在字母表中的對應位置(下標) 16 :param alphabet: 明文字母 17 :return: 下標 18 """ 19 alphabet = alphabet.upper() 20 return ALPHABET.index(alphabet) 21 22 23 def deal_index(list_index): 24 """ 25 加密處理C=KP 26 :param list_index: 每一組明文字母的下標 27 :return: 加密后密文的下標 28 """ 29 deal_list = [0,0,0] 30 for i in range(len(K_LIST)): 31 for j in range(len(K_LIST[i])): 32 deal_list[i] += list_index[j] * K_LIST[i][j] 33 deal_list[i] = (deal_list[i] % 26) 34 return deal_list 35 36 37 def get_alphabet(deal_list): 38 """ 39 通過字母的下標獲得對應字母 40 :param deal_list: 下標的列表 41 :return: 返回密文字母列表 42 """ 43 cipher_list = [] 44 for i in deal_list: 45 cipher_list.append(ALPHABET[i]) 46 return cipher_list 47 48 49 def encryption(clear_text): 50 """ 51 加密時調用的函數 52 :param clear_text:輸入的明文 53 :return: 加密后的密文 54 """ 55 list_clear_text = list(clear_text.strip().replace(" ", "")) 56 print(list_clear_text) 57 #明文每3個一組,不足則補充字母Z 58 for i in range(len(list_clear_text)): 59 if i % 3 == 0 and i+2 > len(list_clear_text)-1: # 越界,則需在最后一組不足3個補字母Z 60 if i+1 > len(list_clear_text)-1: 61 list_clear_text.insert(i + 1, "Z") 62 list_clear_text.insert(i + 2, "Z") 63 print(list_clear_text) 64 cipher_list = [] #用來存入密文 65 #明文每3個為一組,找出每組在字母表中的位置(用一個列表來保存) 66 for i in range(len(list_clear_text)): 67 if i % 3 == 0 and i+2 <= len(list_clear_text)-1: 68 x = get_index(list_clear_text[i]) 69 y = get_index(list_clear_text[i+1]) 70 z = get_index(list_clear_text[i+2]) 71 list_index = [x, y, z] 72 print(list_index) 73 #調用deal_inde函數進行加密 矩陣K與明文P運算得到密文C,即C=KP 74 deal_list = deal_index(list_index) 75 #print(deal_list) #測試用的 76 part_cipher_list = get_alphabet(deal_list) #返回一組密文 77 cipher_list.extend(part_cipher_list) 78 #print(part_cipher_list) #測試用的 79 80 print(cipher_list) 81 return "".join(cipher_list) 82 83 84 def decryption(): 85 print("解密未實現...") 86 87 88 if __name__ == "__main__": 89 while True: 90 choice = input("Please input E for encryption or D for decryption:") 91 if choice == "E": 92 clear_text = input("請輸入明文:") 93 print("加密成功!密文:%s" % encryption(clear_text)) 94 if choice == "D": 95 cipher_text = input("請輸入密文:") 96 decryption()
測試
1 Please input E for encryption or D for decryption:E 2 請輸入明文:pay more money 3 ['p', 'a', 'y', 'm', 'o', 'r', 'e', 'm', 'o', 'n', 'e', 'y'] 4 ['p', 'a', 'y', 'm', 'o', 'r', 'e', 'm', 'o', 'n', 'e', 'y'] 5 [15, 0, 24] 6 [12, 14, 17] 7 [4, 12, 14] 8 [13, 4, 24] 9 ['L', 'N', 'S', 'H', 'D', 'L', 'E', 'W', 'M', 'T', 'R', 'W'] 10 加密成功!密文:LNSHDLEWMTRW 11 Please input E for encryption or D for decryption:payp 12 Please input E for encryption or D for decryption:E 13 請輸入明文:payy 14 ['p', 'a', 'y', 'y'] 15 ['p', 'a', 'y', 'y', 'Z', 'Z'] 16 [15, 0, 24] 17 [24, 25, 25] 18 ['L', 'N', 'S', 'W', 'X', 'B'] 19 加密成功!密文:LNSWXB 20 Please input E for encryption or D for decryption:D 21 請輸入密文:LNSWXB 22 解密未實現... 23 Please input E for encryption or D for decryption:
