Advanced Bash Scripting – Part 2

Shell ScriptThe problem statement is as follows :

The Playfair Cipher encrypts text by substitution of digrams (2-letter groupings). It is traditional to use a 5 x 5 letter scrambled-alphabet key square for the encryption and decryption.

Each letter of the alphabet appears once, except “I” also represents “J”. The arbitrarily chosen key word, “CODES” comes first, then all the rest of the alphabet, in order from left to right, skipping letters already used.

To encrypt, separate the plaintext message into digrams (2-letter groups). If a group has two identical letters, delete the second, and form a new group. If there is a single letter left over at the end, insert a “null” character, typically an “X.”

To read more about the question, check

The solution for this question is pretty long (over 100 lines in shell script). You can check the final solution at

Explanation of function/code used in the solution.

locateInKeySquare: Searches for the character passed as the parameter in the keySquare and returns the position of the character.
addToKeySquare: Adds a character c passed as the parameter to the keySquare. This checks if the character is already present, if it is J and changes it to uppercase (if needed)
printKeySquare: Prints the keySquare from 1D to 2D format.

The rest of the code adds the keyWord to keySquare. The the remaining words are added to the keySquare. After that, we extract the dialects from the plaintext and based on the 3 rules, we encrypt the text.

Hope this is helpful..

Leave a Reply

Your email address will not be published. Required fields are marked *

five + = 8

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>