|
1 |
| -#PROJECT1 |
2 |
| -#CAESAR CIPHER DECODER |
| 1 | +# PROJECT1 |
| 2 | +# CAESAR CIPHER ENCODER/DECODER |
3 | 3 |
|
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 |
6 | 6 |
|
| 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 |
7 | 21 |
|
8 | 22 | def main():
|
| 23 | + |
9 | 24 | print("[>] CAESAR CIPHER DECODER!!! \n")
|
10 | 25 | 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 | 26 |
|
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": |
21 | 30 | 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 | + |
25 | 39 |
|
26 | 40 | def encode():
|
27 |
| - text = input("Enter text to encode: ") |
28 |
| - key = input("Enter number of characters you want to shift: ") |
| 41 | + |
29 | 42 | 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 | + |
41 | 46 | 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) |
57 | 50 |
|
| 51 | + print(f"Encoded text: {encoded_cipher}") |
58 | 52 |
|
59 | 53 |
|
60 | 54 | def decode():
|
| 55 | + |
| 56 | + decoded_cipher = "" |
61 | 57 | 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) |
83 | 65 |
|
84 | 66 |
|
85 | 67 | if __name__ == '__main__':
|
|
0 commit comments