Bifid cipher

From Rosetta Code
Revision as of 14:48, 13 July 2022 by PureFox (talk | contribs) (Created new draft task and added a Wren solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Bifid cipher is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Description

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.

Operation

Suppose we want to encrypt the message "ATTACKATDAWN".

We use this archetypal Polybius square where I and J share the same position.

x/y 1 2 3 4 5
-------------
1 | A B C D E
2 | F G H I K
3 | L M N O P
4 | Q R S T U 
5 | V W X Y Z

The message is first converted to its x, y coordinates, but they are written vertically beneath.

A T T A C K A T D A W N
1 4 4 1 1 2 1 4 1 1 5 3
1 4 4 1 3 5 1 4 4 1 2 3

They are then arranged in a row.

1 4 4 1 1 2 1 4 1 1 5 3 1 4 4 1 3 5 1 4 4 1 2 3

Finally, they are divided up into pairs which are used to look up the encrypted letters in the square.

14 41 12 14 11 53 14 41 35 14 41 23
D  Q  B  D  A  X  D  Q  P  D  Q  H

The encrypted message is therefore "DQBDAXDQPDQH".

Decryption can be achieved by simply reversing these steps.

Task

Write routines in your language to encrypt and descrypt a message using the Bifid cipher.

Use them to verify (including subsequent decryption):

1. The above example.

2. The example in the Wikipedia article using the message and Polybius square therein.

3. 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.

Bonus

Suggest a way in which the cipher could be modified so that ALL 26 letters can be uniquely encrypted.

Related task

Playfair cipher

Wren

One way of enabling all 26 letters to be encrypted uniquely would be to use a 6 x 6 Polybius square including the 10 digits. We could then encrypt text using numerals as well.

However, the following just uses the standard version of the cipher. <lang ecmascript>import "./str" for Str import "./seq" for Lst

class Bifid {

   static encrypt(polybius, message) {
       message = Str.upper(message).replace("J", "I")
       var rows = []
       var cols = []
       for (c in message) {
           var ix = polybius.indexOf(c)
           if (ix == -1) continue
           rows.add((ix/5).floor + 1)
           cols.add((ix%5) + 1)
       }
       var s = ""
       for (pair in Lst.chunks(rows + cols, 2)) {
           var ix = (pair[0] - 1) * 5 + pair[1] - 1
           s = s + polybius[ix]
       }
       return s
   }
   static decrypt(polybius, message) {
       var rows = []
       var cols = []
       for (c in message) {
           var ix = polybius.indexOf(c)
           rows.add((ix/5).floor + 1)
           cols.add((ix%5) + 1)
       }
       var lines = Lst.flatten(Lst.zip(rows, cols))
       var count = lines.count/2
       rows = lines[0...count]
       cols = lines[count..-1]
       var s = ""
       for (i in 0...count) {
           var ix = (rows[i] - 1) * 5 + cols[i] - 1
           s = s + polybius[ix]
       }
       return s
   }

} var poly1 = "ABCDEFGHIKLMNOPQRSTUVWXYZ" var poly2 = "BGWKZQPNDSIOAXEFCLUMTHYVR" var poly3 = "PLAYFIREXMBCDGHKNOQSTUVWZ" var polys = [poly1, poly2, poly2, poly3] var msg1 = "ATTACKATDAWN" var msg2 = "FLEEATONCE" var msg3 = "The invasion will start on the first of January" var msgs = [msg1, msg2, msg1, msg3] for (i in 0...msgs.count) {

   var encrypted = Bifid.encrypt(polys[i], msgs[i])
   var decrypted = Bifid.decrypt(polys[i], encrypted)
   System.print("Message   : %(msgs[i])")
   System.print("Encrypted : %(encrypted)")
   System.print("Decrypted : %(decrypted)")
   if (i < msgs.count-1) System.print()

}</lang>

Output:
Message   : ATTACKATDAWN
Encrypted : DQBDAXDQPDQH
Decrypted : ATTACKATDAWN

Message   : FLEEATONCE
Encrypted : UAEOLWRINS
Decrypted : FLEEATONCE

Message   : ATTACKATDAWN
Encrypted : EYFENGIWDILA
Decrypted : ATTACKATDAWN

Message   : The invasion will start on the first of January
Encrypted : VRSYXSIYTMQVIRSKISLPVLDTCKRTCAIVTMATCEX
Decrypted : THEINVASIONWILLSTARTONTHEFIRSTOFIANUARY