Polybius Square Cipher
Last Updated :
15 Sep, 2023
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++
#include <cmath>
#include <iostream>
using namespace std;
void polybiusCipher(string s) {
int row, col;
for ( int i = 0; s[i]; i++) {
row = ceil ((s[i] - 'a' ) / 5) + 1;
col = ((s[i] - 'a' ) % 5) + 1;
if (s[i] == 'k' ) {
row = row - 1;
col = 5 - col + 1;
}
else if (s[i] >= 'j' ) {
if (col == 1) {
col = 6;
row = row - 1;
}
col = col - 1;
}
cout << row << col;
}
cout << endl;
}
int main() {
string s = "geeksforgeeks" ;
polybiusCipher(s);
return 0;
}
|
Java
class GFG
{
static void polybiusCipher(String s)
{
int row, col;
for ( int i = 0 ;i < s.length(); i++)
{
row = ( int )Math.ceil((s.charAt(i) - 'a' ) / 5 ) + 1 ;
col = ((s.charAt(i) - 'a' ) % 5 ) + 1 ;
if (s.charAt(i) == 'k' )
{
row = row - 1 ;
col = 5 - col + 1 ;
}
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();
}
public static void main (String[] args)
{
String s = "geeksforgeeks" ;
polybiusCipher(s);
}
}
|
Python
def polybiusCipher(s):
for char in s:
row = int (( ord (char) - ord ( 'a' )) / 5 ) + 1
col = (( ord (char) - ord ( 'a' )) % 5 ) + 1
if char = = 'k' :
row = row - 1
col = 5 - col + 1
elif ord (char) > = ord ( 'j' ):
if col = = 1 :
col = 6
row = row - 1
col = col - 1
print (row, col, end = ' ', sep =' ')
if __name__ = = "__main__" :
s = "geeksforgeeks"
polybiusCipher(s)
|
C#
using System;
class GFG
{
static void polybiusCipher( string s)
{
int row, col;
for ( int i = 0;
i < s.Length; i++)
{
row = ( int )Math.Floor((s[i] -
'a' ) / 5.0) + 1;
col = ((s[i] - 'a' ) % 5) + 1;
if (s[i] == 'k' )
{
row = row - 1;
col = 5 - col + 1;
}
else if (s[i] >= 'j' )
{
if (col == 1)
{
col = 6;
row = row - 1;
}
col = col - 1;
}
Console.Write(row +
"" + col);
}
Console.WriteLine();
}
static void Main ()
{
string s = "geeksforgeeks" ;
polybiusCipher(s);
}
}
|
Javascript
function polybiusCipher(s) {
let row, col;
let result = "" ;
for (let i = 0; i < s.length; i++) {
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--;
row = Math.floor(code / 5) + 1;
col = (code % 5) + 1;
}
result += row + "" + col;
}
document.write(result);
}
let s = "geeksforgeeks" ;
polybiusCipher(s);
|
PHP
<?php
function polybiusCipher( $s )
{
$row = 0;
$col = 0;
for ( $i = 0;
$i < strlen ( $s ); $i ++)
{
$row = floor ((ord( $s [ $i ]) -
ord( 'a' )) / 5) + 1;
$col = ((ord( $s [ $i ]) -
ord( 'a' )) % 5) + 1;
if ( $s [ $i ] == 'k' )
{
$row = $row - 1;
$col = 5 - $col + 1;
}
else if ( $s [ $i ] >= 'j' )
{
if ( $col == 1)
{
$col = 6;
$row = $row - 1;
}
$col = $col - 1;
}
echo ( $row . $col );
}
echo ( "\n" );
}
$s = "geeksforgeeks" ;
polybiusCipher( $s );
?>
|
Output
22151525432134422215152543
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...