forked from Silver-Taurus/Project-Sipher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ciphers.py
executable file
·289 lines (249 loc) · 11 KB
/
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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#! /usr/bin/env python3
''' Project Sipher '''
import math
import string
import random
from Crypto.Util.number import isPrime
from cipher_sub_routines import CipherSubRoutine
from cipher_sub_routines import single_cipher_dispatch
@single_cipher_dispatch
def get_cipher_func(cipher):
return KeyError('No Such Cipher Exists!!!')
def safe_run(func):
''' A decorator sub-routine for handling exceptions '''
def decorated_function(*args, **kwargs):
while True:
try:
return func(*args, **kwargs)
except KeyError or TypeError:
print(f'{CipherSubRoutine.exceptions[func.__name__]}!!!\n')
except ValueError:
print('Invalid Literal!!!\n')
else:
break
return decorated_function
class Cipher(CipherSubRoutine):
''' Class for performing the cipher methods on a given text '''
# Cipher Constructor for taking input of text and passing it to CipherSubRoutine Constructor.
def __init__(self):
self._text = input('\nEnter the text: ').lower()
self._length = len(self._text)
super().__init__(self._text, self._length)
#---------------------------- Properties (Getters and Setters) --------------------------------
@property
def text(self):
return self._text
@text.setter
def text(self, _):
print('\nWarning... Inititalised text is not meant to be changed manually!!!\n')
@property
def length(self):
return self._length
@length.setter
def length(self, _):
print('\nWarning... Internally Calculated length is not meant to be changed manually!!!\n')
#---------------------------- Main Methods ----------------------------------------------------
def _primary_cipher_routine(self, mode):
''' primary cipher routine for applying the cipher algorithm with the defined mode legally '''
cipher_menu = {}
print('\n\nCipher list:')
for n, fn_name in enumerate(get_cipher_func.registry.keys(), 1):
if n != 1:
print(f'{n-1}. {fn_name}')
cipher_menu[n-1] = fn_name
choice = int(input('\nEnter Your Choice: '))
print(f'\nThe {mode}d string is:', get_cipher_func(cipher_menu[1])(self) if choice == 1 \
else get_cipher_func(cipher_menu[choice])(self, mode))
def encode(self):
''' Encode-Routine for Encoding the plaintext into ciphertext '''
self._primary_cipher_routine('encode')
def decode(self):
''' Decode-Routine for Decoding the ciphertext into plaintext '''
self._primary_cipher_routine('decode')
def hack(self):
''' Hack-Routine for Hacking the ciphertext without key(s) into plaintext '''
pass
#---------------------------- Primary Cipher Routines -----------------------------------------
@get_cipher_func.register('Reverse Cipher')
def _reverse_cipher(self):
''' Cipher Routine to encode into or decode from Reverse Cipher '''
return self.text[::-1]
@get_cipher_func.register('Caesar Cipher')
@safe_run
def _caesar_cipher(self, mode):
''' Cipher Routine to encode into or decode from Caesar Cipher '''
key = int(input('(integer >0 and not a multiple of 26)\nEnter the key: '))
if key < 1 or key%26 == 0:
raise KeyError
if mode == 'encode':
return self._caesar_sub_routine(key)
else:
return self._caesar_sub_routine(26-key)
@get_cipher_func.register('Transposition Cipher')
@safe_run
def _transposition_cipher(self, mode):
''' Cipher Routine to encode into or decode form Transposition Cipher '''
key = int(input(f'(integer >2 and < {self.length})\nEnter the key: '))
if (key < 2) or (key >= self.length):
raise KeyError
return self._transposition_sub_routine(key, mode)
@get_cipher_func.register('Affine Cipher')
@safe_run
def _affine_cipher(self, mode):
''' Cipher Routine to encode into or decode from Affine Cipher '''
key_a = key_b = 0
if mode == 'encode':
while True:
choice = input("Enter key Automatically/Manually [A/m]: ")
if choice == 'A' or choice == 'a' or choice == '':
while True:
key_a = random.randint(1, 10**5)
if math.gcd(key_a, 26) == 1 :
break
key_b = random.randint(1, 10**5)
break
elif choice == 'm' or choice == 'M':
n = int(input('Range 1 to n from which a valid key is choosen\nEnter n'
'(should be greater than 1): '))
if n < 2:
raise KeyError
keys_a = []
for i in range(1, n):
if math.gcd(i,26) == 1 :
print(i, end = ' ')
keys_a.append(i)
key_a = int(input('\nEnter key-a: '))
if key_a not in keys_a:
raise KeyError
key_b = int(input('Enter key-b: '))
if key_b < 0:
raise KeyError
break
else:
print('Invalid Choice!!!')
print(f'\nkey-a = {key_a}\nkey-b = {key_b}')
else:
key_a = int(input('\n(integer >1 and should be co-prime with 26)\nEnter the key-a: '))
if key_a < 2 or math.gcd(key_a, 26) != 1:
raise KeyError
key_b = int(input('(positive integer)\nEnter the key-b: '))
if key_b < 0:
raise KeyError
return self._affine_sub_routine((key_a, key_b), mode)
@get_cipher_func.register('Vignere Cipher')
@safe_run
def _vigenere_cipher(self, mode):
''' Cipher Routine to encode into or decode from Vigenere Cipher '''
key = input(f'(alphabets only and length of key should be >0 and <{self.length})\nEnter the key: ')
if any(char.isdigit() for char in key) or len(key) > self.length:
raise KeyError
key = list(key)
if self._length != len(key):
for i in range(self.length - len(key)):
key.append(key[i % len(key)])
return self._vigenere_otp_sub_routine(key, mode)
@get_cipher_func.register('Otp Cipher')
@safe_run
def _otp_cipher(self, mode):
''' Cipher Routine to encode into or decode from One Time Pad Cipher '''
if mode == 'encode':
while True:
choice = input('Enter key Automatically/Manually [A/m]: ')
if choice in ('A', 'a', ''):
key = ''.join(random.choice(string.ascii_letters).lower() for _ in self.text)
print('Encryption key is:', key)
break
elif choice in ('M', 'm'):
key = input(f'\n(alphabets only and length of key should be = {self.length})\nEnter the key: ')
if len(key) != self.length or any(not ch.isalpha() for ch in key):
raise KeyError
break
else:
print('Invalid Choice!!!')
else:
key = input(f'(alphabets only and length of key should be = {self.length})\nEnter the key: ')
if len(key) != self.length or any(not ch.isalpha() for ch in key):
raise KeyError
return self._vigenere_otp_sub_routine(key, mode)
@get_cipher_func.register('RSA Cipher')
def _rsa_cipher(self, mode):
''' Cipher Routine to encode into or decode from RSA Cipher '''
if mode == 'encode':
while True:
choice = input('Enter key Automatically [A/m]: ')
if choice in ('A', 'a', ''):
primes = [i for i in range(1000, 100000) if isPrime(i)]
p = random.choice(primes)
primes.remove(p)
q = random.choice(primes)
print('P = {} and Q = {}'.format(p, q))
break
elif choice in ('M', 'm'):
#print("Enter range min and max :")
range_min, range_max = int(input("Enter min range :")), int(input("Enter max range :"))
primes = [i for i in range(range_min, range_max) if isPrime(i)]
print(primes)
p,q = int(input("Enter p:")), int(input("Enter q:"))
print(p, q)
break
else :
break
n = p*q
phi = (p-1)*(q-1)
e = 2
while(e < phi):
if math.gcd(e, phi) == 1 :
break
else :
e += 1
return self._rsa_sub_routine(e, n, 'encode')
if mode == 'decode' :
p, q, e = int(input('Enter key public key p:')), int(input('Enter public key q:')), int(input('Enter e :'))
n = p*q
phi = (p-1)*(q-1)
d = (1 + (2*phi))/e
return self._rsa_sub_routine(d, n, 'decode')
def main():
''' Main Driver Program '''
print('''
_/_/_/ _/ _/
_/ _/ _/ _/_/ _/_/ _/_/ _/_/_/ _/_/_/_/
_/_/_/ _/_/ _/ _/ _/ _/_/_/_/ _/ _/
_/ _/ _/ _/ _/ _/ _/ _/
_/ _/ _/_/ _/ _/_/_/ _/_/_/ _/_/
_/
_/
_/_/_/ _/ _/
_/ _/_/_/ _/_/_/ _/_/ _/ _/_/
_/_/ _/ _/ _/ _/ _/ _/_/_/_/ _/_/
_/ _/ _/ _/ _/ _/ _/ _/
_/_/_/ _/ _/_/_/ _/ _/ _/_/_/ _/
_/
_/
''' )
options = ['1', '2', '3', '4']
while(True):
try:
choice = input('''\n\nMain Menu:
1. Encode into ciphertext
2. Decode into plaintext
3. Hack the ciphertext
4. Exit
\nEnter Your Choice: ''')
if choice not in options:
print('Please enter a valid choice!')
continue
elif choice == '4':
exit()
print('\n(Press `Ctrl+C` to return to Main Menu)')
cipher = Cipher()
if choice == '1':
cipher.encode()
elif choice == '2':
cipher.decode()
else:
cipher.hack()
except KeyboardInterrupt:
continue
if __name__ == '__main__':
main()