forked from vishalkoc2016/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextdecyptencrpyt.py
38 lines (28 loc) · 877 Bytes
/
textdecyptencrpyt.py
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
31
32
33
34
35
36
37
38
# encrypt and decrypt a text using a simple algorithm of offsetting the letters
key = 'abcdefghijklmnopqrstuvwxyz'
def encrypt(n, plaintext):
"""Encrypt the string and return the ciphertext"""
result = ''
for l in plaintext.lower():
try:
i = (key.index(l) + n) % 26
result += key[i]
except ValueError:
result += l
return result.lower()
def decrypt(n, ciphertext):
"""Decrypt the string and return the plaintext"""
result = ''
for l in ciphertext:
try:
i = (key.index(l) - n) % 26
result += key[i]
except ValueError:
result += l
return result
text = "I am coding Python on SoloLearn!"
offset = 5
encrypted = encrypt(offset, text)
print('Encrypted:', encrypted)
decrypted = decrypt(offset, encrypted)
print('Decrypted:', decrypted)