Engee documentation
Notebook

Transposition method

A permutation method is an encryption method in which the characters of the source text are swapped according to a predetermined rule. The key element is the permutation order, which determines how the characters are moved. The permutation method does not change the characters themselves. Only their position in the text is changed. This method is often used in combination with other encryption methods to increase the security of transmitted data.

In our example, we will consider a simple permutation method that shifts the entire message by a predetermined number of characters.

In [ ]:
# Функция для кодирования и декодирования методом перестановки
function transform_message(message::String, key::Int, mode::String)
    transformed_message = ""
    for i in 1:length(message)
        if mode == "encode"
            new_index = mod(i + key - 1, length(message)) + 1 # Перемещаем символ на key позиций вперед
        elseif mode == "decode"
            new_index = mod(i - key - 1, length(message)) + 1 # Перемещаем символ на key позиций влево
        else
            error("Допустимые значения: :encode, :decode")
        end
        transformed_message *= message[new_index]
    end
    return transformed_message
end
Out[0]:
transform_message (generic function with 2 methods)

Let's check the function performance.

In [ ]:
message = "Engee";
key = 6;

key = 6 % length(message)
println("Приведём ключ к длине строки, отбросив целые длины строк: $(key)")
println()

encoded = transform_message(message, key, "encode")
decoded = transform_message(encoded, key, "decode")

println("Исходное сообщение: ", message)
println("Закодированное сообщение: ", encoded)
println("Декодированное сообщение: ", decoded)
Приведём ключ к длине строки, отбросив целые длины строк: 1

Исходное сообщение: Engee
Закодированное сообщение: ngeeE
Декодированное сообщение: Engee

Conclusion

As we can see, according to the results of this model in our demonstration, the input and output word coincide, which indicates that this function works correctly.