Files
labs-information-security/lab2-the-RSA-algorithm/app/modes/encrypt.py
T

58 lines
1.5 KiB
Python
Raw Normal View History

2026-07-12 14:06:45 +04:00
from __future__ import annotations
from typing import Dict, Any, List
from core.public_key import PublicKey
from core.prime_number import PrimeNumber
class EncryptMode:
def run(
self,
*,
text: str | None,
m: int | None,
n: int,
e: int,
) -> Dict[str, Any]:
pub = PublicKey(n=n, e=PrimeNumber(e))
if text is not None:
result: List[Dict[str, Any]] = []
for ch in text:
code = ord(ch)
cipher = pub.encrypt(code)
result.append({
"char": ch,
"ascii": code,
"hex_in": hex(code),
"cipher": cipher,
"hex_out": hex(cipher),
})
return {
"mode": "text",
"input": text,
"output": result,
}
if m is not None:
cipher = pub.encrypt(m)
char = chr(m) if 0 <= m < 128 else None
return {
"mode": "int",
"input": {
"char": char,
"ascii": m,
"hex_in": hex(m),
},
"output": {
"char": char,
"ascii": m,
"cipher": cipher,
"hex_out": hex(cipher),
},
}
raise ValueError("Необходимо указать либо text, либо m")