Hill Cipher Implementation
The Hill cipher is a polygraphic substitution cipher based on linear algebra. It was the first polygraphic cipher in which it was practical to operate on more than three symbols at once.
This article does not cover the algorithm behind the Hill cipher. We suggest going through the simple explanation given on Wikipedia for a detailed explanation on Encryption and Decryption.
Implementation:
Following is the implementation of the Hill cipher in C++:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
#include <iostream> #include <string> using namespace std; #define M 3 int inverse(int b) { int inv; int q, r, r1 = 26, r2 = b, t, t1 = 0, t2 = 1; while (r2 > 0) { q = r1 / r2; r = r1 - q * r2; r1 = r2; r2 = r; t = t1 - q * t2; t1 = t2; t2 = t; } if (r1 == 1) { inv = t1; if (inv < 0) { inv = inv + 26; } return inv; } return -1; } void inverseMatrix(int key[][3], int inv[][3]) { int C[3][3]; // matrix for cofactors of matrix key[][] int A[3][3]; // matrix for adjoint of matrix C[][] C[0][0] = key[1][1] * key[2][2] - key[2][1] * key[1][2]; C[0][1] = -(key[1][0] * key[2][2] - key[2][0] * key[1][2]); C[0][2] = key[1][0] * key[2][1] - key[1][1] * key[2][0]; C[1][0] = -(key[0][1] * key[2][2] - key[2][1] * key[0][2]); C[1][1] = key[0][0] * key[2][2] - key[2][0] * key[0][2]; C[1][2] = -(key[0][0] * key[2][1] - key[2][0] * key[0][1]); C[2][0] = key[0][1] * key[1][2] - key[0][2] * key[1][1]; C[2][1] = -(key[0][0] * key[1][2] - key[1][0] * key[0][2]); C[2][2] = key[0][0] * key[1][1] - key[1][0] * key[0][1]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { A[i][j] = C[j][i]; } } int det = key[0][0] * C[0][0] + key[0][1] * C[0][1] + key[0][2] * C[0][2]; if (det == 0 || det % 2 == 0 || det % 13 == 0) { printf("The text cannot be encrypted. Take valid key.\n"); exit(1); } int invs = inverse(det); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { inv[i][j] = A[i][j] * invs; } } printf("\nInverse of a key\n\n"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (inv[i][j] >= 0) { inv[i][j] = inv[i][j] % 26; printf("%3d", inv[i][j]); } else { for (int x = 0;; x++) { if (x * 26 + inv[i][j] > 0) { inv[i][j] = x * 26 + inv[i][j]; break; } } printf("%3d", inv[i][j]); } } printf("\n"); } } string encrypt(string text, int key[][3]) { string c = ""; int k = 0; int input[3]; while (k < text.length()) { input[0] = text[k++] - 65; input[1] = text[k++] - 65; input[2] = text[k++] - 65; for (int i = 0; i < M; i++) { int cipher = 0; for (int j = 0; j < M; j++) { cipher += key[i][j] * input[j]; } c += (cipher % 26) + 65; } } return c; } string decrypt(string s, int inv[][3]) { string d = ""; int k = 0; int input[3]; while (k < s.length()) { input[0] = s[k++] - 65; input[1] = s[k++] - 65; input[2] = s[k++] - 65; for (int i = 0; i < M; i++) { int decipher = 0; for (int j = 0; j < M; j++) { decipher += inv[i][j] * input[j]; } d += (decipher % 26) + 65; } } return d; } int main() { int key[3][3] = { { 6, 24, 1 }, { 13, 16, 10 }, { 20, 17, 15 } }; string text = "ACTBEFOREDAWNZZ"; string c = encrypt(text, key); cout << "The corresponding ciphertext is " << c; int inv[3][3]; inverseMatrix(key, inv); cout << "\nThe corresponding decrypted text is " << decrypt(c, inv); return 0; } |
Output:
The corresponding ciphertext is POHDXHCAFOZABNU
Inverse of a key
8 5 10
21 8 21
21 12 8
The corresponding decrypted text is ACTBEFOREDAWNZZ
That’s all about Hill Cipher Implementation.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)