Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. // Impliments the classical cipher "Playfair"
  2. // Usage: cat key.txt message.txt | playfair message -[e/d]
  3. // The decrypted message will be missing spaces and have an x
  4. // in place of every double letter.
  5. //////////////////////////////////////////////////////////////*/
  6.  
  7. #include<iostream>
  8. #include<vector>
  9. #include<string.h>
  10.  
  11. using namespace std;
  12.  
  13. char mat[5][5];
  14. void createMatrix(char key[]);
  15. void prepareText(char origmessage[], char message[]);
  16. void encipher(char message[]);
  17. void decipher(char message[]);
  18.  
  19. int main(int argc, char** argv)
  20. {
  21. char message[1000];
  22. char origmessage[1000];
  23. char key[25];
  24.  
  25. if (argc != 2)
  26. {
  27. cout << "Incorrect number of command line arguments, " << endl
  28. << "Usage: cat key.txt message.txt | playfair message -[e/d] " << endl;
  29. return false;
  30. }
  31.  
  32. cin >> key;
  33. cin >> origmessage;
  34.  
  35. // function creates the matrix
  36. createMatrix(key);
  37.  
  38. // encipher/decipher flag
  39. switch (argv[1][1]) {
  40. case 'e':
  41. // removes spaces and punctuation, checks for pairs of double letters
  42. prepareText(origmessage, message);
  43. // turns plaintext into ciphertext
  44. encipher(message);
  45. case 'd':
  46. // turns ciphertext into plaintext
  47. decipher(message);
  48. }
  49.  
  50. return 0;
  51. }
  52.  
  53. void createMatrix(char key[])
  54. {
  55. int i = 0, j = 0, k = 0;
  56. int letter = 'a';
  57. bool ok = true;
  58. char used[25];
  59.  
  60. // keep going until the key runs out
  61. while(key[k] != '0')
  62. {
  63. // we throw out the letter j
  64. if(key[k] == 'j')
  65. key[k] = 'i';
  66.  
  67. // end of row
  68. if (j > 4)
  69. {
  70. j = 0;
  71. i++;
  72. }
  73.  
  74. // check to see if the letter is a duplicate
  75. for(int check = 0; check < k; check++)
  76. {
  77. if(used[check]==key[k])
  78. ok = false;
  79. }
  80. // If not a duplicate add it to the matrix
  81. if(ok)
  82. {
  83. cout << mat[i][j];
  84. mat[i][j] = key[k];
  85. }
  86. j++;
  87. k++;
  88. }
  89.  
  90. // Add remaining letters of the alphabet to the matrix
  91. for(int remain = k; remain < 25; remain ++)
  92. {
  93. // we throw out the letter j
  94. if(letter == 'j')
  95. letter++;
  96.  
  97. // end of row in matrix
  98. if (j > 4)
  99. {
  100. j = 0;
  101. i++;
  102. }
  103.  
  104. // check to see if the letter has been used
  105. for(int check = 0; check < k; check ++)
  106. {
  107. if(used[check] == letter)
  108. ok = false;
  109. }
  110.  
  111. // if letter hasn't been used add it to the matrix
  112. if(ok)
  113. {
  114. mat[i][j] = letter;
  115. letter++;
  116. }
  117. }
  118.  
  119. // print out the matrix to test it
  120. for(int m=0; m < 5; m++)
  121. for(int n = 0; n < 5; n++)
  122. {
  123. cout << mat[m][n] << " ";
  124. }
  125. }
  126.  
  127.  
  128. // remove spaces and punctuation, checks for pairs of double letters
  129. void prepareText(char origmessage[], char message[])
  130. {
  131. int stepO = 0;
  132. int stepM = 0;
  133.  
  134. //transform(origmessage.begin(), origmessage.end(), origmessage.begin(), ::tolower);
  135.  
  136. while(!origmessage[stepO])
  137. {
  138. if(origmessage[stepO] >= 'a' && origmessage[stepO] <= 'z')
  139. {
  140. if(stepM%2 == 1)
  141. {
  142. if(origmessage[stepO] == origmessage[stepO-1])
  143. {
  144. message[stepM] = 'x';
  145. }
  146. else
  147. {
  148. message[stepM] = origmessage[stepO];
  149. stepO++;
  150. }
  151.  
  152. }
  153. else
  154. {
  155. message[stepM] = origmessage[stepO];
  156. stepO++;
  157. }
  158. }
  159. stepM++;
  160. }
  161. }
  162.  
  163. // this turns our plaintext into cipher text
  164. void encipher(char message[])
  165. {
  166. int x1, x2, y1, y2, j, k;
  167.  
  168. // the first 50 characters of the plaintext
  169. for(int i =0; i < 50; i++)
  170. {
  171. j = 0;
  172. k = 0;
  173.  
  174. // find the 1st character from the message in the matrix
  175. while(message[i] != mat[j][k] && k < 5)
  176. {
  177. j++;
  178. if(j>4)
  179. {
  180. j=0;
  181. k++;
  182. }
  183. }
  184. x1 = j; // row number
  185. y1 = k; // column number
  186.  
  187. j=0;
  188. k=0;
  189.  
  190. // find the 2nd character from the message in the matrix
  191. while(message[i+1] == mat[j][k])
  192. {
  193. j++;
  194. if(j>4)
  195. {
  196. j=0;
  197. k++;
  198. }
  199. }
  200. x2 = j; // row number
  201. y2 = k; // column number
  202.  
  203. // if they are not in the same row or column
  204. if(x1!=x2 && y1!=y2)
  205. {
  206. message[i] = mat[x1][y2];
  207. message[i+1] = mat[x2][y1];
  208. }
  209.  
  210. // if they in the same row
  211. else if(y1 != y2)
  212. {
  213. if(x1 == 4)
  214. message[i] = mat[0][y1];
  215. else
  216. message[i] = mat[x1+1][y1];
  217. if(x2 == 4)
  218. message[i+1] = mat[0][y2];
  219. else
  220. message[i+1] = mat[x2+1][y2];
  221. }
  222. // if they are in the same column
  223. else
  224. {
  225. if(y1 == 4)
  226. message[i] = mat[x1][0];
  227. else
  228. message[i] = mat[x1][y1+1];
  229. if(y2 == 4)
  230. message[i+1] = mat[x2][0];
  231. else
  232. message[i+1] = mat[x2][y2+1];
  233. }
  234. i++;
  235. }
  236. }
  237. void decipher(char message[])
  238. {
  239. int x1, x2, y1, y2, j, k;
  240.  
  241. // the first 50 characters of the plaintext
  242. for(int i =0; i < 50; i++)
  243. {
  244.  
  245. j = 0;
  246. k = 0;
  247.  
  248. // find the 1st character from the message in the matrix
  249. while(message[i] != mat[j][k] && k < 5)
  250. {
  251. j++;
  252. if(j>4)
  253. {
  254. j=0;
  255. k++;
  256. }
  257. }
  258. x1 = j; // row number
  259. y1 = k; // column number
  260.  
  261. j=0;
  262. k=0;
  263.  
  264. // find the 2nd character from the message in the matrix
  265. while(message[i+1] == mat[j][k])
  266. {
  267. j++;
  268. if(j>4)
  269. {
  270. j=0;
  271. k++;
  272. }
  273. }
  274. x2 = j; // row number
  275. y2 = k; // column number
  276.  
  277. // if they are not in the same row or column
  278. if(x1!=x2 && y1!=y2)
  279. {
  280. message[i] = mat[x1][y2];
  281. message[i+1] = mat[x2][y1];
  282. }
  283.  
  284. // if they in the same row
  285. else if(y1 != y2)
  286. {
  287. if(x1 == 0)
  288. message[i] = mat[4][y1];
  289. else
  290. message[i] = mat[x1-1][y1];
  291. if(x2 == 0)
  292. message[i+1] = mat[4][y2];
  293. else
  294. message[i+1] = mat[x2-1][y2];
  295. }
  296. // if they are in the same column
  297. else
  298. {
  299. if(y1 == 0)
  300. message[i] = mat[x1][4];
  301. else
  302. message[i] = mat[x1][y1-1];
  303. if(y2 == 0)
  304. message[i+1] = mat[x2][4];
  305. else
  306. message[i+1] = mat[x2][y2-1];
  307. }
  308. i++;
  309. }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement