Skip to content

Commit ad86f7e

Browse files
authored
Add files via upload
1 parent 81e54d5 commit ad86f7e

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

Caesar Cipher Encoder & Decoder.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#PROJECT1
2+
#CAESAR CIPHER DECODER
3+
4+
#Author: InTruder
5+
#Cloned from: https://github.com/InTruder-Sec/caesar-cipher
6+
7+
8+
def main():
9+
print("[>] CAESAR CIPHER DECODER!!! \n")
10+
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()
16+
17+
if func == 2:
18+
decode()
19+
else:
20+
if func == 1:
21+
encode()
22+
else:
23+
print("\n[>] Invalid input")
24+
exit()
25+
26+
def encode():
27+
text = input("Enter text to encode: ")
28+
key = input("Enter number of characters you want to shift: ")
29+
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()
41+
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)
57+
58+
59+
60+
def decode():
61+
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)
83+
84+
85+
if __name__ == '__main__':
86+
main()

0 commit comments

Comments
 (0)