-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypt.py
More file actions
205 lines (160 loc) · 5.35 KB
/
crypt.py
File metadata and controls
205 lines (160 loc) · 5.35 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os
from Globals import Global
CHARS = Global.CHARS
CHARS_LEN = Global.CHARS_LEN
def file_check(infile, outfile):
if os.path.isfile(infile):
if os.path.isfile(outfile):
if os.path.samefile(infile, outfile):
raise ValueError('Input and output files are the same.')
else:
raise ValueError('Error while writing file.')
else:
raise ValueError('Error while reading file.')
if not os.path.getsize(infile):
raise ValueError('Input file is empty.')
if os.path.getsize(outfile):
raise ValueError('Output file is not empty.')
def key_check(key):
if os.path.isfile(key):
if not os.path.getsize(key):
raise ValueError('Key file is empty.')
else:
raise ValueError('Error while reading file.')
def caesar_encode(infile, outfile, key):
file_check(infile, outfile)
source = open(infile, 'r', encoding='utf8')
result = open(outfile, 'w', encoding='utf8')
while True:
symbol = source.read(1).lower()
if not symbol:
break
if symbol not in CHARS:
result.write(symbol)
continue
result.write(CHARS[(CHARS.find(symbol) + key) % CHARS_LEN])
source.close()
result.close()
def caesar_decode(infile, outfile, key):
file_check(infile, outfile)
source = open(infile, 'r', encoding='utf8')
result = open(outfile, 'w', encoding='utf8')
while True:
symbol = source.read(1).lower()
if not symbol:
break
if symbol not in CHARS:
result.write(symbol)
continue
result.write(CHARS[(CHARS.find(symbol) - key) % CHARS_LEN])
source.close()
result.close()
def vigenere_encode(infile, outfile, key_word):
file_check(infile, outfile)
source = open(infile, 'r', encoding='utf8')
result = open(outfile, 'w', encoding='utf8')
key_len = len(key_word)
i = 0
while True:
symbol = source.read(1).lower()
if not key_word:
raise ValueError('Key is empty.')
if not symbol:
break
if symbol not in CHARS:
result.write(symbol)
continue
symbol_idx = (CHARS.find(symbol)
+ CHARS.find(key_word[i].lower())) % CHARS_LEN
result.write(CHARS[symbol_idx])
i = (1 + i) % key_len
source.close()
result.close()
def vigenere_decode(infile, outfile, key_word):
file_check(infile, outfile)
source = open(infile, 'r', encoding='utf8')
result = open(outfile, 'w', encoding='utf8')
key_len = len(key_word)
i = 0
while True:
symbol = source.read(1).lower()
if not key_word:
raise ValueError('Key is empty.')
if not symbol:
break
if symbol not in CHARS:
result.write(symbol)
continue
symbol_idx = (CHARS.find(symbol)
- CHARS.find(key_word[i].lower())) % CHARS_LEN
result.write(CHARS[symbol_idx])
i = (1 + i) % key_len
source.close()
result.close()
def vernam_encode(infile, outfile, key):
file_check(infile, outfile)
key_check(key)
source = open(infile, 'r', encoding='utf8')
result = open(outfile, 'w', encoding='utf8')
key_file = open(key, 'r', encoding='utf8')
symbol = source.read(1).lower()
letter = key_file.read(1).lower()
while True:
if symbol not in CHARS:
result.write(symbol)
symbol = source.read(1).lower()
continue
if letter not in CHARS:
letter = key_file.read(1).lower()
continue
if bool(letter) ^ bool(symbol):
raise EOFError('EOF reached.')
if not symbol or not letter:
break
result.write(CHARS[(CHARS.find(symbol) + CHARS.find(letter))
% CHARS_LEN])
symbol = source.read(1).lower()
letter = key_file.read(1).lower()
source.close()
result.close()
key_file.close()
def vernam_decode(infile, outfile, key):
file_check(infile, outfile)
key_check(key)
source = open(infile, 'r', encoding='utf8')
result = open(outfile, 'w', encoding='utf8')
key_file = open(key, 'r', encoding='utf8')
symbol = source.read(1).lower()
letter = key_file.read(1).lower()
while True:
if symbol not in CHARS:
result.write(symbol)
symbol = source.read(1).lower()
continue
if letter not in CHARS:
letter = key_file.read(1).lower()
continue
if bool(letter) ^ bool(symbol):
raise EOFError('EOF reached.')
if not symbol or not letter:
break
result.write(CHARS[(CHARS.find(symbol) - CHARS.find(letter))
% CHARS_LEN])
symbol = source.read(1).lower()
letter = key_file.read(1).lower()
source.close()
result.close()
key_file.close()
def analyses(infile, outfile):
file_check(infile, outfile)
letters = [0 for _ in range(CHARS_LEN)]
source = open(infile, 'r', encoding='utf8')
while True:
symbol = source.read(1).lower()
if not symbol:
break
if symbol in CHARS:
letters[CHARS.find(symbol)] += 1
key = letters.index(max(letters)) - CHARS.find('e')
caesar_decode(infile, outfile, key)
source.close()