-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMicroCipher (REPL).py
More file actions
157 lines (138 loc) · 6.1 KB
/
MicroCipher (REPL).py
File metadata and controls
157 lines (138 loc) · 6.1 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
# MIT License
#
# Designer: Kevin Thomas
# Developer: Kevin Thomas
#
# Copyright (c) 2021 My Techno Talent
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
class MicroCipher:
"""
Class to handle the MicroCipher engine
"""
def __init__(self, fast_wheel, medium_wheel, slow_wheel):
"""
Params:
fast_wheel: int
medium_wheel: int
slow_wheel: int
"""
self.alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
# computers are zero-indexed, we need to subtract 1, if user selects 1 it becomes 0
self.fast_wheel = fast_wheel - 1
self.medium_wheel = medium_wheel - 1
self.slow_wheel = slow_wheel - 1
wheels = [self.fast_wheel, self.medium_wheel, self.slow_wheel]
for wheel in wheels:
wheel = wheel % 26
self.fast_wheel = wheels[0]
self.medium_wheel = wheels[1]
self.slow_wheel = wheels[2]
self.reverse_wheel = [letter for letter in reversed(self.alphabet)]
def permutate(self, wheel):
"""
Method to re-order the alphabet depending on the wheels settings
Params:
wheel: int
"""
new_alphabet = ''.join(self.alphabet)
new_alphabet = list(new_alphabet)
for iter in range(wheel):
new_alphabet.insert(0, new_alphabet[-1])
new_alphabet.pop(-1)
return new_alphabet
def inverse_permutation(self, wheel):
"""
Method to inverse re-order the alphabet depending on the wheels settings
Params:
wheel: int
"""
new_alphabet = ''.join(self.alphabet)
new_alphabet = list(new_alphabet)
for iter in range(wheel):
new_alphabet.append(new_alphabet[0])
new_alphabet.pop(0)
return new_alphabet
def cipher(self, text):
"""
Method to encrypt/decrypt text
Params:
text: str
"""
cipher_text = []
text.split()
# cipher each letter
for letter in text:
# handle empty space to ignore wheel algorithm
if letter == ' ':
cipher_text.append(' ')
else:
# letter is ciphered by fast wheel
temp_letter = self.permutate(self.fast_wheel)[self.alphabet.index(letter)]
# letter is ciphered by medium wheel
temp_letter = self.permutate(self.medium_wheel)[self.alphabet.index(temp_letter)]
# letter is ciphered by slow wheel
temp_letter = self.permutate(self.slow_wheel)[self.alphabet.index(temp_letter)]
# reverse_wheel is returning the inverse of that letter
temp_letter = self.reverse_wheel[self.alphabet.index(temp_letter)]
# letter is ciphered by slow wheel, inverse algorithm
temp_letter = self.inverse_permutation(self.slow_wheel)[self.alphabet.index(temp_letter)]
# letter is ciphered by medium wheel, inverse algorithm
temp_letter = self.inverse_permutation(self.medium_wheel)[self.alphabet.index(temp_letter)]
# letter is ciphered by fast wheel, inverse algorithm
temp_letter = self.inverse_permutation(self.fast_wheel)[self.alphabet.index(temp_letter)]
# append temp letter to cipher text
cipher_text.append(temp_letter)
# wheel rotation algorithm
self.fast_wheel += 1
if self.fast_wheel % len(self.alphabet) == 0:
self.medium_wheel += 1
self.fast_wheel = 0
if self.medium_wheel % len(self.alphabet) == 0 and self.fast_wheel % len(self.alphabet) != 0 and self.medium_wheel >= len(
self.alphabet) - 1:
self.slow_wheel += 1
self.medium_wheel = 1
upper_cipher_text = ''
for letter in cipher_text:
upper_cipher_text += letter
return ''.join(upper_cipher_text.upper())
while True:
try:
fast_wheel = int(input('FAST WHEEL: '))
if fast_wheel < 1 or fast_wheel > 26:
print('Please enter a value from 1 to 26...')
break
medium_wheel = int(input('MEDIUM WHEEL: '))
if medium_wheel < 1 or medium_wheel > 26:
print('Please enter a value from 1 to 26...')
break
slow_wheel = int(input('SLOW WHEEL: '))
if slow_wheel < 1 or slow_wheel > 26:
print('Please enter a value from 1 to 26...')
break
micro_cipher = MicroCipher(fast_wheel, medium_wheel, slow_wheel)
cipher = input('CIPHER: ')
if not cipher.isupper():
print('Please enter only capital letters...')
break
print('CIPHER: ', end='')
print(micro_cipher.cipher(cipher))
except ValueError:
print('Please enter a value from 1 to 26...')
break