-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathancient_ciphers.py
executable file
·141 lines (110 loc) · 4.6 KB
/
ancient_ciphers.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
# Gravity Falls Ciphers / Шифры Гравити Фолз
# Caesar, Atbash, Vigenere for kids
#
# -----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# zmey20000@yahoo.com wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return Mikhail Zakharov
# -----------------------------------------------------------------------------
#
# 2018.01.14 v0.1 Mikhail Zakharov <zmey20000@yahoo.com> Initial release
# 2018.01.26 v0.1.1 Mikhail Zakharov <zmey20000@yahoo.com> Caesar encode
#
import sys
abc_dictionary = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'.lower()
# abc_dictionary = 'abcdefghijklmnopqrstuvwxyz'.lower()
caesar_shift = 23
# ----------------------------------------------------------------------------------------------------------------------
def dictionary_caesar(abc, shift=4):
"""Creates Caesar dictionary"""
abcl = len(abc)
caesar_abc = ''
for pos_abc in range(0, abcl):
pos_caesar = pos_abc + shift
if pos_caesar > abcl - 1:
pos_caesar = pos_caesar - abcl
caesar_abc += abc[pos_caesar]
return caesar_abc
def dictionary_atbash(abc):
"""Creates Atbash dictionary"""
return abc[::-1]
def cipher_ca(text, d1, d2):
"""Caesar or Atbash encoding/decoding. Supply both dictionaries via d1 and d2"""
result = ''
for c in text:
if c not in d1:
result += c
else:
result += d2[d1.index(c)]
return result
def vigenere(abc, text, secret, decrypt=False):
"""Simple Vigenere encoder/decoder over the dictionary"""
ignore = ' ,.!?-+='
abcl = len(abc)
result = ''
ti = []
si = []
for t in text:
if t in ignore:
ti.append(t) # Do not encode punctuation marks
else:
ti.append(abc.index(t))
for s in secret:
si.append(abc.index(s))
ps = 0
for pt in range(len(text)):
if ps == len(si):
ps = 0
if str(ti[pt]) in ignore:
result += ti[pt] # Pass punctuation mark transparently
else:
if decrypt:
result += abc[(ti[pt] - si[ps] + abcl) % abcl]
else:
result += abc[(ti[pt] + si[ps]) % abcl]
ps += 1
return result
def usage():
print('Помощь:\n'
'\tancient_ciphers.py a "Текст"\n'
'\tancient_ciphers.py c "Текст" [закодировать]\n'
'\tancient_ciphers.py v "Текст" "Ключ" [закодировать]\n'
'Укажи "а", "c" или "v" чтобы выбрать метод кодирования: Атбаш, Цезаря или Виженера\n'
'Чтобы зашифровать послание методом Цезаря или Виженера, добавь "закодировать" в конце команды')
sys.exit(1)
# ----------------------------------------------------------------------------------------------------------------------
if len(sys.argv) > 2:
cipher = sys.argv[1].lower()
text = sys.argv[2].lower()
else:
usage()
if cipher == 'a':
encrypted_dictionary = dictionary_atbash(abc_dictionary)
print('Код Атбаш:', cipher_ca(text, encrypted_dictionary, abc_dictionary))
elif cipher == 'c':
if len(sys.argv) == 3:
# We want Caesar to be decrypted
encrypted_dictionary = dictionary_caesar(abc_dictionary, caesar_shift)
elif len(sys.argv) == 4:
# We want Caesar encryption
encrypted_dictionary = dictionary_caesar(abc_dictionary, len(abc_dictionary) - caesar_shift)
print('Код Цезаря:', cipher_ca(text, encrypted_dictionary, abc_dictionary))
elif cipher == 'v':
if len(sys.argv) == 3:
print('Вы забыли указать ключ для шифра Виженера!')
usage()
secret = sys.argv[3].lower()
if len(sys.argv) == 4:
print('Код Виженера:', vigenere(abc_dictionary, text, secret, decrypt=True))
elif len(sys.argv) == 5:
action = sys.argv[4].lower()
if action:
# We want Vigenere encryption
print('Код Виженера:', vigenere(abc_dictionary, text, secret, decrypt=False))
else:
usage()
else:
print('Укажи "а", "c" или "v" чтобы выбрать метод кодирования')
usage()