Skip to content

Commit c917fb1

Browse files
authored
caesar_cipher has been added
1 parent 5e0d289 commit c917fb1

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
def encrypt(text, shift):
2+
result = ""
3+
for char in text:
4+
if char.isalpha():
5+
shift_amount = shift % 26
6+
base = ord('A') if char.isupper() else ord('a')
7+
result += chr((ord(char) - base + shift_amount) % 26 + base)
8+
else:
9+
result += char
10+
return result
11+
12+
def decrypt(text, shift):
13+
return encrypt(text, -shift)
14+
15+
def main():
16+
while True:
17+
print("Select operation:")
18+
print("1. Encrypt")
19+
print("2. Decrypt")
20+
print("3. Exit")
21+
22+
choice = input("Enter choice (1-3): ")
23+
24+
if choice == '3':
25+
break
26+
27+
if choice in ['1', '2']:
28+
text = input("Enter text: ")
29+
shift = int(input("Enter shift value: "))
30+
31+
if choice == '1':
32+
result = encrypt(text, shift)
33+
print(f"Encrypted text: {result}")
34+
elif choice == '2':
35+
result = decrypt(text, shift)
36+
print(f"Decrypted text: {result}")
37+
38+
else:
39+
print("Invalid choice")
40+
41+
if __name__ == "__main__":
42+
main()

0 commit comments

Comments
 (0)