Users Online

· Guests Online: 42

· 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 Encrypt Message using Playfair Cipher

C++ Program to Encrypt Message using Playfair Cipher

 

 

This C++ program encodes any message using the technique of traditional playfair cipher. Input is not case sensitive and works only for characters from ‘a’ to ‘z’ and ‘A’ to ‘Z’. White spaces are ignored.

 

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

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

 

Output:
 
Enter Message to Encrypt:Balloon
CODE: CBNVMPPO

 

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.88 seconds
10,820,498 unique visits