forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesar_cipher.py
68 lines (62 loc) · 1.93 KB
/
caesar_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
import sys
def encrypt(strng, key):
encrypted = ''
for x in strng:
indx = (ord(x) + key) % 256
if indx > 126:
indx = indx - 95
encrypted = encrypted + chr(indx)
return encrypted
def decrypt(strng, key):
decrypted = ''
for x in strng:
indx = (ord(x) - key) % 256
if indx < 32:
indx = indx + 95
decrypted = decrypted + chr(indx)
return decrypted
def brute_force(strng):
key = 1
decrypted = ''
while key <= 94:
for x in strng:
indx = (ord(x) - key) % 256
if indx < 32:
indx = indx + 95
decrypted = decrypted + chr(indx)
print("Key: {}\t| Message: {}".format(key, decrypted))
decrypted = ''
key += 1
return None
def main():
print('-' * 10 + "\n**Menu**\n" + '-' * 10)
print("1.Encrpyt")
print("2.Decrypt")
print("3.BruteForce")
print("4.Quit")
while True:
choice = input("What would you like to do?: ")
if choice not in ['1', '2', '3', '4']:
print ("Invalid choice")
elif choice == '1':
strng = input("Please enter the string to be ecrypted: ")
while True:
key = int(input("Please enter off-set between 1-94: "))
if key in range(1, 95):
print (encrypt(strng, key))
main()
elif choice == '2':
strng = input("Please enter the string to be decrypted: ")
while True:
key = int(input("Please enter off-set between 1-94: "))
if key > 0 and key <= 94:
print(decrypt(strng, key))
main()
elif choice == '3':
strng = input("Please enter the string to be decrypted: ")
brute_force(strng)
main()
elif choice == '4':
print ("Goodbye.")
sys.exit()
main()