Skip to content

Commit b7fb063

Browse files
yuriimchgcclauss
authored andcommitted
Feature/fix caesar cipher (TheAlgorithms#1350)
* change var names for better reading * rewrite encrypt function to fix ascii char ranges * fix decrypt function * update formatting (add f-strings) * upd fuctions * add f-string formatting (python3.6+) * add type hints (python3.4+)
1 parent 3cc3531 commit b7fb063

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

ciphers/caesar_cipher.py

+35-34
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,62 @@
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
1+
def encrypt(input_string: str, key: int) -> str:
2+
result = ''
3+
for x in input_string:
4+
if not x.isalpha():
5+
result += x
6+
elif x.isupper():
7+
result += chr((ord(x) + key - 65) % 26 + 65)
8+
elif x.islower():
9+
result += chr((ord(x) + key - 97) % 26 + 97)
10+
return result
911

1012

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
13+
def decrypt(input_string: str, key: int) -> str:
14+
result = ''
15+
for x in input_string:
16+
if not x.isalpha():
17+
result += x
18+
elif x.isupper():
19+
result += chr((ord(x) - key - 65) % 26 + 65)
20+
elif x.islower():
21+
result += chr((ord(x) - key - 97) % 26 + 97)
22+
return result
1923

2024

21-
def brute_force(strng):
25+
def brute_force(input_string: str) -> None:
2226
key = 1
23-
decrypted = ""
27+
result = ''
2428
while key <= 94:
25-
for x in strng:
29+
for x in input_string:
2630
indx = (ord(x) - key) % 256
2731
if indx < 32:
2832
indx = indx + 95
29-
decrypted = decrypted + chr(indx)
30-
print("Key: {}\t| Message: {}".format(key, decrypted))
31-
decrypted = ""
33+
result = result + chr(indx)
34+
print(f'Key: {key}\t| Message: {result}')
35+
result = ''
3236
key += 1
3337
return None
3438

3539

3640
def main():
3741
while True:
38-
print("-" * 10 + "\n**Menu**\n" + "-" * 10)
39-
print("1.Encrpyt")
40-
print("2.Decrypt")
41-
print("3.BruteForce")
42-
print("4.Quit")
42+
print(f'{"-" * 10}\n Menu\n{"-", * 10}')
43+
print(*["1.Encrpyt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep='\n')
4344
choice = input("What would you like to do?: ")
4445
if choice not in ["1", "2", "3", "4"]:
4546
print("Invalid choice, please enter a valid choice")
4647
elif choice == "1":
47-
strng = input("Please enter the string to be encrypted: ")
48-
key = int(input("Please enter off-set between 1-94: "))
48+
input_string = input("Please enter the string to be encrypted: ")
49+
key = int(input("Please enter off-set between 0-25: "))
4950
if key in range(1, 95):
50-
print(encrypt(strng.lower(), key))
51+
print(encrypt(input_string.lower(), key))
5152
elif choice == "2":
52-
strng = input("Please enter the string to be decrypted: ")
53+
input_string = input("Please enter the string to be decrypted: ")
5354
key = int(input("Please enter off-set between 1-94: "))
5455
if key in range(1, 95):
55-
print(decrypt(strng, key))
56+
print(decrypt(input_string, key))
5657
elif choice == "3":
57-
strng = input("Please enter the string to be decrypted: ")
58-
brute_force(strng)
58+
input_string = input("Please enter the string to be decrypted: ")
59+
brute_force(input_string)
5960
main()
6061
elif choice == "4":
6162
print("Goodbye.")

0 commit comments

Comments
 (0)