The Bifid cipher
This example implements encryption and decryption using the Polybius square, a classical encryption method that was used in ancient Greece. The Polybius square is a table in which each letter of the alphabet is mapped to coordinates (row and column). In this code, the Polybius square is created dynamically based on a given key, and then used to encrypt and decrypt messages.
Description
Bifid cipher
is a polygraphic substitution cipher that was invented
By Felix Delastell around 1901.
The Polybius square size is used to encrypt the message. 5 х 5 combined
with transposition and fractionation. Any
Polybius square can be used. 5 х 5 but
since it has only 25 cells and 26 letters of the (English) alphabet, one cell should represent two letters - I and J,
which is a common choice.
Function polybius(text)
This function creates a Polybius square based on the transmitted text (key).
polybius(text) = Char.(reshape(Int.(collect(text)), isqrt(length(text)), :)')
collect(text)converts a stringtextinto an array of characters.Int.(...)converts each character to its numeric representation (ASCII code).reshape(...)converts an array of numbers into a square matrix.
The dimension of the matrix is defined as the square root of the length
of the text(isqrt(length(text))).Char.(...)converts numbers back to characters.
Function encrypt(message, poly)
This function encrypts the message using the Polybius square.
function encrypt(message, poly)
positions = [findall(==(c), poly)[1] for c in message]
numbers = vcat([c[1] for c in positions], [c[2] for c in positions])
return String([poly[numbers[i], numbers[i+1]] for i in 1:2:length(numbers)-1])
end
- For each character in the message
(message)finds its
coordinates (row and column) in the Polybius square using
findall(==(c), poly). - Coordinates are combined into a single array of numbers
(numbers),
where all row numbers go first, and then all column numbers. - The numbers are grouped in pairs, and for each pair there is
the corresponding symbol is in the Polybius square. - The result is returned as an encrypted string.
Function decrypt(message, poly)
This function decrypts a message encrypted with a square.
Polybius.
function decrypt(message, poly)
n = length(message)
positions = [findall(==(c), poly)[1] for c in message]
numbers = reduce(vcat, [[c[1], c[2]] for c in positions])
return String([poly[numbers[i], numbers[i+n]] for i in 1:n])
end
-
For each character in the encrypted message, finds it
coordinates in the Polybius square. -
Coordinates are combined into a single array of numbers
(numbers). -
The numbers are grouped in pairs, and for each pair there is
the corresponding symbol is in the Polybius square. -
The result is returned as a decrypted string.
The main cycle
Various combinations of keys and messages are checked in the loop.:
-
Key: "
ABCDEFGHIKLMNOPQRSTUVWXYZ", message: "ILOVEENGEE". -
Key: "
BGWKZQPNDSIOAXEFCLUMTHYVR", message: "EXPONENTA". -
Key: extended alphabet (including spaces, numbers, and symbols),
message: "Engee is the best environment for model based developement."
for (key, text) in [("ABCDEFGHIKLMNOPQRSTUVWXYZ", "ILOVEENGEE"), ("BGWKZQPNDSIOAXEFCLUMTHYVR", "EXPONENTA"),
([' '; '.'; 'A':'Z'; 'a':'z'; '0':'9'], "Engee is the best environment for model based design.")]
poly = polybius(key)
encrypted = encrypt(text, poly)
decrypted = decrypt(encrypted, poly)
println("Using polybius:")
display(poly)
println("\n Сообщение: $text\n
Зашифрованное: $encrypted\n
Дешифрованное: $decrypted\n\n")
end
Conclusion
This example demonstrates how the Polybius square can be used to encrypt and decrypt messages, as well as how to adapt it to work with different alphabets and symbols.