fork download
  1. // PLAYFAIR CIPHER
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <cctype>
  7. using namespace std;
  8.  
  9. // Generate the 5x5 key table
  10. void generateKeyTable(const string &key, char table[5][5]) {
  11. bool used[26] = {};
  12. string processedKey = "";
  13.  
  14. for (char c : key) {
  15. c = tolower(c == 'j' ? 'i' : c);
  16. if (isalpha(c) && !used[c - 'a']) {
  17. processedKey += c;
  18. used[c - 'a'] = true;
  19. }
  20. }
  21.  
  22. for (char c = 'a'; c <= 'z'; ++c) {
  23. if (c == 'j') continue;
  24. if (!used[c - 'a']) {
  25. processedKey += c;
  26. used[c - 'a'] = true;
  27. }
  28. }
  29.  
  30. int idx = 0;
  31. for (int i = 0; i < 5; ++i)
  32. for (int j = 0; j < 5; ++j)
  33. table[i][j] = processedKey[idx++];
  34. }
  35.  
  36. // Find character positions in key table
  37. void findPosition(char table[5][5], char ch, int &row, int &col) {
  38. if (ch == 'j') ch = 'i';
  39. for (int i = 0; i < 5; ++i)
  40. for (int j = 0; j < 5; ++j)
  41. if (table[i][j] == ch) {
  42. row = i;
  43. col = j;
  44. return;
  45. }
  46. }
  47.  
  48. // Prepare the text (insert 'x' for repeated pairs, pad with 'z' if odd length)
  49. string prepareText(string text) {
  50. string result = "";
  51. for (char c : text)
  52. if (isalpha(c)) result += tolower(c == 'j' ? 'i' : c);
  53.  
  54. string finalText = "";
  55. for (size_t i = 0; i < result.length(); ++i) {
  56. finalText += result[i];
  57. if (i + 1 < result.length()) {
  58. if (result[i] == result[i + 1])
  59. finalText += 'x';
  60. else
  61. finalText += result[++i];
  62. }
  63. }
  64. if (finalText.length() % 2) finalText += 'z';
  65. return finalText;
  66. }
  67.  
  68. // Encrypt using Playfair cipher
  69. string encrypt(const string &text, const string &key) {
  70. char table[5][5];
  71. generateKeyTable(key, table);
  72. string prepared = prepareText(text), cipher = "";
  73.  
  74. for (size_t i = 0; i < prepared.length(); i += 2) {
  75. int r1, c1, r2, c2;
  76. findPosition(table, prepared[i], r1, c1);
  77. findPosition(table, prepared[i + 1], r2, c2);
  78.  
  79. if (r1 == r2) {
  80. cipher += table[r1][(c1 + 1) % 5];
  81. cipher += table[r2][(c2 + 1) % 5];
  82. }
  83. else if (c1 == c2) {
  84. cipher += table[(r1 + 1) % 5][c1];
  85. cipher += table[(r2 + 1) % 5][c2];
  86. }
  87. else {
  88. cipher += table[r1][c2];
  89. cipher += table[r2][c1];
  90. }
  91. }
  92.  
  93. return cipher;
  94. }
  95.  
  96. // Decrypt using Playfair cipher
  97. string decrypt(const string &cipher, const string &key) {
  98. char table[5][5];
  99. generateKeyTable(key, table);
  100. string plain = "";
  101.  
  102. for (size_t i = 0; i < cipher.length(); i += 2) {
  103. int r1, c1, r2, c2;
  104. findPosition(table, cipher[i], r1, c1);
  105. findPosition(table, cipher[i + 1], r2, c2);
  106.  
  107. if (r1 == r2) {
  108. plain += table[r1][(c1 + 4) % 5];
  109. plain += table[r2][(c2 + 4) % 5];
  110. }
  111. else if (c1 == c2) {
  112. plain += table[(r1 + 4) % 5][c1];
  113. plain += table[(r2 + 4) % 5][c2];
  114. }
  115. else {
  116. plain += table[r1][c2];
  117. plain += table[r2][c1];
  118. }
  119. }
  120.  
  121. return plain;
  122. }
  123.  
  124. int main() {
  125. string key = "Monarchy";
  126. string plaintext = "instruments";
  127.  
  128. cout << "Key: " << key << "\nPlaintext: " << plaintext << endl;
  129.  
  130. string ciphertext = encrypt(plaintext, key);
  131. cout << "Ciphertext: " << ciphertext << endl;
  132.  
  133. string decrypted = decrypt(ciphertext, key);
  134. cout << "Decrypted: " << decrypted << endl;
  135.  
  136. return 0;
  137. }
  138.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Key: Monarchy
Plaintext: instruments
Ciphertext: gatlmzclrqtx
Decrypted: instrumentsz