-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher.py
83 lines (69 loc) · 2.29 KB
/
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
73
74
75
76
77
78
79
80
81
82
83
import re
from itertools import cycle
from conf import alpha_a, alpha_b, key_len, rotations, num
from termcolor import cprint
DEBUG = True
TEST = True
if TEST:
MSG = 'Mijn code werkt gewoon!'
KEY = '538'
def rotation_calc(input_key, rotor=1):
"""Returns a new key based on the rotations"""
for r in range(0, int(key_len)):
key = []
for i in input_key:
i = int(i)/int(key_len)
i = round(i)
key.append(i)
return key
def converter(l, k, state):
"""
Gets the corresponding number for the letter from the `alpha` dict, adds the
number with the `k` value and returns the corresponding letter.
"""
initial_value = alpha_a[l]
if state == 'encrypt':
new_value = initial_value + int(k)
if new_value > num:
new_value = new_value-num
if DEBUG:
cprint(f'converter ==> set to {new_value} == {alpha_b[new_value]}', 'yellow')
if state == 'decrypt':
new_value = initial_value - int(k)
if new_value < 0:
new_value = new_value+num
if DEBUG:
cprint(f'converter ==> set to {new_value} == {alpha_b[new_value]}', 'yellow')
if DEBUG:
cprint(f'\t{l} =---> {alpha_b[new_value]}', 'cyan')
return alpha_b[new_value]
def sanitize(msg):
m = re.sub(' ' , '', msg.upper())
ret = re.sub(r'[^\w]', '', m)
if DEBUG:
cprint(f'Sanitized: {ret}', 'blue')
return ret
def encrypt(msg, key, state, rotor=rotations):
tmp = []
m = sanitize(msg)
key = cycle(key)
for l in m:
k = next(key)
tmp.append(converter(l, k, state))
return ''.join(tmp)
def decrypt(msg, key, state, rotor=rotations):
tmp = []
key = cycle(key)
for l in msg:
k = next(key)
tmp.append(converter(l, k, state))
return ''.join(tmp)
if TEST:
cprint(f'Initial msg: "{MSG}"', 'green')
cprint(f'Key: {KEY}', 'green')
e = encrypt(msg=MSG, key=KEY, rotor=0, state='encrypt')
cprint(f'Encrypted output: {e}', 'white', 'on_red', attrs=['bold'])
d = decrypt(msg=e, key=KEY, rotor=0, state='decrypt')
cprint(f'Decrypted output: {d}', 'white', 'on_blue', attrs=['bold'])
k = rotation_calc(input_key=KEY, rotor=2)
cprint(f'New key: {k}, based on 2 rotations', 'yellow')