playfair.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4. int main() {
  5. int x,y,i,j,k,count=0;
  6. string keyword,message,new_message,encrypted_text,decrypted_text,row_coordinates,column_coordinates,location_of_x;
  7. cout<<"\nEnter the size of matrix \n";
  8. cin>>x>>y;
  9. char matrix[x][y];
  10. map<char,int> flag;
  11. memset(matrix, '0', sizeof(matrix[0][0]) * x * y);
  12. cout<<"Enter the keyword\n";
  13. cin>>keyword;
  14. cout<<"Enter the message to be encrypted: \n";
  15. cin>>message;
  16. for(i=0;message[i]!='\0';i++)
  17. {
  18. if((message[i]==message[i-1])&&count%2!=0)
  19. {
  20. new_message+='x';
  21. location_of_x+=i+'0';
  22. count++;
  23. }
  24. new_message+=message[i];
  25. count++;
  26. }
  27. if(count%2==1)
  28. {
  29. new_message+='x';
  30. location_of_x+=i+'0';
  31. }
  32. for(i=0,k=0;keyword[i]!='\0';i++,k++)
  33. {
  34. matrix[0][k]=keyword[i];
  35. flag[keyword[i]]=1;
  36. for(j=0;new_message[j]!='\0';j++)
  37. if(keyword[i]==new_message[j])
  38. {
  39. row_coordinates+=new_message[j];
  40. column_coordinates+=new_message[j];
  41. row_coordinates+=(k/5)+'0';
  42. column_coordinates+=(k%5)+'0';
  43. }
  44. }
  45. for(i=97;i<=122;i++)
  46. {
  47. if(flag[(char)i]==0)
  48. {
  49. matrix[0][k]=(char)i;
  50. for(j=0;new_message[j]!='\0';j++)
  51. if(matrix[0][k]==new_message[j])
  52. {
  53. row_coordinates+=new_message[j];
  54. column_coordinates+=message[j];
  55. row_coordinates+=(k/5)+'0';
  56. column_coordinates+=(k%5)+'0';
  57. }
  58. k++;
  59. }
  60. }
  61. k=0;
  62. label:for(i=0;i<x&&new_message[k]!='\0';i++)
  63. for(j=0;j<y;j++)
  64. if(matrix[i][j]==new_message[k])
  65. {
  66. if(k%2==0)
  67. {
  68. int row_value=(int)row_coordinates[row_coordinates.find(new_message[k+1])+1]-48;
  69. int column_value=(int)column_coordinates[column_coordinates.find(new_message[k+1])+1]-48;
  70. if(row_value==i)
  71. {
  72. int column_value=j+1;
  73. if(column_value>=y)
  74. column_value=0;
  75. encrypted_text+=matrix[row_value][column_value];
  76. }
  77. else if(column_value==j)
  78. {
  79. int row_value=i+1;
  80. if(row_value>=x)
  81. row_value=0;
  82. encrypted_text+=matrix[row_value][column_value];
  83. }
  84. else
  85. encrypted_text+=matrix[row_value][j];
  86. }
  87. if(k%2==1)
  88. {
  89. int row_value=(int)row_coordinates[row_coordinates.find(new_message[k-1])+1]-48;
  90. int column_value=(int)column_coordinates[column_coordinates.find(new_message[k-1])+1]-48;
  91. if(row_value==i)
  92. {
  93. int column_value=j+1;
  94. if(column_value>=y)
  95. column_value=0;
  96. encrypted_text+=matrix[row_value][column_value];
  97. }
  98. else if(column_value==j)
  99. {
  100. int row_value=i+1;
  101. if(row_value>=x)
  102. row_value=0;
  103. encrypted_text+=matrix[row_value][column_value];
  104. }
  105. else
  106. encrypted_text+=matrix[row_value][j];
  107. }
  108. k++;
  109. goto label;
  110. }
  111. cout<<"Encrypted text is: "<<encrypted_text<<endl;
  112. new_message=row_coordinates=column_coordinates="";
  113. new_message=encrypted_text;
  114. for(i=0,k=0;keyword[i]!='\0';i++,k++)
  115. {
  116. matrix[0][k]=keyword[i];
  117. flag[keyword[i]]=1;
  118. for(j=0;new_message[j]!='\0';j++)
  119. if(keyword[i]==new_message[j])
  120. {
  121. row_coordinates+=new_message[j];
  122. column_coordinates+=new_message[j];
  123. row_coordinates+=(k/5)+'0';
  124. column_coordinates+=(k%5)+'0';
  125. }
  126. }
  127. for(i=97;i<=122;i++)
  128. {
  129. if(flag[(char)i]==0)
  130. {
  131. matrix[0][k]=(char)i;
  132. for(j=0;new_message[j]!='\0';j++)
  133. if(matrix[0][k]==new_message[j])
  134. {
  135. row_coordinates+=new_message[j];
  136. column_coordinates+=new_message[j];
  137. row_coordinates+=(k/5)+'0';
  138. column_coordinates+=(k%5)+'0';
  139. }
  140. k++;
  141. }
  142. }
  143. k=count=0;
  144. label2:for(i=0;i<x&&encrypted_text[k]!='\0';i++)
  145. {
  146. if(k==(location_of_x[count]-48))
  147. {
  148. count++;
  149. k++;
  150. continue;
  151. }
  152. for(j=0;j<y;j++)
  153. if(matrix[i][j]==encrypted_text[k])
  154. {
  155. if(k%2==0)
  156. {
  157. int row_value=(int)row_coordinates[row_coordinates.find(encrypted_text[k+1])+1]-48;
  158. int column_value=(int)column_coordinates[column_coordinates.find(encrypted_text[k+1])+1]-48;
  159. if(row_value==i)
  160. {
  161. cout<<"111111"<<endl;
  162. int column_value=j-1;
  163. if(column_value<0)
  164. column_value=y-1;
  165. decrypted_text+=matrix[row_value][column_value];
  166. }
  167. else if(column_value==j)
  168. {
  169. int row_value=i-1;
  170. if(row_value<0)
  171. row_value=x-1;
  172. decrypted_text+=matrix[row_value][column_value];
  173. }
  174. else
  175. decrypted_text+=matrix[row_value][j];
  176. }
  177. if(k%2==1)
  178. {
  179. int row_value=(int)row_coordinates[row_coordinates.find(encrypted_text[k-1])+1]-48;
  180. int column_value=(int)column_coordinates[column_coordinates.find(encrypted_text[k-1])+1]-48;
  181. if(row_value==i)
  182. {
  183. int column_value=j-1;
  184. if(column_value<0)
  185. column_value=y-1;
  186. decrypted_text+=matrix[row_value][column_value];
  187. }
  188. else if(column_value==j)
  189. {
  190. int row_value=i-1;
  191. if(row_value<0)
  192. row_value=x-1;
  193. decrypted_text+=matrix[row_value][column_value];
  194. }
  195. else
  196. decrypted_text+=matrix[row_value][j];
  197. }
  198. k++;
  199. goto label2;
  200. }
  201. }
  202. cout<<"Decrypted text is: "<<decrypted_text<<endl;
  203. return 0;
  204. }
  205. /*
  206. OUTPUT:-
  207. Enter the size of matrix
  208. 5 5
  209. Enter the keyword
  210. monarchy
  211. Enter the message to be encrypted:
  212. vitpune
  213. Encrypted text is: gwkvovtj
  214. Decrypted text is: vitpune
  215. Enter the size of matrix
  216. 5 5
  217. Enter the keyword
  218. monarchy
  219. Enter the message to be encrypted:
  220. balloon
  221. Encrypted text is: ibusuhna
  222. Decrypted text is: balloon
  223. */