Skip to content

Commit 2275eb5

Browse files
authored
Merge pull request csubhasundar#233 from Sgvkamalakar/patch-2
Create caesar_cipher_encoder__decoder.py
2 parents 829ff77 + e17f5cc commit 2275eb5

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, n):
2+
encrypted = ""
3+
for char in text:
4+
if char.isupper():
5+
c = ord(char) + (n % 26)
6+
if c > ord('Z'):
7+
c -= 26
8+
elif char.islower():
9+
c = ord(char) + (n % 26)
10+
if c > ord('z'):
11+
c -= 26
12+
else:
13+
c = ord(char) + 1
14+
encrypted += chr(c)
15+
return encrypted
16+
17+
def decrypt(text, n):
18+
decrypted = ""
19+
for char in text:
20+
if char.isupper():
21+
c = ord(char) - (n % 26)
22+
if c < ord('A'):
23+
c += 26
24+
elif char.islower():
25+
c = ord(char) - (n % 26)
26+
if c < ord('a'):
27+
c += 26
28+
else:
29+
c = ord(char) - 1
30+
decrypted += chr(c)
31+
return decrypted
32+
33+
def main():
34+
text = input("Enter a string: ")
35+
n = int(input("Enter key: "))
36+
encrypted_string = encrypt(text, n)
37+
print("\nEncrypted text:", encrypted_string)
38+
decrypted_string = decrypt(encrypted_string, n)
39+
print("Decrypted text:", decrypted_string)
40+
41+
if __name__ == "__main__":
42+
main()

0 commit comments

Comments
 (0)