36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
import os
|
||
import json
|
||
from RSA_Crypto.encrypt import RSAEncryptor
|
||
from RSA_Crypto.decrypt import RSADecryptor
|
||
from RSA_Crypto.generate_save import RSAKeyPair
|
||
|
||
KEYS_DIR = 'keys'
|
||
PRIVATE_KEY_FILE = f'{KEYS_DIR}/private-key.pem'
|
||
PUBLIC_KEY_FILE = f'{KEYS_DIR}/public-key.pem'
|
||
|
||
if not os.path.isdir(KEYS_DIR):
|
||
os.makedirs(KEYS_DIR, exist_ok=True)
|
||
|
||
key_pair = RSAKeyPair(PRIVATE_KEY_FILE, PUBLIC_KEY_FILE)
|
||
key_pair.generate_and_save_keys()
|
||
|
||
message = '{"organization": "ООО Рога, Копыта & Ко", "license_expiration_date": "2024-01-01", "license_number": "1234567890", "count_licenses": 100}'
|
||
rsa_encryptor = RSAEncryptor(PUBLIC_KEY_FILE)
|
||
encrypted_message = rsa_encryptor.encrypt_message(message)
|
||
|
||
with open('output.txt', 'w') as f:
|
||
f.write(encrypted_message)
|
||
|
||
rsa_decryptor = RSADecryptor(PRIVATE_KEY_FILE)
|
||
|
||
with open('output.txt', 'r') as f:
|
||
encrypted_message_from_file = f.read()
|
||
|
||
decrypted_message = rsa_decryptor.decrypt_message(encrypted_message_from_file)
|
||
|
||
try:
|
||
decrypted_message_dict = json.loads(decrypted_message)
|
||
print(decrypted_message_dict['organization'])
|
||
except json.JSONDecodeError:
|
||
print("Ошибка: Не удалось декодировать сообщение как JSON.")
|