[Python] Decryption of the closed text using frequency analysis

Discussão em 'Python' iniciado por Stack, Novembro 28, 2025.

  1. Stack

    Stack Membro Participativo

    0

    I have an encrypted text. I split it into dyads to get numbers for frequency analysis. I wrote an html program that calculated the number of numbers and their frequency and compared them with the frequency of letters in the Russian language. I will attach the ciphertext and code directly below. the problem is that when substituted according to the calculated frequency, an unrelated text is obtained. I ask you to help anyone who understands this. presumably the text should be from a series of books about dunno.

    Here is the ciphertext: 81 69 83 90 69 83 88 17 54 83 88 45 17 82 69 66 35 82 69 87 17 11 70 17 69 75 93 35 69 87 21 57 83 79 30 87 35 21 88 54 35 82 69 87 83 87 35 93 83 54 57 17 55 57 17 57 79 83 93 83 11 83 12 64 35 45 35 53 35 87 83 57 21 82 54 21 45 35 58 17 55 57 17 57 82 69 83 45 83 64 35 54 83 12 58 35 82 93 21 87 11 17 82 69 45 17 11 17 93 17 83 69 82 54 83 35 12 87 35 30 57 93 99 64 35 82 69 21 21 35 29 35 53 83 93 75 66 35 83 69 69 83 38 83 81 69 83 87 35 53 42 93 17 87 21 64 35 87 29 21 87 83 12 87 21 11 35 54 83 81 57 83 12 54 66 57 83 93 35 54 82 35 35 29 35 83 53 40 83 11 21 93 83 82 75 87 83 83 87 17 88 17 57 83 87 81 21 93 17 30 81 35 53 30 21 87 21 57 69 83 57 17 88 17 93 83 82 75 87 35 21 79 35 93 87 21 79 17 93 35 12 66 35 38 83 58 45 35 11 82 69 17 54 93 35 87 21 55 83 35 35 53 30 11 30 29 35 79 83 69 35 70 82 83 53 21 45 17 93 82 55 83 69 58 45 17 54 21 69 75 35 35 87 17 82 93 35 11 30 99 29 30 99 88 21 79 30 54 58 17 45 21 64 87 83 90 69 83 35 12 82 83 54 35 45 66 35 87 87 83 87 35 30 93 42 53 17 93 83 82 75 58 45 17 54 11 17 21 69 83 81 69 83 35 12 53 83 93 75 66 35 40 83 69 35 93 83 82 75 83 82 69 17 54 17 69 75 82 55 11 83 79 17 83 87 17 83 82 83 88 87 17 93 17 90 69 83 87 35 11 17 54 87 83 87 83 81 35 69 57 83 83 87 17 87 35 87 17 54 21 11 35 93 17 17 45 93 35 87 30

    here is the html file:

    enter image description here

    <!-- Russian alphabet -->
    <div class="row justify-content-center">
    <div class="col-12">
    <h5 class="text-center mb-2" style="font-size: 16px;">Russian alphabet (by frequency)</h5>
    <div class="table-responsive">
    <table class="table table-bordered table-sm alphabet-table">
    <tr id="letters"></tr>
    <tr id="freq-order"></tr>
    </table>
    </div>
    </div>
    </div>

    <!-- Horizontal dyad frequency table -->
    <div class="mt-3">
    <h5 class="text-center mb-2" style="font-size: 16px;">Frequency of dyads in the text</h5>
    <div class="horizontal-table">
    <table class="frequency-table">
    <!-- String with dyads -->
    <tr>
    <td class="table-title">DYAD</td>
    <td id="diads-header"></td>
    </tr>
    <!-- Quantity string -->
    <tr>
    <td class="table-title">QUANTITY</td>
    <td id="counts"></td>
    </tr>
    <!-- String with substitutions -->
    <tr>
    <td class="table-title">REPLACEMENT</td>
    <td id="inputs"></td>
    </tr>
    </table>
    </div>
    </div>

    <div class="mt-3">
    <h4 style="font-size: 18px;">Decryption result:</h4>
    <div id="code" class="p-2 bg-light border rounded"></div>
    </div>

    <div class="mt-2">
    <button id="reset-btn" class="btn btn-warning btn-sm">Reset all replacements</button>
    <span class="ms-2 text-muted" style="font-size: 12px;">Enter the letters in the "REPLACEMENT" fields for substitution</span>>
    </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
    // Your ciphertext (dyads)
    const encryptedText = "81 69 83 90 69 83 88 17 54 83 88 45 17 82 69 66 35 82 69 87 17 11 70 17 69 75 93 35 69 87 21 57 83 79 30 87 35 21 88 54 35 82 69 87 83 87 35 93 83 54 57 17 55 57 17 57 79 83 93 83 11 83 12 64 35 45 35 53 35 87 83 57 21 82 54 21 45 35 58 17 55 57 17 57 82 69 83 45 83 64 35 54 83 12 58 35 82 93 21 87 11 17 82 69 45 17 11 17 93 17 83 69 82 54 83 35 12 87 35 30 57 93 99 64 35 82 69 21 21 35 29 35 53 83 93 75 66 35 83 69 69 83 38 83 81 69 83 87 35 53 42 93 17 87 21 64 35 87 29 21 87 83 12 87 21 11 35 54 83 81 57 83 12 54 66 57 83 93 35 54 82 35 35 29 35 83 53 40 83 11 21 93 83 82 75 87 83 83 87 17 88 17 57 83 87 81 21 93 17 30 81 35 53 30 21 87 21 57 69 83 57 17 88 17 93 83 82 75 87 35 21 79 35 93 87 21 79 17 93 35 12 66 35 38 83 58 45 35 11 82 69 17 54 93 35 87 21 55 83 35 35 53 30 11 30 29 35 79 83 69 35 70 82 83 53 21 45 17 93 82 55 83 69 58 45 17 54 21 69 75 35 35 87 17 82 93 35 11 30 99 29 30 99 88 21 79 30 54 58 17 45 21 64 87 83 90 69 83 35 12 82 83 54 35 45 66 35 87 87 83 87 35 30 93 42 53 17 93 83 82 75 58 45 17 54 11 17 21 69 83 81 69 83 35 12 53 83 93 75 66 35 40 83 69 35 93 83 82 75 83 82 69 17 54 17 69 75 82 55 11 83 79 17 83 87 17 83 82 83 88 87 17 93 17 90 69 83 87 35 11 17 54 87 83 87 83 81 35 69 57 83 83 87 17 87 35 87 17 54 21 11 35 93 17 17 45 93 35 87 30";

    // Russian alphabet in descending order of frequency
    const russianAlphabet = [
    'o', 'e', 'a', 'I', 'n', 't', 's', 'r', 'v', 'l',
    'k', 'm', 'd', 'p', 'y', 'I', 's', 'b', 'g', 'z',
    'b', 'h', 'y', 'x', 'w', 'y', 'c', 'sch', 'e',
    'f', 'b'
    ];

    // Using your frequency table (dyads are sorted in descending order of frequency)
    const frequencyData = [
    {diad: '83', count: 57},
    {diad: '35', count: 50},
    {diad: '17', count: 38},
    {diad: '87', count: 34},
    {diad: '69', count: 27},
    {diad: '93', count: 24},
    {diad: '21', count: 23},
    {diad: '82', count: 22},
    {diad: '54', count: 17},
    {diad: '57', count: 14},
    {diad: '11', count: 13},
    {diad: '45', count: 12},
    {diad: '30', count: 11},
    {diad: '53', count: 9},
    {diad: '75', count: 9},
    {diad: '12', count: 8},
    {diad: '79', count: 7},
    {diad: '81', count: 7},
    {diad: '88', count: 7},
    {diad: '58', count: 6},
    {diad: '66', count: 6},
    {diad: '29', count: 5},
    {diad: '55', count: 5},
    {diad: '64', count: 5},
    {diad: '90', count: 3},
    {diad: '99', count: 3},
    {diad: '38', count: 2},
    {diad: '40', count: 2},
    {diad: '42', count: 2},
    {diad: '70', count: 2}
    ];

    // Displaying the source text
    $("#code").text(encryptedText);

    // Filling in the Russian alphabet table
    russianAlphabet.forEach(function(letter, index) {
    $(`<td style="width: 35px; font-size: 14px; font-weight: bold;">${letter}</td>`).appendTo("#letters");
    $(`<td style="width: 35px; background-color: #f8f9fa; font-size: 13px;">${index + 1}</td>`).appendTo("#freq-order");
    });

    // Filling in the horizontal frequency table
    let diadsHeaderHtml = '';
    let countsHtml = '';
    let inputsHtml = '';

    frequencyData.forEach(function(item, index) {
    diadsHeaderHtml += `<td class="diad-cell">${item.diad}</td>`;
    countsHtml += `<td class="count-cell">${item.count}</td>`;
    inputsHtml += `
    <td class="input-cell">
    <input class="substitution-input"
    data-diad="${item.diad}"
    style="width:32px; height:28px;"
    maxlength="1"/>
    </td>
    `;
    });

    $("#diads-header").html(diadsHeaderHtml);
    $("#counts").html(countsHtml);
    $("#inputs").html(inputsHtml);

    // Function for applying substitutions
    function applySubstitutions() {
    let result = encryptedText;

    // Resetting the backlight
    $("#letters td").removeClass('active');

    // We apply all the entered substitutions
    $(".substitution-input").each(function() {
    const diad = $(this).data('diad');
    const letter = $(this).val().toLowerCase();

    if (letter && /[a-ze]/.test(letter)) {
    // Replacing all occurrences of the dyad
    const regex = new RegExp(diad, 'g');
    result = result.replace(regex, `<mark>${letter}</mark>`);

    // Highlighting the letter used in the alphabet
    $('#letters td').each(function() {
    if ($(this).text() === letter) {
    $(this).addClass('active');
    }
    });
    }
    });

    // Displaying the result
    $("#code").html(result);
    }

    // Handler for entering substitutions into fields
    $(document).on('keyup', '.substitution-input', function() {
    applySubstitutions();
    });

    // Reset button
    $("#reset-btn").click(function() {
    $(".substitution-input").val('');
    applySubstitutions();
    });

    // Initialization at boot
    $(document).ready(function() {
    applySubstitutions();
    });
    </script>

    Continue reading...

Compartilhe esta Página