homelab/services/mtproto_page/xk9m2p4q7/encrypt.py
2026-05-02 18:36:53 +03:00

30 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import json, os, base64, getpass
from pathlib import Path
# Скрипт который мы шифруем наш файл с proxy (data.json)
# Зачем? Да все просто. Оч впадлу писать какой-то бэк, да я и не программист и не ебу за бэстпрактики
# Отдаем пользователю на фронте шифрованный файл с прокси, пользак вводит пароль и JS его расшифровывает
# Добавился новый прокси? Пересоздаем файл - готово. Также можно легко менять пароль
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
here = Path(__file__).parent
plaintext = here.joinpath('data.json').read_bytes()
password = getpass.getpass('Password: ').encode()
salt = os.urandom(16)
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, iterations=100000)
key = kdf.derive(password)
iv = os.urandom(12)
ciphertext = AESGCM(key).encrypt(iv, plaintext, None)
here.joinpath('data.enc').write_text(json.dumps({
'salt': base64.b64encode(salt).decode(),
'iv': base64.b64encode(iv).decode(),
'ct': base64.b64encode(ciphertext).decode(),
}))
print('data.enc written.')