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.
# A function for encoding and decoding by permutation method
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 # Moving the symbol forward by key positions
elseif mode == "decode"
new_index = mod(i - key - 1, length(message)) + 1 # Moving the symbol by key positions to the left
else
error("Acceptable values: :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("Let's reduce the key to the length of the string, discarding the integer lengths of the strings: $(key)")
println()
encoded = transform_message(message, key, "encode")
decoded = transform_message(encoded, key, "decode")
println("The original message: ", message)
println("The encoded message: ", encoded)
println("The decoded message: ", 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.