How to resolve the algorithm Bifid cipher step by step in the Quackery programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bifid cipher step by step in the Quackery 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):

  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. 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 Quackery programming language

Source code in the quackery programming language

  [ $ "" swap
    witheach
      [ upper
        dup char I > if [ 1 - ]
        dup char A char Z
        within iff
          [ char A - join ]
        else drop ] ]               is ->0..24    (         $ --> [ )

  [ $ "" swap
    witheach
      [ char A +
        dup char I > if 1+
       join ] ]                     is ->A..Z     (         [ --> $ )

  [ [] 5 times
     [ dip ->0..24 join ] ]         is makesquare ( $ $ $ $ $ --> [ )

  [ dup witheach
      [ i^ unrot poke ] ]           is makeindex  (         [ --> [ )

  [ dup temp put
    makeindex temp put
    ->0..24
    [] swap witheach
      [ temp share swap peek
        5 /mod join
        nested join ]
    temp release
    transpose
    unpack join
    [] swap
    dup size 2 / times
      [ 2 split dip
          [ nested join ] ]
    drop
    $ "" swap
    witheach
      [ unpack swap 5 * +
        temp share swap peek
        join ]
    ->A..Z
    temp release ]                  is encrypt    (       $ [ --> $ )

  [ dup temp put
    makeindex temp put
    ->0..24
    [] swap witheach
      [ temp share swap peek
        5 /mod join join ]
    temp release
    dup size 2 / split
    2 pack
    transpose
    [] swap witheach
      [ unpack swap 5 * +
        temp share swap peek
        join ]
     ->A..Z
    temp release ]                  is decrypt    (       $ [ --> $ )

  [ $ "ABCDE"
    $ "FGHIK"
    $ "LMNOP"
    $ "QRSTU"
    $ "VWXYZ"
    makesquare ]           constant is tasksquare (           --> [ )

  [ $ "BGWKZ"
    $ "QPNDS"
    $ "IOAXE"
    $ "FCLUM"
    $ "THYVR"
    makesquare ]           constant is wikisquare (           --> [ )

  [ $ "QUACK"
    $ "DEPTH"
    $ "LYING"
    $ "FORMS"
    $ "BVWXZ"
    makesquare ]           constant is ducksquare (           --> [ )


  say "Using tasksquare:" cr
  $ "Attack at dawn."                                  dup echo$ say " -> "
  tasksquare encrypt                                   dup echo$ say " -> "
  tasksquare decrypt                                       echo$
  cr cr
  say "Using wikisquare:" cr
  $ "Flee at once."                                    dup echo$ say " -> "
  wikisquare encrypt                                   dup echo$ say " -> "
  wikisquare decrypt                                       echo$
  cr cr
  say "Using ducksquare:" cr
  $ "The invasion will start on the first of January." dup echo$ cr say " -> "
  ducksquare encrypt                                   dup echo$ cr say " -> "
  ducksquare decrypt                                       echo$

  

You may also check:How to resolve the algorithm ABC problem step by step in the Haskell programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Potion programming language
You may also check:How to resolve the algorithm Top rank per group step by step in the FunL programming language
You may also check:How to resolve the algorithm Naming conventions step by step in the Forth programming language
You may also check:How to resolve the algorithm Quine step by step in the OxygenBasic programming language