forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesar Cipher Encoder & Decoder.py
86 lines (76 loc) · 2.24 KB
/
Caesar Cipher Encoder & Decoder.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#PROJECT1
#CAESAR CIPHER DECODER
#Author: InTruder
#Cloned from: https://github.com/InTruder-Sec/caesar-cipher
def main():
print("[>] CAESAR CIPHER DECODER!!! \n")
print("[1] Encrypt\n[2] Decrypt")
try:
func = int(input("Choose one of the above(example for encode enter 1): "))
except:
print("\n[>] Invalid input")
exit()
if func == 2:
decode()
else:
if func == 1:
encode()
else:
print("\n[>] Invalid input")
exit()
def encode():
text = input("Enter text to encode: ")
key = input("Enter number of characters you want to shift: ")
encoded_cipher = ""
try:
key = int(key)
except:
print("Only intigers between 0 to 25 are allowed. Try again :)")
exit()
if key > 25:
print("Only intigers between 0 to 25 are allowed. Try again :)")
exit()
else:
key = key
text = text.upper()
for char in text:
ascii = ord(char)
if ascii > 90:
new_ascii = ascii
else:
if ascii < 65:
new_ascii = ascii
else:
new_ascii = ascii + key
if new_ascii > 90:
new_ascii = new_ascii - 26
else:
new_ascii = new_ascii
encoded = chr(new_ascii)
encoded_cipher = encoded_cipher + encoded
print("Encoded text: " + encoded_cipher)
def decode():
cipher = input("\n[>] Enter your cipher text: ")
print("Posiblities of cipher text are: \n")
cipher = cipher.lower()
for i in range(1, 26):
decoded = ""
decoded_cipher = ""
for char in cipher:
ascii = ord(char)
if ascii < 97:
new_ascii = ascii
else:
if ascii > 122:
new_ascii = ascii
else:
new_ascii = ascii - int(i)
if new_ascii < 97:
new_ascii = new_ascii + 26
else:
new_ascii = new_ascii
decoded = chr(new_ascii)
decoded_cipher = decoded_cipher + decoded
print("\n" + decoded_cipher)
if __name__ == '__main__':
main()