Skip to content

Commit ec02d24

Browse files
author
Darshan
committed
feat: preserve case in encryption and decryption
1 parent 60b4c24 commit ec02d24

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

Caesar_cipher.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
2-
def encryption(plain_text,shift_key):
2+
3+
def encryption(plain_text, shift_key):
34
cipher_text = ""
45
for char in plain_text:
5-
if char in alphabet:
6-
position = alphabet.index(char)
7-
new_position = (position+shift_key)%26
8-
cipher_text += alphabet[new_position]
6+
if char.lower() in alphabet: # Check lowercase version of the character
7+
is_upper = char.isupper() # Check if the original character is uppercase
8+
position = alphabet.index(char.lower())
9+
new_position = (position + shift_key) % 26
10+
new_char = alphabet[new_position]
11+
cipher_text += new_char.upper() if is_upper else new_char # Preserve original case
912
else:
10-
cipher_text += char
11-
print(f"Here's is the text after encryption: {cipher_text}")
13+
cipher_text += char # Non-alphabet characters remain unchanged
14+
print(f"Here's the text after encryption: {cipher_text}")
1215

13-
def decryption(cipher_text,shift_key):
16+
def decryption(cipher_text, shift_key):
1417
plain_text = ""
1518
for char in cipher_text:
16-
if char in alphabet:
17-
position = alphabet.index(char)
18-
new_position = (position-shift_key)%26
19-
plain_text += alphabet[new_position]
19+
if char.lower() in alphabet: # Check lowercase version of the character
20+
is_upper = char.isupper() # Check if the original character is uppercase
21+
position = alphabet.index(char.lower())
22+
new_position = (position - shift_key) % 26
23+
new_char = alphabet[new_position]
24+
plain_text += new_char.upper() if is_upper else new_char # Preserve original case
2025
else:
21-
plain_text += char
22-
print(f"Here's is the text after decryption: {plain_text}")
26+
plain_text += char # Non-alphabet characters remain unchanged
27+
print(f"Here's the text after decryption: {plain_text}")
2328

2429
end_program = False
25-
while not end_program:
26-
what_to_do = input("Type 'encrypt' for encryption, type 'decrypt' for decryption:\n")
27-
text = input("Type your message:\n").lower()
30+
while not end_program:
31+
what_to_do = input("Type 'encrypt' for encryption, type 'decrypt' for decryption:\n").lower()
32+
text = input("Type your message:\n") # No longer forcing lowercase here
2833
shift = int(input("Enter shift key:\n"))
29-
if what_to_do=="encrypt":
34+
if what_to_do == "encrypt":
3035
encryption(plain_text=text, shift_key=shift)
31-
elif what_to_do=="decrypt":
36+
elif what_to_do == "decrypt":
3237
decryption(cipher_text=text, shift_key=shift)
33-
play_again = input("Type 'yes' to continue, type 'no' to exit.\n")
38+
else:
39+
print("Invalid option. Please type 'encrypt' or 'decrypt'.")
40+
play_again = input("Type 'yes' to continue, type 'no' to exit.\n").lower()
3441
if play_again == 'no':
3542
end_program = True
36-
print("Have a nice day! Bye..")
43+
print("Have a nice day! Bye..")

0 commit comments

Comments
 (0)