Polybius Square Cipher In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Polybius Square Cipher In C++

Polybius Square Cipher In C++

BLUF: Mastering Polybius Square Cipher In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Polybius Square Cipher In C++

C++ is renowned for its efficiency. Learn how Polybius Square Cipher In C++ enables low-level control and high-performance computing in the tutorial below.

A grid that facilitates the conversion of letters to numbers is known as a Polybius Square. This grid can be exchanged with the recipient and generated randomly to enhance the complexity of the encoding process. The characters "i" and "j" are usually grouped together in one cell to fit all 26 alphabetic letters within the 25 cells of the table. The ancient Greek alphabet with its 24 letters never posed a challenge in this regard.

A basic encryption method known as the Polybius Square Cipher is commonly employed to encode communication by converting individual characters or symbols into a set of numeric coordinates.

Create a 5 × 5 matrix and populate it with letters from the alphabet, excluding the letter "J". Fill the grid sequentially row by row using the letters A to I and then K to Z.

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's consider an example to demonstrate the implementation of the Polybius Square Cipher in the C++ programming language.

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's consider another instance to demonstrate the implementation of the Polybius Square Cipher in the C++ programming language.

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 offers the advantage of being uncomplicated, making it easy to utilize and comprehend. However, its susceptibility to frequency analysis and other conventional cryptanalysis methods renders it less robust compared to modern encryption algorithms.

In essence, the Polybius Square Cipher is unsuitable for applications demanding robust encryption, despite its utility in encoding messages or educational contexts where top-level security is unnecessary.

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience