File size: 1,918 Bytes
40749a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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)
#