Polybius Square Cipher In C++

A table that allows someone to translate letters into numbers is called a Polybius Square . This table can be shared with the receiver and randomly generated to increase the difficulty of the encryption. The letters "i" and "j" are typically combined into a single cell to accommodate the 26 alphabetic letters inside the 25 cells that the table creates. Due to the 24 letters of the ancient Greek alphabet, there was never a difficulty.

A simple substitution cipher called the Polybius Square Cipher , which is frequently used to encode messages by transforming each letter or symbol into a pair of numerical coordinates.

How does it operates?

  • Make a 5 × 5 grid and fill it with the alphabet's letters (except from "J" ). The letters A through I and then K through Z can be used to fill the grid row by row.
  • Example
    
    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
    
  • Replace each letter in the grid with the relevant row and column numbers to encrypt a message. To illustrate, the character 'C' would be represented by the code "11" (row 1, column 1).
  • All you have to do is turn each set of digits back into the matching letter to decipher a message.
  • Pseudocode:

    Example
    
    function encryptionx(plaintxt):
        grids = createGrids()
        ciphertxt = ""
        for each character in plaintxt:
            if the character is in the grids:
                find the coordinates of the character in the grids
                append the coordinates to ciphertxt
        return ciphertxt
    
    function decryptionx(ciphertxt):
        grids = createGrids()
        plaintxt = ""
        for each pair of characters in ciphertxt:
            find the character at the coordinates in the grids
            append the character to plaintxt
        return plaintxt
    
    function createGrids():
        grids = 5x5 array of characters
        fill the grids with the letters A - Z(excluding J)
        return grids
    

    Program 1:

Let us take an example to illustrate the Polybius Square Cipher in C++.

Example

#include <iostream>
#include <string>
#include <map>
class Polybius {
private:
    std::map<char, std::string> encryptMap;
    std::map<std::string, char> decryptMap;
public:
    Polybius() {
        // Initialize the encryption and decryption maps
        char row = 'A';
        char col = 'A';
        for (int x= 0; x < 28; ++x) {
            std::string coords = std::string(1, row) + std::string(1, col);
            encryptMap['A' + x] = coords;
            decryptMap[coords] = 'A' + x;
            ++col;
            if (col > 'Z') {
                col = 'A';
                ++row;
            }
        }
        // Add the last character ('Z') at the end of the grid
        encryptMap['Z'] = "63";
        decryptMap["63"] = 'Z';
    }
    std::string encrypt(const std::string& plaintext) {
        std::string ciphertext;
        for (char c: plaintext) {
            if (std::isalpha(c)) {
                ciphertext += encryptMap[std::toupper(c)];
            }
        }
        return ciphertext;
    }
    std::string decrypt(const std::string& ciphertext) {
        std::string plaintext;
        for (size_t i = 0; i < ciphertext.length(); i += 2) {
            std::string coords = ciphertext.substr(i, 2);
            plaintext += decryptMap[coords];
        }
        return plaintext;
    }
};
int main() {
    Polybius ciphers;
    std::string plaintext = "HELLO";
    std::string ciphertext = ciphers.encrypt(plaintext);
    std::cout << "Encrypted: " << ciphertext << std::endl;
    std::string decrypted = ciphers.decrypt(ciphertext);
    std::cout << "Decrypted: " << decrypted << std::endl;
    return 0;
}

Output:

Program 2:

Let us take another example to illustrate the Polybius Square Cipher in C++.

Example

#include <iostream>
#include <string>
#include <cctype>
class Polybius {
public:
    std::string encrypt(const std::string& plaintext) {
        std::string ciphertext;
        for (char c: plaintext) {
            if (std::isalpha(c)) {
                int rows, cols;
                getCoordinates(c, rows, cols);
                ciphertext += std::to_string(rows) + std::to_string(cols);
            }
        }
        return ciphertext;
    }

    std::string decrypt(const std::string& ciphertext) {
        std::string plaintext;
        for (size_t x = 0; x < ciphertext.length(); x += 2) {
            int rows = ciphertext[x] - '0';
            int cols = ciphertext[x + 1] - '0';
            plaintext += getCharacter(rows, cols);
        }
        return plaintext;
    }

private:
    void getCoordinates(char c, int& rows, int& cols) {
        c = std::toupper(c);
        if (c == 'J')
            c = 'I'; // Treat 'J' as 'I'
        rows = (c - 'A') / 5 + 1;
        cols = (c - 'A') % 5 + 1;
    }

    char getCharacter(int row, int col) {
        char c = (row - 1) * 5 + col - 1 + 'A';
        if (c >= 'J')
            c++; // Skip 'J'
        return c;
    }
};

int main() {
    Polybius cipher;
    std::string plaintext = "HELLO";
    std::string ciphertext = cipher.encrypt(plaintext);
    std::cout << "Encrypted: " << ciphertext << std::endl;
    std::string decrypted = cipher.decrypt(ciphertext);
    std::cout << "Decrypted: " << decrypted << std::endl;
    return 0;
}

Output:

The Polybius Square Cipher has the benefit of being straightforward, which makes it simple to use and understand. Its vulnerability to frequency analysis and other traditional cryptanalysis techniques, makes it less secure than more recent encryption schemes.

In summary, the Polybius Square Cipher is not appropriate for use in scenarios requiring strong encryption, even if it can be a helpful tool for encoding communications or instructional reasons when high security is not a concern.

Input Required

This code uses input(). Please provide values below: