Skip to content

Commit 483b4ba

Browse files
committed
FIXED: Caesar decoder possible result
1 parent efb7907 commit 483b4ba

File tree

1 file changed

+47
-65
lines changed

1 file changed

+47
-65
lines changed

Caesar Cipher Encoder & Decoder.py

+47-65
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,67 @@
1-
#PROJECT1
2-
#CAESAR CIPHER DECODER
1+
# PROJECT1
2+
# CAESAR CIPHER ENCODER/DECODER
33

4-
#Author: InTruder
5-
#Cloned from: https://github.com/InTruder-Sec/caesar-cipher
4+
# Author: InTruder
5+
# Cloned from: https://github.com/InTruder-Sec/caesar-cipher
66

7+
# Improved by: OfficialAhmed (https://github.com/OfficialAhmed)
8+
9+
def get_int() -> int:
10+
"""
11+
Get integer, otherwise redo
12+
"""
13+
14+
try:
15+
key = int(input("Enter number of characters you want to shift: "))
16+
except:
17+
print("Enter an integer")
18+
key = get_int()
19+
20+
return key
721

822
def main():
23+
924
print("[>] CAESAR CIPHER DECODER!!! \n")
1025
print("[1] Encrypt\n[2] Decrypt")
11-
try:
12-
func = int(input("Choose one of the above(example for encode enter 1): "))
13-
except:
14-
print("\n[>] Invalid input")
15-
exit()
1626

17-
if func == 2:
18-
decode()
19-
else:
20-
if func == 1:
27+
match input("Choose one of the above(example for encode enter 1): "):
28+
29+
case "1":
2130
encode()
22-
else:
23-
print("\n[>] Invalid input")
24-
exit()
31+
32+
case "2":
33+
decode()
34+
35+
case _:
36+
print("\n[>] Invalid input. Choose 1 or 2")
37+
main()
38+
2539

2640
def encode():
27-
text = input("Enter text to encode: ")
28-
key = input("Enter number of characters you want to shift: ")
41+
2942
encoded_cipher = ""
30-
try:
31-
key = int(key)
32-
except:
33-
print("Only intigers between 0 to 25 are allowed. Try again :)")
34-
exit()
35-
if key > 25:
36-
print("Only intigers between 0 to 25 are allowed. Try again :)")
37-
exit()
38-
else:
39-
key = key
40-
text = text.upper()
43+
text = input("Enter text to encode: ")
44+
key = get_int()
45+
4146
for char in text:
42-
ascii = ord(char)
43-
if ascii > 90:
44-
new_ascii = ascii
45-
else:
46-
if ascii < 65:
47-
new_ascii = ascii
48-
else:
49-
new_ascii = ascii + key
50-
if new_ascii > 90:
51-
new_ascii = new_ascii - 26
52-
else:
53-
new_ascii = new_ascii
54-
encoded = chr(new_ascii)
55-
encoded_cipher = encoded_cipher + encoded
56-
print("Encoded text: " + encoded_cipher)
47+
48+
ascii = ord(char) + key
49+
encoded_cipher += chr(ascii)
5750

51+
print(f"Encoded text: {encoded_cipher}")
5852

5953

6054
def decode():
55+
56+
decoded_cipher = ""
6157
cipher = input("\n[>] Enter your cipher text: ")
62-
print("Posiblities of cipher text are: \n")
63-
cipher = cipher.lower()
64-
for i in range(1, 26):
65-
decoded = ""
66-
decoded_cipher = ""
67-
for char in cipher:
68-
ascii = ord(char)
69-
if ascii < 97:
70-
new_ascii = ascii
71-
else:
72-
if ascii > 122:
73-
new_ascii = ascii
74-
else:
75-
new_ascii = ascii - int(i)
76-
if new_ascii < 97:
77-
new_ascii = new_ascii + 26
78-
else:
79-
new_ascii = new_ascii
80-
decoded = chr(new_ascii)
81-
decoded_cipher = decoded_cipher + decoded
82-
print("\n" + decoded_cipher)
58+
key = get_int()
59+
60+
for character in cipher:
61+
ascii = ord(character) - key
62+
decoded_cipher += chr(ascii)
63+
64+
print(decoded_cipher)
8365

8466

8567
if __name__ == '__main__':

0 commit comments

Comments
 (0)