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

Published on 12 May 2024 09:40 PM

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

The provided code implements both encryption and decryption using the Polybius square cipher in Ruby. The Polybius square is a 5x5 grid filled with the letters of the alphabet, used for encrypting messages by replacing each letter in the message with the row and column numbers of the corresponding letter in the square.**

Here's a detailed explanation of the Ruby code:

  1. Clean Message:

    • The cleanMsg method takes two parameters: msg (the message to be processed) and square (the Polybius square).
    • It converts the msg to uppercase and removes all spaces.
    • If the J character is not present in the square, it removes all occurrences of J from the message.
  2. Encryption:

    • The encrypt method takes two parameters: msg (the message to be encrypted) and square (the Polybius square).
    • It first calls cleanMsg to prepare the message for encryption.
    • It calculates the size of the square using the square root of its length.
    • It initializes two arrays, rows and cols, each with the same length as the message.
    • It iterates over each character in the message and finds its position in the Polybius square using the index method.
    • It assigns the row and column numbers of the character to the corresponding elements in rows and cols.
    • Finally, it concatenates rows and cols and iterates over the resulting array in pairs. For each pair, it takes the row and column numbers and uses them to retrieve the corresponding character from the Polybius square, building the encrypted message.
  3. Decryption:

    • The decrypt method takes two parameters: msg (the encrypted message) and square (the Polybius square).
    • It converts both msg and square to uppercase.
    • It calculates the size of the square as before.
    • It initializes an empty array coords to store the row and column numbers of the encrypted characters.
    • It initializes an empty string result to store the decrypted message.
    • It iterates over each character in the encrypted message and finds its position in the Polybius square.
    • It appends the row and column numbers of the character to the coords array.
    • Finally, it iterates over the coords array in pairs of two, taking the row and column numbers and using them to retrieve the corresponding character from the Polybius square, building the decrypted message.
  4. Print Square:

    • The printSquare method takes one parameter: square (the Polybius square).
    • It calculates the size of the square as before.
    • It iterates over the square, taking slices of size sq_size (the number of columns) and printing each slice on a new line. This visualizes the Polybius square.
  5. Test Cases:

    • The code includes a list of test cases, each consisting of a message and a Polybius square.
  6. Main Loop:

    • The code iterates over each test case, printing the Polybius square, the original message, the encrypted message, and the decrypted message.

In summary, this code implements a Polybius square cipher in Ruby, allowing for both encryption and decryption of messages using a provided Polybius square.

Source code in the ruby programming language

def cleanMsg(msg, square)
  msg.upcase!
  msg.delete!(' ')
  msg.delete!('J') if square.index('J') == nil
end

def encrypt(msg, square)
  cleanMsg msg, square
  sq_size = (square.length ** 0.5).to_i
  rows = [0] * msg.length
  cols = [0] * msg.length
  (0...msg.length).each do |i|
    p = square.index(msg[i])
    rows[i], cols[i] = p / sq_size, p % sq_size
  end
  result = ""
  rows.concat(cols).each_slice(2) do |coord|
    result += square[coord[0]*sq_size + coord[1]]
  end
  return result
end

def decrypt(msg, square)
  msg.upcase!; square.upcase!
  sq_size = (square.length ** 0.5).to_i
  coords = []
  result = ""
  (0...msg.length).each do |i| 
    p = square.index(msg[i])
    coords << p / sq_size
    coords << p % sq_size
  end
  for i in (0...coords.length/2) do
    row, col = coords[i], coords[i+coords.length/2]
    result += square[row*sq_size + col]
  end
  return result 
end

def printSquare(square)
  sq_size = (square.length ** 0.5).to_i
  (0..square.length).step(sq_size).each do |i|
    print square[i...(i+sq_size)], "\n"
  end
end

tests = [["ATTACKATDAWN" , "ABCDEFGHIKLMNOPQRSTUVWXYZ"],
         ["FLEEATONCE"   , "BGWKZQPNDSIOAXEFCLUMTHYVR"],
         ["ATTACKATDAWN" , "ABCDEFGHIKLMNOPQRSTUVWXYZ"],
         ["the invasion will start on the first of january", "BGWKZQPNDSIOAXEFCLUMTHYVR"],
         ["THIS MESSAGE HAS NUMBERS 2023", "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"],
]

for test in tests
  message = test[0]; square = test[1];
  encrypted = encrypt(message, square)
  decrypted = decrypt(encrypted, square)

  puts "using the polybius:"
  printSquare(square)
  puts "the plain message:", message 
  puts "encrypted:", encrypted 
  puts "decrypted:", decrypted 
  puts "===================================================="
end


  

You may also check:How to resolve the algorithm Vector products step by step in the jq programming language
You may also check:How to resolve the algorithm Sort a list of object identifiers step by step in the C# programming language
You may also check:How to resolve the algorithm Compound data type step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Longest common subsequence step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the ooRexx programming language