69 lines
1.8 KiB
Python
69 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Dict, Any
|
|
|
|
from modes.keygen import KeygenMode
|
|
from modes.encrypt import EncryptMode
|
|
from modes.decrypt import DecryptMode
|
|
|
|
|
|
class AutoMode:
|
|
"""
|
|
Автоматический режим:
|
|
1. Генерация ключей (p, q, n, e, d, phi).
|
|
2. Шифрование текста или числа.
|
|
3. Расшифрование результата обратно.
|
|
"""
|
|
|
|
def run(
|
|
self,
|
|
*,
|
|
text: str | None,
|
|
m: int | None,
|
|
p: int | None = None,
|
|
q: int | None = None,
|
|
min: int | None = None,
|
|
max: int | None = None,
|
|
bits: int | None = None,
|
|
seed: int | None = None,
|
|
) -> Dict[str, Any]:
|
|
# 1. Генерация ключей
|
|
keygen = KeygenMode()
|
|
keys = keygen.run(
|
|
p=p,
|
|
q=q,
|
|
min=min,
|
|
max=max,
|
|
bits=bits,
|
|
seed=seed,
|
|
)
|
|
|
|
n = keys["n"]["dec"]
|
|
e = keys["e"]["dec"]
|
|
d = keys["d"]["dec"]
|
|
|
|
# 2. Шифрование
|
|
encrypt = EncryptMode()
|
|
encrypted = encrypt.run(
|
|
text=text,
|
|
m=m,
|
|
n=n,
|
|
e=e,
|
|
)
|
|
|
|
# 3. Расшифрование
|
|
decrypt = DecryptMode()
|
|
# В зависимости от формата encrypted["output"]
|
|
if encrypted["mode"] == "text":
|
|
ciphers = [item["cipher"] for item in encrypted["output"]]
|
|
decrypted = decrypt.run(c=ciphers, n=n, d=d)
|
|
else:
|
|
cipher = encrypted["output"]["cipher"]
|
|
decrypted = decrypt.run(c=cipher, n=n, d=d)
|
|
|
|
return {
|
|
"keys": keys,
|
|
"encrypted": encrypted,
|
|
"decrypted": decrypted,
|
|
}
|