test: 博客美化中……

信息安全-2:python之hill密码算法[原创]

 

转发注明出处: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运算

 

 二、加密规则

加密规则也不难的,就是有个矩阵运算(忘了可以谷哥一下,和线代有关~)

  1. 对明文进行分组,每3个字母一组,不足则字母Z(我设定的,下面编程也是补Z)
  2. 进行矩阵运算,对每组字母求密文

举例:

对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()
View Code

 

测试

 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:
View Code

 

posted @ 2016-11-28 00:20  前程明亮  阅读(6153)  评论(0编辑  收藏  举报