Моя функция «угадай» выводит тарабарщину при замене наиболее частых байтов наиболее частыми символами [закрыто]Python

Программы на Python
Ответить
Anonymous
 Моя функция «угадай» выводит тарабарщину при замене наиболее частых байтов наиболее частыми символами [закрыто]

Сообщение Anonymous »

У меня есть два файла: текстовый файл, который будет использоваться для подсчета наиболее часто встречающихся символов, и двоичный файл, который будет использоваться для подсчета наиболее часто встречающихся байтов.
Мне нужно написать функцию, которая заменяет наиболее часто встречающиеся символы наиболее часто встречающимися байтами.
Вот результат, который я получаю после тестирования функции:

$o $$ealrde$ $ $ndtey tr $oic lrs nhh n $eal dome ynl$g$ n$ uante$sh
$ra lrs $aeiedme od $l $ltcrd mhnii$g$et$i cn$e n uaent ie$eitea
truetcea$g$$ $rs cn$e ndl $seitordi$ $eeh $aee tr aenmc rst tr $e$
$t$hh $e n $henisae tr niioit lrs$g$ltcrd oi drt nh$nli ni enil ni ot
irsdyi $st ot oi n urry $nl tr an$$ s$ i$ohhi tcnt nae urodu tr $e
$eal sie$shh od tce iemsaotl $rahyg$eit aeunayi$g$rsa tenmceag$coh

Однако в выводе должно быть правильно отображено 80 % символов.
Что я делаю не так? Я попытался увеличить количество пар с 17 до 100, но результат по-прежнему бесполезный.
Вот содержимое текстового файла:

Изучение захватывающих дух пейзажей английской сельской местности — это действительно замечательный опыт.
От холмов, покрытых яркой зеленью, до очаровательных маленьких деревень, расположенных вдоль извилистых рек, здесь есть неоспоримое чувство спокойствия и красоты.
Живописные пейзажи в сочетании с богатой историей и культурой создают идеальное сочетание очарования старого мира и современного очарования.
Если вы решите прогуляться по очаровательным садам, посетить исторические замки или насладиться традиционным послеобеденным чаем, в этом очаровательном месте каждый найдет что-то для себя.

Вот сценарий:
def frq_txt(text: str) -> List[Tuple[str, int]]:
"""
Finds how often each character appears in a plaintext string.
The function returns a list of (character, count) pairs ordered
from the most frequent to the least frequent character.
"""
counts = Counter(text)

# Convert the Counter into (char, count) pairs and sort them.
# The lambda picks item[1] (the count) so sorting is based on frequency.
# reverse=True ensures the most frequent characters come first.
freq_list = sorted(counts.items(), key=lambda item: item[1], reverse=True)
return freq_list

def frq_cpr(cipher_bytes: bytes) -> List[Tuple[int, int]]:
"""
Finds how often each byte value appears in a ciphertext.
The function returns (byte_value, count) pairs ordered from
the most frequent byte to the least frequent.
"""
counts = Counter(cipher_bytes)

# Convert counts to (byte, count) pairs and sort by count descending.
# The lambda selects item[1] (the count) so the sorting is based on frequency.
freq_list = sorted(counts.items(), key=lambda item: item[1], reverse=True)
return freq_list

def build_dec_key(
plain_freq: List[Tuple[str, int]],
cipher_freq: List[Tuple[int, int]],
top_n: int = 17,
) -> Dict[int, str]:
"""
Builds a partial decryption key by pairing the top-N most frequent plaintext
characters with the top-N most frequent ciphertext byte values.

Args:
plain_freq: List of (char, count) sorted from most to least frequent.
cipher_freq: List of (byte_value, count) sorted from most to least frequent.
top_n: Number of most frequent symbols to pair.

Returns:
A dictionary mapping ciphertext byte values (int) to guessed
plaintext characters (str).
"""
dec_key: Dict[int, str] = {}

# Take only the top N from each list
plain_top = plain_freq[:top_n]
cipher_top = cipher_freq[:top_n]

# Pair: most frequent plaintext char with most frequent ciphertext byte, etc.
for (ch, _), (byte_val, _) in zip(plain_top, cipher_top):
dec_key[byte_val] = ch

return dec_key

def guess_txt(cipher_bytes: bytes, dec_key: Dict[int, str]) -> str:
"""
Attempts to decrypt ciphertext using a partial decryption key.

Args:
cipher_bytes: The ciphertext (sequence of bytes).
dec_key: A partial decryption key mapping ciphertext byte values (0–255)
to guessed plaintext characters.

Returns:
A string where known bytes are replaced with their guessed plaintext
character, and unknown bytes are replaced with '$'.
"""
result = []

for b in cipher_bytes:
if b in dec_key:
result.append(dec_key)
else:
result.append("$")

return "".join(result)


Подробнее здесь: https://stackoverflow.com/questions/798 ... equent-byt
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»