56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
|
|
# modes/decrypt.py
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Dict, Any, List
|
||
|
|
|
||
|
|
|
||
|
|
class DecryptMode:
|
||
|
|
def run(
|
||
|
|
self,
|
||
|
|
*,
|
||
|
|
c: int | List[int],
|
||
|
|
n: int,
|
||
|
|
d: int,
|
||
|
|
) -> Dict[str, Any]:
|
||
|
|
# Один блок
|
||
|
|
if isinstance(c, int):
|
||
|
|
plain = pow(c, d, n) # Дешифратор
|
||
|
|
char = chr(plain) if 0 <= plain < 128 else None
|
||
|
|
return {
|
||
|
|
"mode": "int",
|
||
|
|
"input": {
|
||
|
|
"cipher": c,
|
||
|
|
"hex_in": hex(c),
|
||
|
|
},
|
||
|
|
"output": {
|
||
|
|
"ascii": plain,
|
||
|
|
"char": char,
|
||
|
|
"hex_out": hex(plain),
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
# Несколько блоков (список чисел)
|
||
|
|
if isinstance(c, list):
|
||
|
|
result: List[Dict[str, Any]] = []
|
||
|
|
text = ""
|
||
|
|
for block in c:
|
||
|
|
plain = pow(block, d, n) # Дешифратор
|
||
|
|
char = chr(plain) if 0 <= plain < 128 else None
|
||
|
|
if char:
|
||
|
|
text += char
|
||
|
|
result.append({
|
||
|
|
"cipher": block,
|
||
|
|
"hex_in": hex(block),
|
||
|
|
"ascii": plain,
|
||
|
|
"char": char,
|
||
|
|
"hex_out": hex(plain),
|
||
|
|
})
|
||
|
|
return {
|
||
|
|
"mode": "list",
|
||
|
|
"input": c,
|
||
|
|
"output": result,
|
||
|
|
"text": text,
|
||
|
|
}
|
||
|
|
|
||
|
|
raise ValueError("c должен быть числом или списком чисел")
|