forked from hastagAB/Awesome-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vigenere.py
79 lines (74 loc) · 2.12 KB
/
vigenere.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
alph = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
#actuall stuff
def decryption(key, text):
key_len = len(key)
count = 0
#adjusting the key
real_key = ''
#fixing spaces
for i in text:
if i != ' ':
if count == len(key):
count = 0
real_key += key[count]
count += 1
else:
real_key += ' '
#print(real_key)
encr = ''
#decrypting
for c in range(0,len(text)):
if text[c] == ' ':
encr += ' '
elif ((ord(text[c]) >= 48) and (ord(text[c]) <= 57)):
encr += text[c]
else:
encr += (alph[(ord(text[c]) - ord(real_key[c])) % 26])
return encr
def encryption(key, text):
key_len = len(key)
count = 0
#adjusting the key
real_key = ''
#fixing spaces
for i in text:
if i != ' ':
if count == len(key):
count = 0
real_key += key[count]
count += 1
else:
real_key += ' '
#print(real_key)
encr = ''
#encrypting
for c in range(0,len(text)):
if text[c] == ' ':
encr += ' '
elif ((ord(text[c]) >= 48) and (ord(text[c]) <= 57)):
encr += text[c]
else:
encr += (alph[(ord(real_key[c]) + ord(text[c])) % 26])
return encr
#user input
def main():
boolean = True
while(boolean):
try:
mode = input('Do you want to encrypt or to decrypt [e/d]? ')
if mode.upper().startswith('E'):
text = input('Please enter the text: ').upper()
key = input('Please enter the key: ').upper()
print(encryption(key, text))
boolean = False
elif mode.upper().startswith('D'):
text = input('Please enter the text: ').upper()
key = input('Please enter the key: ').upper()
print(decryption(key, text))
boolean = False
else:
print('Please enter a valid choice')
except KeyboardInterrupt:
exit()
if __name__ == '__main__':
main()