How to resolve the algorithm Playfair cipher step by step in the C++ programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Playfair cipher step by step in the C++ programming language
Table of Contents
Problem Statement
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet. The output of the encrypted and decrypted message must be in capitalized digraphs, separated by spaces.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Playfair cipher step by step in the C++ programming language
The provided code implements the Playfair cipher, a classic encryption technique.
Class Overview:
The code defines a playfair
class that handles the encryption/decryption process.
Methods:
doIt(string k, string t, bool ij, bool e)
: The main function that takes the key, text, and flags for handling 'J' and 'Q' characters and the encryption/decryption mode (e
for encryption,-e
for decryption).
Private Methods:
doIt(int dir)
: Performs the actual encryption/decryption by iterating over the text, finding character positions in the grid, and applying the Playfair algorithm based on the direction (dir
).display()
: Displays the encrypted/decrypted text.getChar(int a, int b)
: Retrieves a character from the grid at the given position.getCharPos(char l, int &a, int &b)
: Finds the position of a character in the grid and returnstrue
if found.getTextReady(string t, bool ij, bool e)
: Prepares the text for encryption by handling duplicate characters, replacing 'J' with 'I' (ifij
istrue
), and removing 'Q' (ifij
isfalse
).createGrid(string k, bool ij)
: Creates the 5x5 Playfair grid using the given key and handling 'J' and 'Q' characters.
Main Function:
- The program starts by prompting the user to select encryption or decryption mode (
e
ord
) and enters the key, 'J' handling mode, and the text to be encrypted/decrypted. - It then creates an instance of the
playfair
class and callsdoIt()
to perform the encryption/decryption, resulting in the encrypted/decrypted text.
Playfair Algorithm:
The Playfair algorithm works as follows:
- It creates a 5x5 grid using the key and the alphabet.
- It replaces 'J' with 'I' and removes 'Q' from the text.
- It divides the text into pairs of characters, adding 'X' if necessary.
- For each pair of characters, it finds their positions in the grid.
- If the characters are in the same row or column, it replaces them with the characters to their right or below, respectively.
- If the characters are not in the same row or column, it replaces them with the characters that form the opposite corners of the rectangle they form in the grid.
- The encrypted/decrypted text is formed by concatenating the replaced characters.
Source code in the cpp programming language
#include <iostream>
#include <string>
using namespace std;
class playfair
{
public:
void doIt( string k, string t, bool ij, bool e )
{
createGrid( k, ij ); getTextReady( t, ij, e );
if( e ) doIt( 1 ); else doIt( -1 );
display();
}
private:
void doIt( int dir )
{
int a, b, c, d; string ntxt;
for( string::const_iterator ti = _txt.begin(); ti != _txt.end(); ti++ )
{
if( getCharPos( *ti++, a, b ) )
if( getCharPos( *ti, c, d ) )
{
if( a == c ) { ntxt += getChar( a, b + dir ); ntxt += getChar( c, d + dir ); }
else if( b == d ){ ntxt += getChar( a + dir, b ); ntxt += getChar( c + dir, d ); }
else { ntxt += getChar( c, b ); ntxt += getChar( a, d ); }
}
}
_txt = ntxt;
}
void display()
{
cout << "\n\n OUTPUT:\n=========" << endl;
string::iterator si = _txt.begin(); int cnt = 0;
while( si != _txt.end() )
{
cout << *si; si++; cout << *si << " "; si++;
if( ++cnt >= 26 ) cout << endl, cnt = 0;
}
cout << endl << endl;
}
char getChar( int a, int b )
{
return _m[ (b + 5) % 5 ][ (a + 5) % 5 ];
}
bool getCharPos( char l, int &a, int &b )
{
for( int y = 0; y < 5; y++ )
for( int x = 0; x < 5; x++ )
if( _m[y][x] == l )
{ a = x; b = y; return true; }
return false;
}
void getTextReady( string t, bool ij, bool e )
{
for( string::iterator si = t.begin(); si != t.end(); si++ )
{
*si = toupper( *si ); if( *si < 65 || *si > 90 ) continue;
if( *si == 'J' && ij ) *si = 'I';
else if( *si == 'Q' && !ij ) continue;
_txt += *si;
}
if( e )
{
string ntxt = ""; size_t len = _txt.length();
for( size_t x = 0; x < len; x += 2 )
{
ntxt += _txt[x];
if( x + 1 < len )
{
if( _txt[x] == _txt[x + 1] ) ntxt += 'X';
ntxt += _txt[x + 1];
}
}
_txt = ntxt;
}
if( _txt.length() & 1 ) _txt += 'X';
}
void createGrid( string k, bool ij )
{
if( k.length() < 1 ) k = "KEYWORD";
k += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string nk = "";
for( string::iterator si = k.begin(); si != k.end(); si++ )
{
*si = toupper( *si ); if( *si < 65 || *si > 90 ) continue;
if( ( *si == 'J' && ij ) || ( *si == 'Q' && !ij ) )continue;
if( nk.find( *si ) == -1 ) nk += *si;
}
copy( nk.begin(), nk.end(), &_m[0][0] );
}
string _txt; char _m[5][5];
};
int main( int argc, char* argv[] )
{
string key, i, txt; bool ij, e;
cout << "(E)ncode or (D)ecode? "; getline( cin, i ); e = ( i[0] == 'e' || i[0] == 'E' );
cout << "Enter a en/decryption key: "; getline( cin, key );
cout << "I <-> J (Y/N): "; getline( cin, i ); ij = ( i[0] == 'y' || i[0] == 'Y' );
cout << "Enter the text: "; getline( cin, txt );
playfair pf; pf.doIt( key, txt, ij, e ); return system( "pause" );
}
You may also check:How to resolve the algorithm Benford's law step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Balanced brackets step by step in the Swift programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the Zoomscript programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the Clojure programming language