38 lines
1.6 KiB
Python
38 lines
1.6 KiB
Python
"""
|
||
This is a simple RSA encryptor/decryptor.
|
||
It uses a key pair which is stored in a 'keys' directory.
|
||
The keys are generated when you run the script.
|
||
The script will encrypt a message for you if you provide it.
|
||
The script will decrypt a message for you if you provide it.
|
||
The script will generate a key pair and save it in the 'keys' directory.
|
||
"""
|
||
|
||
import os
|
||
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)
|
||
print("Зашифрованное сообщение (Base64):", encrypted_message)
|
||
|
||
# Дешифрование сообщения
|
||
rsa_decryptor = RSADecryptor(PRIVATE_KEY_FILE)
|
||
decrypted_message = rsa_decryptor.decrypt_message(encrypted_message)
|
||
print("Расшифрованное сообщение:", decrypted_message)
|