Spaces:
Sleeping
Sleeping
File size: 723 Bytes
0859f1c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import hashlib
import base64
def generate_key(password: str) -> bytes:
return hashlib.sha256(password.encode()).digest()
def xor_bytes(data: bytes, key: bytes) -> bytes:
return bytes([b ^ key[i % len(key)] for i, b in enumerate(data)])
def encrypt_string(plain_text: str, password: str) -> str:
key = generate_key(password)
plain_bytes = plain_text.encode()
encrypted_bytes = xor_bytes(plain_bytes, key)
return base64.b64encode(encrypted_bytes).decode()
def decrypt_string(encrypted_text: str, password: str) -> str:
key = generate_key(password)
encrypted_bytes = base64.b64decode(encrypted_text)
decrypted_bytes = xor_bytes(encrypted_bytes, key)
return decrypted_bytes.decode()
|