Spaces:
Sleeping
Sleeping
| # import re | |
| # | |
| # def xor_cipher(text: str, key: str) -> str: | |
| # key_bytes = key.encode('utf-8') | |
| # text_bytes = text.encode('utf-8') | |
| # key_len = len(key_bytes) | |
| # encrypted_bytes = bytes([ | |
| # text_bytes[i] ^ key_bytes[i % key_len] | |
| # for i in range(len(text_bytes)) | |
| # ]) | |
| # return encrypted_bytes.hex() | |
| # | |
| # def encrypt_sensitive_data(text: str, words_to_encrypt: list[str], secret_key: str) -> str: | |
| # result = text | |
| # for word in words_to_encrypt: | |
| # if word in result: | |
| # encrypted = xor_cipher(word, secret_key) | |
| # result = result.replace(word, f"[{encrypted}]") | |
| # return result | |
| # | |
| # def decrypt_sensitive_data(text: str, secret_key: str) -> str: | |
| # | |
| # def decrypt_match(match): | |
| # encrypted_hex = match.group(1) | |
| # # Convert hex back to bytes | |
| # encrypted_bytes = bytes.fromhex(encrypted_hex) | |
| # # XOR with key to decrypt | |
| # key_bytes = secret_key.encode('utf-8') | |
| # decrypted_bytes = bytes([ | |
| # encrypted_bytes[i] ^ key_bytes[i % len(key_bytes)] | |
| # for i in range(len(encrypted_bytes)) | |
| # ]) | |
| # return decrypted_bytes.decode('utf-8') | |
| # | |
| # # Find all [encrypted] patterns and decrypt them | |
| # pattern = r'\[([\da-fA-F]+)\]' | |
| # return re.sub(pattern, decrypt_match, text) | |
| # | |
| # # Example usage: | |
| # if __name__ == "__main__": | |
| # SECRET_KEY = "dda7db64674d3cbc571ccedfdb4321818ba642b8dd3ddbdd80d1ce2b2a4a3546" | |
| # | |
| # # Test encryption | |
| # original_text = "Привет! Меня зовут John, я живу в Moscow, мой email: john@example.com" | |
| # sensitive_words = [] | |
| # | |
| # encrypted_text = encrypt_sensitive_data(original_text, sensitive_words, SECRET_KEY) | |
| # print("Encrypted:", encrypted_text) | |
| # | |
| # # Test decryption | |
| # decrypted_text = decrypt_sensitive_data(encrypted_text, SECRET_KEY) | |
| # print("Decrypted:", decrypted_text) | |
| # |