-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathalgorithms.py
More file actions
39 lines (28 loc) · 1.1 KB
/
Copy pathalgorithms.py
File metadata and controls
39 lines (28 loc) · 1.1 KB
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
39
from itertools import cycle
def encrypt(text, key='0'):
if not text:
raise ValueError("Text should not be empty")
if not key:
raise ValueError("Key should not be empty")
key = key.encode() if isinstance(key, str) else key
encrypted_text = ""
for char, key_char in zip(text, cycle(key)):
encrypted_text += chr((ord(char) + ord(key_char)) % 128)
return encrypted_text
def decrypt(text, key='0'):
if not text:
raise ValueError("Text should not be empty")
if not key:
raise ValueError("Key should not be empty")
key = key.encode() if isinstance(key, str) else key
decrypted_text = ""
for char, key_char in zip(text, cycle(key)):
decrypted_text += chr((ord(char) - ord(key_char)) % 128)
return decrypted_text
# Example Usage:
original_message = "Hello, World!"
encryption_key = "secretkey"
encrypted_message = encrypt(original_message, encryption_key)
print(f"Encrypted: {encrypted_message}")
decrypted_message = decrypt(encrypted_message, encryption_key)
print(f"Decrypted: {decrypted_message}")