Skip to content

Commit 6b7525a

Browse files
committed
Add: Caesar Cipher Encryptor/Decryptor
1 parent f74481e commit 6b7525a

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
print("🔐 Caesar Cipher Encryptor & Decryptor 🔐")
2+
print("Hide your secret messages or reveal them! \n")
3+
4+
while True:
5+
choice = input("🎯 Choose Operation: Encrypt (E), Decrypt (D), or Quit (Q): ").upper()
6+
7+
if choice in ["Q", "QUIT"]:
8+
break
9+
10+
elif choice in ["E", "D"]:
11+
message = input("📝 Enter your message: ")
12+
13+
try:
14+
shift = int(input("🔑 Enter the shift key (whole number): "))
15+
except ValueError:
16+
print("❌ Error: Shift key must be a valid whole number.\n")
17+
continue
18+
19+
# If decrypting, we just reverse the shift direction
20+
if choice == "D":
21+
shift = -shift
22+
23+
result = ""
24+
25+
# Core Caesar Cipher Logic
26+
for char in message:
27+
if char.isalpha():
28+
# Determine ASCII boundary (uppercase vs lowercase)
29+
start = ord('A') if char.isupper() else ord('a')
30+
31+
# Shift character and wrap around using modulo 26
32+
shifted_pos = (ord(char) - start + shift) % 26
33+
new_char = chr(start + shifted_pos)
34+
result += new_char
35+
else:
36+
# Leave spaces, numbers, and punctuation untouched
37+
result += char
38+
39+
print("\n✨ Resulting Message:")
40+
print(f"👉 {result}\n")
41+
42+
else:
43+
print("⚠️ Invalid input. Please type E, D, or Q.\n")
44+
45+
print("\n👋 Thanks for using the Caesar Cipher! Stay secure! 🕵️‍♂️\n")

0 commit comments

Comments
 (0)