Cryptography  1.0
Implementation of Cryptography Algorithms
Macros | Functions
playfair.cpp File Reference
#include <bits/stdc++.h>
Include dependency graph for playfair.cpp:

Macros

#define ll   long long
 
#define pb   push_back
 
#define mp   make_pair
 

Functions

string implmatrix (string s, char mat[][5])
 
int main ()
 

Macro Definition Documentation

#define ll   long long
#define mp   make_pair
#define pb   push_back

Function Documentation

string implmatrix ( string  s,
char  mat[][5] 
)

The matrix obtained in the main function is now used here to generate the cipher text. According to playfair algorithm the conditions for swap vary according to the locations of the digrams in the supplied matrix.

< Iteration Variables for the matrix

First case handling

If the the digrams are not located in the same column and same row then we swap the take the digit in the same row but column number of the second member of the diagram and vice versa for the second element of the digram

Second case handling

If the the digrams are located in the same row and different column then we swap the take the digit in the same row but next column number in that row and same is done for the second element of the digram.

Third case handling

If the the digrams are located in the same colum and different row then we swap the take the digit in the same column but next row number in that row and same is done for the second element of the digram.

Parameters
sString to be encrypted
10  {
11  pair<int,int> p[2];
12  int i,j,k;
13  for(k=0;k<2;k++) {
14  for(i=0;i<5;i++)
15  for(j=0;j<5;j++)
16  if(mat[i][j] == s[k]) {
17  p[k] = mp(i,j);
18  }
19  }
20  /*Adhering the conditions for swapping values*/
22 
26  if(p[0].first != p[1].first && p[0].second != p[1].second) {
27  s[0] = mat[p[0].first][p[1].second];
28  s[1] = mat[p[1].first][p[0].second];
29  }
31 
35  else if(p[0].first == p[1].first) {
36  s[0] = mat[p[0].first][(p[0].second + 1) % 5];
37  s[1] = mat[p[1].first][(p[1].second + 1) % 5];
38  }
40 
44  else if(p[0].second == p[1].second) {
45  s[0] = mat[(p[0].first + 1) % 5][p[0].second];
46  s[1] = mat[(p[1].first + 1) % 5][p[1].second];
47  }
48  return s;
49 }
first
Definition: DESKeygen.py:200
second
Definition: DESKeygen.py:201
#define mp
Definition: playfair.cpp:4
int main ( )
50  {
51  string keyword = "PLAYFAIR";
52  string message = "SECRETMESSAGEISECRET";
53  char mat[5][5];
54  int alpha[26] = {0};
55  alpha[9] = 1;
56  int keyw_len = keyword.size();
57  int i, j, k, count = 0;
58  for (i = 0; i < 5; i++) {
59  for (j = 0; j < 5; j++) {
60  if (count<keyw_len) {
61  if(alpha[(int) (keyword[count] - 65)] == 0){mat[i][j] = (char) (keyword[count]);}
62  else{j--;}
63  alpha[(int) (keyword[count] - 65)] = 1;
64  count++;
65  } else {
66  for (k = 0; k < 26; k++) {
67  if (alpha[k] == 0) {
68  mat[i][j] = (char) (65 + k);
69  alpha[k] = 1;
70  break;
71  }
72 
73  }
74  }
75  }
76  }
77  for(i = 0;i< 5;i++) {
78  for (j = 0;j<5;j++) {
79  cout << mat[i][j] <<" ";
80  }
81  cout << endl;
82  }
83  /*
84  * Building the secret message for encryption
85  *
86  */
87  int msg_len = message.size();
88  vector<string> digrams;
89  for (i = 0; i < msg_len; i++) {
90  string ss;
91  if (message[i] == message[i + 1]) {
92  ss += message[i];
93  ss += 'X';
94  } else {
95  ss += message[i];
96  if (i + 1 < msg_len) { ss += message[i + 1]; }
97  i++;
98  }
99  digrams.pb(ss);
100  //cout << ss<<endl;
101  }
102 
103  if (digrams[digrams.size() - 1].length() == 1) {
104  digrams[digrams.size() - 1] += 'X';
105  }
106  int no_dig = digrams.size();
107  string encrypted_string;
108  for (i = 0; i < no_dig; i++) {
109  digrams[i].c_str();
110  string str = implmatrix(digrams[i], mat);
111  encrypted_string+=str;
112  }
113  cout << "The original string is :" << message <<endl;
114  cout << "The encrypted string is :" << encrypted_string << endl;
115  return 0;
116 }
string implmatrix(string s, char mat[][5])
Definition: playfair.cpp:10