-
Notifications
You must be signed in to change notification settings - Fork 0
/
shift_cipher.py
72 lines (69 loc) · 1.94 KB
/
shift_cipher.py
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
# mã hóa vòng
import string
# mã hóa
plantext = input("Input plantext: ")
key = int(input("Input key: "))
cipher = ""
print("Symbol", end = " | ")
P_index = []
E_x = []
for symbol in plantext:
print("{:<3}".format(symbol), end = "|")
if symbol.isupper():
index = string.ascii_uppercase.find(symbol)
P_index.append(index)
index = (index + key) % 26
E_x.append(index)
cipher += string.ascii_uppercase[index]
elif symbol.islower():
index = string.ascii_lowercase.find(symbol)
P_index.append(index)
index = (index + key) % 26
E_x.append(index)
cipher += string.ascii_lowercase[index]
else:
P_index.append(symbol)
E_x.append(symbol)
cipher += symbol
print("\nIndex ", end = " | ")
for i in P_index:
print("{:<3}".format(i),end = "|")
print("\nE(x) ",end = " | ")
for j in E_x:
print("{:<3}".format(j), end = "|")
print("\nCipher", end = " | ")
for c in cipher:
print("{:<3}".format(c), end = "|")
print("\n____\n____")
# giải mã
decode = ""
print("Symbol", end = " | ")
C_index = []
E_y = []
for symbol in cipher:
print("{:<3}".format(symbol), end = "|")
if symbol.isupper():
index = string.ascii_uppercase.find(symbol)
C_index.append(index)
index = (index - key) % 26
E_y.append(index)
decode += string.ascii_uppercase[index]
elif symbol.islower():
index = string.ascii_lowercase.find(symbol)
C_index.append(index)
index = (index - key) % 26
E_y.append(index)
decode += string.ascii_lowercase[index]
else:
C_index.append(symbol)
E_y.append(symbol)
decode += symbol
print("\nIndex ", end = " | ")
for i in C_index:
print("{:<3}".format(i),end = "|")
print("\nE(x) ",end = " | ")
for j in E_y:
print("{:<3}".format(j), end = "|")
print("\nCipher", end = " | ")
for c in decode:
print("{:<3}".format(c), end = "|")