Repository navigation
Expand file tree
/
Copy pathcaesarcipher.py
More file actions
147 lines (123 loc) · 5.13 KB
/
Copy pathcaesarcipher.py
File metadata and controls
147 lines (123 loc) · 5.13 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import string
def encrypt(message, shift):
"""
Encrypts a message using the Caesar Cipher.
Each letter is shifted forward by the shift amount.
Numbers and symbols are left unchanged.
"""
result = ""
# Loop through every character in the message
for char in message:
# If it's an uppercase letter
if char.isupper():
# ord() converts a character to its ASCII number
# e.g. ord('A') = 65, ord('Z') = 90
# We subtract 65 to get 0-25, add shift, use % 26 to wrap around
# then add 65 back to get the ASCII number of the new letter
shifted = (ord(char) - 65 + shift) % 26 + 65
result += chr(shifted) # chr() converts ASCII number back to character
# If it's a lowercase letter
elif char.islower():
# Same process but lowercase starts at ASCII 97
shifted = (ord(char) - 97 + shift) % 26 + 97
result += chr(shifted)
# If it's a number, symbol, or space — leave it unchanged
else:
result += char
return result
def decrypt(message, shift):
"""
Decrypts a Caesar Cipher message.
Decrypting is just encrypting with the opposite shift!
"""
return encrypt(message, -shift)
def brute_force(message):
"""
Tries all 25 possible shifts and prints them all.
This is how a hacker would crack a Caesar Cipher —
since there are only 25 possibilities, you can try them all instantly.
This is why Caesar Cipher is not secure by modern standards!
"""
print("\n🔓 BRUTE FORCE — All possible decryptions:")
print("-" * 50)
for shift in range(1, 26):
attempt = decrypt(message, shift)
print(f"Shift {shift:2d}: {attempt}")
print("-" * 50)
def get_gandalf_reaction(mode):
"""
Gandalf reacts to what you're doing in character.
"""
if mode == "encrypt":
reactions = [
"🧙 'Clever! Even the Eye of Sauron could not read this!'",
"🧙 'Your message is now hidden, like the entrance to Moria!'",
"🧙 'Excellent! A secret worthy of the White Council!'",
]
elif mode == "decrypt":
reactions = [
"🧙 'The message reveals itself, like dawn breaking over Mordor!'",
"🧙 'Ah, the secret is laid bare! Knowledge is power, my friend!'",
"🧙 'Like the inscription on the One Ring — all things are revealed in time!'",
]
else:
reactions = [
"🧙 'We try all paths, like searching every passage in Moria!'",
"🧙 'Brute force — not elegant, but effective. Even I am not above it!'",
"🧙 'A wizard tries every door until one opens!'",
]
import random
return random.choice(reactions)
def main():
print("🧙 GANDALF'S CAESAR CIPHER TOOL")
print("=" * 50)
print("*unrolls an ancient scroll and peers at it*")
print("A most ancient form of secret communication...\n")
while True:
print("\nWhat would you like to do?")
print("1. Encrypt a message")
print("2. Decrypt a message")
print("3. Brute force crack a message")
print("4. Quit")
choice = input("\nEnter your choice (1-4): ").strip()
if choice == "1":
message = input("\nEnter your message: ")
while True:
try:
shift = int(input("Enter shift amount (1-25): "))
if 1 <= shift <= 25:
break
print("Please enter a number between 1 and 25!")
except ValueError:
print("Please enter a valid number!")
encrypted = encrypt(message, shift)
print(f"\n🔐 Original: {message}")
print(f"🔒 Encrypted: {encrypted}")
print(f"🔑 Shift key: {shift}")
print(f"\n{get_gandalf_reaction('encrypt')}")
elif choice == "2":
message = input("\nEnter encrypted message: ")
while True:
try:
shift = int(input("Enter shift amount (1-25): "))
if 1 <= shift <= 25:
break
print("Please enter a number between 1 and 25!")
except ValueError:
print("Please enter a valid number!")
decrypted = decrypt(message, shift)
print(f"\n🔒 Encrypted: {message}")
print(f"🔓 Decrypted: {decrypted}")
print(f"🔑 Shift key: {shift}")
print(f"\n{get_gandalf_reaction('decrypt')}")
elif choice == "3":
message = input("\nEnter message to brute force: ")
print(get_gandalf_reaction('brute_force'))
brute_force(message)
elif choice == "4":
print("\n🧙 'Keep your secrets well, and trust them only to the worthy. Farewell!'")
break
else:
print("❌ Invalid choice — even Gandalf needs clear instructions!")
if __name__ == "__main__":
main()