Skip to content

Commit 93c20a7

Browse files
authored
Updated_caesar_cipher.py
Python 3 and more features
1 parent 2e74c8e commit 93c20a7

File tree

1 file changed

+67
-43
lines changed

1 file changed

+67
-43
lines changed

ciphers/caesar_cipher.py

+67-43
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,69 @@
1-
from __future__ import print_function
2-
# The Caesar Cipher Algorithm
1+
def encrypt(strng, key):
2+
encrypted = ''
3+
for x in strng:
4+
indx = (ord(x) + key) % 256
5+
if indx > 126:
6+
indx = indx - 95
7+
encrypted = encrypted + chr(indx)
8+
return encrypted
9+
10+
11+
def decrypt(strng, key):
12+
decrypted = ''
13+
for x in strng:
14+
indx = (ord(x) - key) % 256
15+
if indx < 32:
16+
indx = indx + 95
17+
decrypted = decrypted + chr(indx)
18+
return decrypted
19+
20+
def brute_force(strng):
21+
key = 1
22+
decrypted = ''
23+
while key != 96:
24+
for x in strng:
25+
indx = (ord(x) - key) % 256
26+
if indx < 32:
27+
indx = indx + 95
28+
decrypted = decrypted + chr(indx)
29+
print(decrypted)
30+
decrypted = ''
31+
key += 1
32+
return None
33+
334

435
def main():
5-
message = input("Enter message: ")
6-
key = int(input("Key [1-26]: "))
7-
mode = input("Encrypt or Decrypt [e/d]: ")
8-
9-
if mode.lower().startswith('e'):
10-
mode = "encrypt"
11-
elif mode.lower().startswith('d'):
12-
mode = "decrypt"
13-
14-
translated = encdec(message, key, mode)
15-
if mode == "encrypt":
16-
print(("Encryption:", translated))
17-
elif mode == "decrypt":
18-
print(("Decryption:", translated))
19-
20-
def encdec(message, key, mode):
21-
message = message.upper()
22-
translated = ""
23-
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
24-
for symbol in message:
25-
if symbol in LETTERS:
26-
num = LETTERS.find(symbol)
27-
if mode == "encrypt":
28-
num = num + key
29-
elif mode == "decrypt":
30-
num = num - key
31-
32-
if num >= len(LETTERS):
33-
num -= len(LETTERS)
34-
elif num < 0:
35-
num += len(LETTERS)
36-
37-
translated += LETTERS[num]
38-
else:
39-
translated += symbol
40-
return translated
41-
42-
if __name__ == '__main__':
43-
import doctest
44-
doctest.testmod()
45-
main()
36+
print("**Menu**")
37+
print("1.Encrpyt")
38+
print("2.Decrypt")
39+
print("3.BruteForce")
40+
print("4.Quit")
41+
while True:
42+
choice = input("what would you like to do")
43+
if choice not in ['1', '2', '3', '4']:
44+
print ("Invalid choice")
45+
elif choice == '1':
46+
strng = input("Please enter the string to be ecrypted:")
47+
while True:
48+
key = int(input("Please enter off-set between 1-94"))
49+
if key > 0 and key <= 94:
50+
print (encrypt(strng, key))
51+
main()
52+
elif choice == '2':
53+
strng = input("Please enter the string to be decrypted:")
54+
while True:
55+
key = int(input("Please enter off-set between 1-94"))
56+
if key > 0 and key <= 94:
57+
print(decrypt(strng, key))
58+
main()
59+
elif choice == '3':
60+
strng = input("Please enter the string to be decrypted:")
61+
brute_force(strng)
62+
main()
63+
elif choice == '4':
64+
print ("GoodBye.")
65+
break
66+
67+
main()
68+
69+

0 commit comments

Comments
 (0)