https://www.cnblogs.com/longhai3/longhai

PYTHON>>维吉尼亚密码表

Posted on 2022-02-13 00:54  凡是过去,皆为序曲  阅读(330)  评论(0编辑  收藏  举报
 1 #
 2 
 3 def biao():
 4     lista = list(range(0,26))
 5     for i in range(0,26):
 6         lista[i] = list(range(0,26))
 7     return lista
 8 # print(biao())
 9 def biao02():
10     lista = biao()
11     a = 0
12     for i in range(0,26):
13         for j in range(0,26):
14             if lista[i][j] + a + 97 > 97 + 25 :
15                 lista[i][j] = chr(lista[i][j] + a + 97 - 26)
16             else:
17                 lista[i][j] = chr(lista[i][j] + a + 97)
18         a = a + 1
19     return lista
20 
21 struse = input("输入明文")
22 strmiyao = input("输入加密密钥")
23 a = len(struse)
24 b = len(strmiyao)
25 c = a % b
26 strmiyao = strmiyao * int(a/b) + strmiyao[0:c]
27 strlistuse = list(struse)
28 strlistmiyao = list(strmiyao)
29 print(strlistuse)
30 print(strlistmiyao)
31 
32 
33 
34 zidian = {"a":0,"b":1,"c":2,"d":3,"e":4,"f":5,"g":6,"h":7,"i":8,"j":9,"k":10,"l":11,"m":12,"n":13,"o":14,"p":15,"q":16,"r":17,"s":18,"t":19,"u":20,"v":21,"w":22,"x":23,"y":24,"z":25}
35 
36 alist = biao02()
37 strmiwen = ""
38 for i in range(a):
39     a1 = zidian[strlistuse[i]]
40     b1 = zidian[strlistmiyao[i]]
41     strmiwen0 = alist[a1][b1]
42     strmiwen = strmiwen + strmiwen0
43 print(strmiwen)
44 
45 # ===========================以上加密(无空格),以下解密(有空格)====================================================
46 
47 zidian = {"a":0,"b":1,"c":2,"d":3,"e":4,"f":5,"g":6,"h":7,"i":8,"j":9,"k":10,"l":11,"m":12,"n":13,"o":14,"p":15,"q":16,"r":17,"s":18,"t":19,"u":20,"v":21,"w":22,"x":23,"y":24,"z":25}
48 
49 miwenB = input("输入密文")
50 strmiyao = input("输入加密密钥")
51 
52 strming0 = ""
53 strmiyaoW0 = 0
54 strmiyao0 = ""
55 for i in miwenB:
56     if 97 <= ord(i) <= 122:
57         strmiyaoW0 = strmiyaoW0 + 1
58         strmiyao0 = strmiyao0 + strmiyao[strmiyaoW0-1:strmiyaoW0]
59         strmiyaoW0 = strmiyaoW0 % len(strmiyao)
60     elif 65 <= ord(i) <=90:
61         i = chr(ord(i)+32)
62         strmiyaoW0 = strmiyaoW0 + 1
63         strmiyao0 = strmiyao0 + strmiyao[strmiyaoW0-1:strmiyaoW0]
64         strmiyaoW0 = strmiyaoW0 % len(strmiyao)
65     else:
66         strmiyao0 = strmiyao0 + " "
67     strming0 = strming0 + i
68 
69 fininin0 = ""
70 for d in range(len(strmiyao0)):
71     i = strming0[d]
72     j = strmiyao0[d]
73     if i == " ":
74         fininin0 = fininin0 + " "
75         continue
76     else:
77         if zidian[i] < zidian[j]:
78             a = zidian[i] + 26 - zidian[j] + 97
79             fininin0 = fininin0 + chr(a)
80         else:
81             a = zidian[i] - zidian[j] + 97
82             fininin0 = fininin0 + chr(a)
83 print(fininin0)

 

随心,随记

https://www.cnblogs.com/w1hg/331817