Open In App

Polybius Square Cipher

Last Updated : 15 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Polybius Square is a table that allows someone to convert letters into numbers. To make the encryption little harder, this table can be randomized and shared with the recipient. In order to fit the 26 letters of the alphabet into the 25 cells created by the table, the letters ‘i’ and ‘j’ are usually combined into a single cell. Originally there was no such problem because the ancient greek alphabet has 24 letters. 

 A table of bigger size could be used if a language contain large number of alphabets. 

Examples:

Input :  bus
Output : 124543
Input : geeksforgeeks
Output : 22151525432134422215152543

Implementation:

C++




// CPP Program to implement polybius cipher
#include <cmath>
#include <iostream>
using namespace std;
 
// function to display polybius cipher text
void polybiusCipher(string s) {
  int row, col;
 
  // convert each character to its encrypted code
  for (int i = 0; s[i]; i++) {
 
    // finding row of the table
    row = ceil((s[i] - 'a') / 5) + 1;
 
    // finding column of the table
    col = ((s[i] - 'a') % 5) + 1;
 
    // if character is 'k'
    if (s[i] == 'k') {
      row = row - 1;
      col = 5 - col + 1;
    }
 
    // if character is greater than 'j'
    else if (s[i] >= 'j') {
      if (col == 1) {
        col = 6;
        row = row - 1;
      }
      col = col - 1;
    }
    cout << row << col;
  }
 
  cout << endl;
}
 
// Driver's Code
int main() {
  string s = "geeksforgeeks";
  polybiusCipher(s);
  return 0;
}


Java




// Java Program to implement polybius cipher
 
class GFG
{
    // Function to display polybius
    // cipher text
    static void polybiusCipher(String s)
    {
        int row, col;
         
        // convert each character
        // to its encrypted code
        for (int i = 0;i < s.length(); i++)
        {
         
            // finding row of the table
            row = (int)Math.ceil((s.charAt(i) - 'a') / 5) + 1;
         
            // finding column of the table
            col = ((s.charAt(i) - 'a') % 5) + 1;
         
            // if character is 'k'
            if (s.charAt(i) == 'k')
            {
                row = row - 1;
                col = 5 - col + 1;
            }
         
            // if character is greater than 'j'
            else if (s.charAt(i) >= 'j')
            {
                if (col == 1)
                {
                    col = 6;
                    row = row - 1;
                }
                col = col - 1;
            }
            System.out.print(row +""+ col);
        }
         
        System.out.println();
    }
     
    // Driver code
    public static void main (String[] args)
    {
        String s = "geeksforgeeks";
        polybiusCipher(s);
    }
}
 
// This code is contributed by Anant Agarwal.


Python




# Python Program to implement polybius cipher
 
# function to display polybius cipher text
def polybiusCipher(s):
 
        # convert each character to its encrypted code
        for char in s:
             
                # finding row of the table
                row = int((ord(char) - ord('a')) / 5) + 1
         
                # finding column of the table
                col = ((ord(char) - ord('a')) % 5) + 1
 
                # if character is 'k'
                if char == 'k':
                        row = row - 1
                        col = 5 - col + 1
                         
                # if character is greater than 'j'
                elif ord(char) >= ord('j'):
                        if col == 1 :
                            col = 6
                            row = row - 1
                             
                        col = col - 1
                         
                print(row, col, end ='', sep ='')
 
# Driver's Code
if __name__ == "__main__":
 
        s = "geeksforgeeks"
 
        # print the cipher of "geeksforgeeks"
        polybiusCipher(s)


C#




// C# Program to implement
// polybius cipher
using System;
 
class GFG
{
    // Function to display
    // polybius cipher text
    static void polybiusCipher(string s)
    {
        int row, col;
         
        // convert each character
        // to its encrypted code
        for (int i = 0;
                 i < s.Length; i++)
        {
            // finding row of the table
            row = (int)Math.Floor((s[i] -
                         'a') / 5.0) + 1;
             
            // finding column
            // of the table
            col = ((s[i] - 'a') % 5) + 1;
         
            // if character is 'k'
            if (s[i] == 'k')
            {
                row = row - 1;
                col = 5 - col + 1;
            }
         
            // if character is
            // greater than 'j'
            else if (s[i] >= 'j')
            {
                if (col == 1)
                {
                    col = 6;
                    row = row - 1;
                }
                col = col - 1;
            }
            Console.Write(row +
                          "" + col);
        }
        Console.WriteLine();
    }
     
    // Driver code
    static void Main ()
    {
        string s = "geeksforgeeks";
        polybiusCipher(s);
    }
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)


Javascript




// JavaScript program to implement Polybius Cipher
 
function polybiusCipher(s) {
 
    let row, col;
    let result = "";
 
    // convert each character
    // to its encrypted code
    for (let i = 0; i < s.length; i++) {
 
        // finding row of the table
        if (s.charAt(i).toLowerCase() === 'j') {
            row = 2;
            col = 4;
        } else {
            let code = s.charAt(i).toLowerCase().charCodeAt(0) - 'a'.charCodeAt(0);
            if (code >= 9) code--; // excluding j, shift remaining codes by 1
            row = Math.floor(code / 5) + 1;
            col = (code % 5) + 1;
        }
 
        result += row + "" + col;
    }
 
    document.write(result);
}
 
// Driver code
let s = "geeksforgeeks";
polybiusCipher(s);


PHP




<?php
// PHP Program to implement
// polybius cipher
 
// function to display
// polybius cipher text
function polybiusCipher($s)
{
$row = 0;
$col = 0;
 
// convert each character
// to its encrypted code
for ($i = 0;
     $i < strlen($s); $i++)
{
 
    // finding row
    // of the table
    $row = floor((ord($s[$i]) -
                  ord('a')) / 5) + 1;
 
    // finding column
    // of the table
    $col = ((ord($s[$i]) -
             ord('a')) % 5) + 1;
 
    // if character is 'k'
    if ($s[$i] == 'k')
    {
        $row = $row - 1;
        $col = 5 - $col + 1;
    }
 
    // if character is
    // greater than 'j'
    else if ($s[$i] >= 'j')
    {
        if ($col == 1)
        {
            $col = 6;
            $row = $row - 1;
        }
        $col = $col - 1;
    }
    echo ($row.$col);
}
echo ("\n");
}
 
// Driver Code
$s = "geeksforgeeks";
polybiusCipher($s);
 
// This code is contributed by
// Manish Shaw(manishshaw1)
?>


Output

22151525432134422215152543


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads