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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bifid cipher step by step in the Lua 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 Lua programming language

Source code in the lua programming language

function transcipher (cipher, message, decipher)
    local message = message:gsub("%s+", ""):upper()
    local xStr, yStr, s, char = "", "", ""
    for pos = 1, #message do
        char = message:sub(pos, pos)
        for x = 1, #cipher do
            for y = 1, #cipher[x] do
                if cipher[x][y] == char then
                    s = s .. x .. y
                    xStr = xStr .. x
                    yStr = yStr .. y
                end
            end
        end
    end
    if decipher then
        xStr, yStr = s:sub(1, #s/2), s:sub(#s/2 + 1, #s)
    else
        s = xStr .. yStr
    end
    local result, x, y = ""
    local limit = decipher and #s/2 or #s
    local step = decipher and 1 or 2
    for pos = 1, limit, step do
        x = tonumber(s:sub(pos, pos))
        y = decipher and
            tonumber(s:sub(pos + #s/2, pos + #s/2)) or
            tonumber(s:sub(pos + 1, pos + 1))
        result = result .. cipher[x][y]
    end
    return result
end

local RCbifid = {
    {"A", "B", "C", "D", "E"},
    {"F", "G", "H", "I", "K"},
    {"L", "M", "N", "O", "P"},
    {"Q", "R", "S", "T", "U"},
    {"V", "W", "X", "Y", "Z"}
}

local wikibifid = {
    {"B", "G", "W", "K", "Z"},
    {"Q", "P", "N", "D", "S"},
    {"I", "O", "A", "X", "E"},
    {"F", "C", "L", "U", "M"},
    {"T", "H", "Y", "V", "R"}
}

local mybifid = {
    {"A", "B", "C", "D", "E", "F"},
    {"G", "H", "I", "J", "K", "L"},
    {"M", "N", "O", "P", "Q", "R"},
    {"S", "T", "U", "V", "W", "X"},
    {"Y", "Z", "1", "2", "3", "4"},
    {"5", "6", "7", "8", "9", "0"}
}

local testCases = {
    {RCbifid, "ATTACKATDAWN"},
    {wikibifid, "FLEEATONCE"},
    {wikibifid, "ATTACKATDAWN",},
    {mybifid, "The invasion will start on the first of January"}
}

local msg
for task, case in pairs(testCases) do
    print("\nTask " .. task)
    msg = transcipher(case[1], case[2])
    print("Encoded message: " .. msg)
    msg = transcipher(case[1], msg, true)
    print("Decoded message: " .. msg)
end


  

You may also check:How to resolve the algorithm Bitmap/Write a PPM file step by step in the Scala programming language
You may also check:How to resolve the algorithm Best shuffle step by step in the Go programming language
You may also check:How to resolve the algorithm S-expressions step by step in the Haskell programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the Pascal programming language
You may also check:How to resolve the algorithm Collections step by step in the PowerShell programming language