How to resolve the algorithm Bifid cipher step by step in the Raku programming language
How to resolve the algorithm Bifid cipher step by step in the Raku programming language
Table of Contents
Problem Statement
The Bifid cipher is a polygraphic substitution cipher which was invented by Félix Delastelle in around 1901. It uses a 5 x 5 Polybius square combined with transposition and fractionation to encrypt a message. Any 5 x 5 Polybius square can be used but, as it only has 25 cells and there are 26 letters of the (English) alphabet, one cell needs to represent two letters - I and J being a common choice. Suppose we want to encrypt the message "ATTACKATDAWN". We use this archetypal Polybius square where I and J share the same position. The message is first converted to its x, y coordinates, but they are written vertically beneath. They are then arranged in a row. Finally, they are divided up into pairs which are used to look up the encrypted letters in the square. The encrypted message is therefore "DQBDAXDQPDQH". Decryption can be achieved by simply reversing these steps. Write routines in your language to encrypt and descrypt a message using the Bifid cipher. Use them to verify (including subsequent decryption):
- The above example.
- The example in the Wikipedia article using the message and Polybius square therein.
- The above example but using the Polybius square in the Wikipedia article to illustrate that it doesn't matter which square you use as long, of course, as the same one is used for both encryption and decryption. In addition, encrypt and decrypt the message "The invasion will start on the first of January" using any Polybius square you like. Convert the message to upper case and ignore spaces. Suggest a way in which the cipher could be modified so that ALL 26 letters can be uniquely encrypted. Playfair cipher
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bifid cipher step by step in the Raku programming language
Source code in the raku programming language
sub polybius ($text) {
my $n = $text.chars.sqrt.narrow;
$text.comb.kv.map: { $^v => ($^k % $n, $k div $n).join: ' ' }
}
sub encrypt ($message, %poly) {
%poly.invert.hash{(flat reverse [Z] %poly{$message.comb}».words).batch(2)».reverse».join: ' '}.join
}
sub decrypt ($message, %poly) {
%poly.invert.hash{reverse [Z] (reverse flat %poly{$message.comb}».words».reverse).batch($message.chars)}.join
}
for 'ABCDEFGHIKLMNOPQRSTUVWXYZ', 'ATTACKATDAWN',
'BGWKZQPNDSIOAXEFCLUMTHYVR', 'FLEEATONCE',
(flat '_', '.', 'A'..'Z', 'a'..'z', 0..9).pick(*).join, 'The invasion will start on the first of January 2023.'.subst(/' '/, '_', :g)
-> $polybius, $message {
my %polybius = polybius $polybius;
say "\nUsing polybius:\n\t" ~ $polybius.comb.batch($polybius.chars.sqrt.narrow).join: "\n\t";
say "\n Message : $message";
say "Encrypted : " ~ my $encrypted = encrypt $message, %polybius;
say "Decrypted : " ~ decrypt $encrypted, %polybius;
}
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the Slate programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the K programming language
You may also check:How to resolve the algorithm Logical operations step by step in the OOC programming language
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the Clojure programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the MAD programming language