列置换密码
参考教材:《现代密码学教程》P46 3.1.1
过程
加密过程
- 将明文p以设定的固定分组宽度m按行写出,即每行有m个字符;若明文长度不是m的整数倍,则不足部分用双方约定的方式填充,如双方约定用空格代替空缺处字符,不妨设最后得字符矩阵[Mp]n×m
- 按1,2…,m的某一置换σ交换列的位置次序得字符矩阵[Mp]n×m
- 把矩阵按[Mp]n×m列的顺序依次读出得密文序列c。
解密过程
- 将密文c以分组宽度n按列写出得到字符矩阵[Mp]n×m
- 按加密过程用的置换σ的逆置换σ-1交换列的位置次序得字符矩阵[Mp]n×m
- 把矩阵[Mp]n×m按1,2…,n行的顺序依次读出得明文p
说明
以例3.2为例
σ=(143)(56)即每个括号里部分轮着相对应
1→4→3→1
5→6→5
这里我是用字典表示
1
2
3
4
5
6
7
|
for i in range ( len (s)): for j in range ( len (s[i]) - 1 ): Key[ int (s[i][j])] = int (s[i][j + 1 ]) #密钥字典 antiKey[ int (s[i][j + 1 ])] = int (s[i][j]) #反密钥字典 Key[ int (s[i][ - 1 ])] = int (s[i][ 0 ]) #解决最后一个的问题 antiKey[ int (s[i][ 0 ])] = int (s[i][ - 1 ]) temp.append( int (s[i][ - 1 ])) |
当然,这儿σ=(143)(56)中没有2,因为2对应他自己即
2→2
所以要再单独表示
1
2
3
4
|
sameKey = lenKey - set (temp) #找到没有变化的密钥 for i in sameKey: Key[i] = i antiKey[i] = i |
而加密的时候,便是把明文先变为矩阵,载根据密钥进行转换
比如本题便将矩阵第一列转换为第四列,第四列转换为第三列,第三列转换为第一列,以此类推
这里可以用生成式完成
1
|
M = [M[i][Key[j + 1 ] - 1 ] for i in range (n) for j in range (m)] #矩阵转换 |
完整代码
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
|
'''列置换密码 Auher:Jarrycow time:2020-04-17 ''' import re class colCode: __m = 0 __n = 0 __key = [] # 密钥 __apaMsg = "" # 明文 __secMsg = "" #密文 def __init__( self ,m): # 初始化,定义矩阵宽 self .__m = m __n = 0 __key = [] __apaMsg = "" __secMsg = "" def getKey( self ,s): # 密钥形成函数 m = self .__m Key = {} antiKey = {} s = re.split(r '[()]' ,s) #以()分界 while ' ' in s: # 消除' ' s.remove('') temp = [] lenKey = {i + 1 for i in range (m)} #密钥长度 for i in range ( len (s)): for j in range ( len (s[i]) - 1 ): Key[ int (s[i][j])] = int (s[i][j + 1 ]) #密钥字典 antiKey[ int (s[i][j + 1 ])] = int (s[i][j]) #反密钥字典 temp.append( int (s[i][j])) #钥匙收录 Key[ int (s[i][ - 1 ])] = int (s[i][ 0 ]) #解决最后一个的问题 antiKey[ int (s[i][ 0 ])] = int (s[i][ - 1 ]) temp.append( int (s[i][ - 1 ])) sameKey = lenKey - set (temp) #找到没有变化的密钥 for i in sameKey: Key[i] = i antiKey[i] = i self .__key.append(Key) self .__key.append(antiKey) def enCode( self ,p): #加密函数 self .__apaMsg = p m = self .__m n = self .__n Key = self .__key[ 0 ] p = p.replace( ' ' ,'') #去除空格 p + = ' ' * (m - len (p) % m) #末尾补齐 n = len (p) / / m #矩阵列数 self .__n = n M = [p[i * m:(i + 1 ) * m] for i in range (n)] #矩阵生成 M = [M[i][Key[j + 1 ] - 1 ] for i in range (n) for j in range (m)] #矩阵转换 M = ''.join(M) #列表转换为字符串 self .__secMsg = M return M def deCode( self ,q): self .__apaMsg = p m = self .__m n = self .__n Key = self .__key[ 1 ] M = [q[i * m:(i + 1 ) * m] for i in range (n)] M = [M[i][Key[j + 1 ] - 1 ] for i in range (n) for j in range (m)] M = ''.join(M) self .__secMsg = M return M def Print ( self ): print ( self .__m, self .__n, self .__key, self .__apaMsg, self .__secMsg) if __name__ = = '__main__' : m = 6 p = "Beijing 2008 Olympic Games" s = '(143)(56)' a = colCode(m) a.getKey(s) q = a.enCode(p) e = a.deCode(q) a. Print () def main(): pass |
以上就是python密码学列置换密码学习的详细内容,更多关于python密码学列置换密码的资料请关注服务器之家其它相关文章!
原文链接:https://blog.csdn.net/Jarrycow/article/details/105571651