-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1.py
More file actions
30 lines (28 loc) · 974 Bytes
/
P1.py
File metadata and controls
30 lines (28 loc) · 974 Bytes
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
#Write a program to perform encryption and decryption using Caesar cipher
#(substitutional cipher).
def encrypt(text,shift):
result = ""
for char in text:
if char.isupper():
result += chr((ord(char) + shift - 65) % 26 + 65)
elif char.islower():
result += chr((ord(char) + shift - 97) % 26 + 97)
else:
result += char
return result
def decrypt(text,shift):
result = ""
for char in text:
if char.isupper():
result += chr((ord(char) - shift - 65) % 26 + 65)
elif char.islower():
result += chr((ord(char) - shift - 97) % 26 + 97)
else:
result += char
return result
text=str(input("Enter the text to be encrypted: "))
shift=int(input("Enter the shift value (1-25): "))
encrypted_text=encrypt(text,shift)
print("Encrypted text:", encrypted_text)
decrypted_text=decrypt(encrypted_text,shift)
print("Decrypted text:", decrypted_text)