The permutation method
The permutation method is an encryption method in which the characters of the source text are swapped according to a predefined rule. The key element is the permutation order, which determines how the characters will be moved. The permutation method does not change the characters themselves. Only their position in the text changes. This method is often used in combination with other encryption methods to enhance the security of transmitted data.
In our example, we will consider a simple permutation option that shifts the entire message by a pre-specified number of characters.
# Функция для кодирования и декодирования методом перестановки
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
Let's perform a health check of the function.
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)
Conclusion
As we can see, according to the results of this model in our demonstration, the input and output words coincided, which indicates that this function is working correctly.