Users Online

· Guests Online: 44

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C++ Program to Decrypt Message using Playfair Cipher

C++ Program to Decrypt Message using Playfair Cipher

 

 

This C++ program decodes any message encoded using the technique of traditional playfair cipher. The Playfair cipher or Playfair square is a manual symmetric encryption technique and was the first literal digraph substitution cipher. Input is not case sensitive and works only for characters from ‘a’ to ‘z’ and ‘A’ to ‘Z’.

 

This C++ program is successfully compiled and tested on our system. The program output is given below.

  1. /*
  2.  * C++ Program to Decode a Message Encoded Using Playfair Cipher
  3.  */
  4. #include<iostream>
  5. #include<vector>
  6. using namespace std;
  7.  
  8. const char encoder[5][5] = {{'A','B','C','D','E'},
  9.                       {'F','G','H','I','K'},
  10.                       {'L','M','N','O','P'},
  11.                       {'Q','R','S','T','U'},
  12.                       {'V','W','X','Y','Z'}};
  13.  
  14. void input_string(vector<char>& a)
  15. {
  16.     char c;
  17.     while (1)
  18.     {
  19.         c=getchar();
  20.         if (c >= 97 && c <= 122)
  21.         c -= 32;
  22.         if (c == '\n')
  23.             break;
  24.         else if (c==' ')
  25.             continue;
  26.         else if (c == 'J')
  27.         a.push_back('I');
  28.         a.push_back(c);
  29.     }
  30.     return;
  31. }
  32.  
  33.  
  34.  
  35. void get_pos(char p, int& r, int& c)
  36. {
  37.     if (p < 'J')
  38.     {
  39.         r = (p - 65) / 5;
  40.         c = (p - 65) % 5;
  41.     }
  42.     else if (p > 'J')
  43.     {
  44.         r = (p - 66) / 5;
  45.         c = (p - 66) % 5;
  46.     }
  47.     return;
  48. }
  49.  
  50. void same_row(int r, vector<char>& code, int c1, int c2)
  51. {
  52.     code.push_back(encoder[r][(c1 + 4) % 5]);
  53.     code.push_back(encoder[r][(c2 + 4) % 5]);
  54.     return;
  55. }
  56.  
  57. void same_column(int c, vector<char>& code, int r1, int r2)
  58. {
  59.     code.push_back(encoder[(r1 + 4) % 5][c]);
  60.     code.push_back(encoder[(r2 + 4) % 5][c]);
  61.     return;
  62. }
  63.  
  64. void diff_col_row(int r1, int c1, vector<char>& code, int r2, int c2)
  65. {
  66.     code.push_back(encoder[r1][c2]);
  67.     code.push_back(encoder[r2][c1]);
  68.     return;
  69. }
  70.  
  71.  
  72. void encode(vector<char> msgx, int len)
  73. {
  74.     vector<char> code;
  75.     int i = 0, j = 0;
  76.     int r1, c1, r2, c2;
  77.     while (i < len)
  78.     {
  79.         get_pos(msgx[i], r1, c1);
  80.         i++;
  81.         get_pos(msgx[i], r2, c2);
  82.         if (r1 == r2)
  83.         {
  84.             same_row(r1, code, c1, c2);
  85.         }
  86.         else if (c1 == c2)
  87.         {
  88.             same_column(c1, code, r1, r2);
  89.         }
  90.         else
  91.         {
  92.             diff_col_row(r1, c1, code, r2, c2);
  93.         }
  94.         i++;
  95.     }
  96.     cout<<"\nCODE: ";
  97.     for (j = 0;j < code.size();j++)
  98.     {
  99.         if (code[j] == 'X')
  100.         continue;
  101.         cout<<code[j];
  102.     }
  103.     return;
  104. }
  105.  
  106.  
  107. int main()
  108. {
  109.     vector<char> msg;
  110.     std::cout<<"Enter the Encrypted Message:";
  111.     input_string(msg);
  112.     int len=msg.size();
  113.     encode(msg,len);
  114.     return 0;
  115. }

 

Output:
 
Enter the Encrypted Message:CBNVMPPO
CODE: BALLOON

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.75 seconds
10,818,252 unique visits