ElGamal Encryption Algorithm Implementation in C
The ElGamal encryption is an asymmetric key encryption algorithm for public-key cryptography, which is based on the Diffie–Hellman key exchange. It can be defined over any cyclic group G
. Its security depends upon the difficulty of a certain problem in G
related to computing discrete logarithms.
ElGamal encryption consists of three components: the key generator, the encryption algorithm, and the decryption algorithm. These operations are out of the scope of this article. We suggest going through the simple explanation given on Wikipedia for a detailed explanation.
Implementation:
Following is the implementation of the ElGamal encryption algorithm in C. The program expects an input file, plain.txt
, which contains the plain text, and generates an output file, results.txt
, which contains our decrypted text. The program also generates two intermediary files – cipher1.txt
and cipher2.txt
.
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <math.h> int e1, e2; int p, d; int C1, C2; FILE *out1, *out2; int gcd(int a, int b) { int q, r1, r2, r; if (a > b) { r1 = a; r2 = b; } else { r1 = b; r2 = a; } while (r2 > 0) { q = r1 / r2; r = r1 - q * r2; r1 = r2; r2 = r; } return r1; } int FastExponention(int bit, int n, int* y, int* a) { if (bit == 1) { *y = (*y * (*a)) % n; } *a = (*a) * (*a) % n; } int FindT(int a, int m, int n) { int r; int y = 1; while (m > 0) { r = m % 2; FastExponention(r, n, &y, &a); m = m / 2; } return y; } int PrimarityTest(int a, int i) { int n = i - 1; int k = 0; int m, T; while (n % 2 == 0) { k++; n = n / 2; } m = n; T = FindT(a, m, i); if (T == 1 || T == i - 1) { return 1; } for (int j = 0; j < k; j++) { T = FindT(T, 2, i); if (T == 1) { return 0; } if (T == i - 1) { return 1; } } return 0; } int PrimitiveRoot(int p) { int flag; for (int a = 2; a < p; a++) { flag = 1; for (int i = 1; i < p; i++) { if (FindT(a, i, p) == 1 && i < p - 1) { flag = 0; } else if (flag && FindT(a, i, p) == 1 && i == p - 1) { return a; } } } } int KeyGeneration() { do { do p = rand() + 256; while (p % 2 == 0); } while (!PrimarityTest(2, p)); p = 107; e1 = 2; do { d = rand() % (p - 2) + 1; // 1 <= d <= p-2 } while (gcd(d, p) != 1); d = 67; e2 = FindT(e1, d, p); } int Encryption(int Plaintext) { out1 = fopen("cipher1.txt", "a+"); out2 = fopen("cipher2.txt", "a+"); int r; do { r = rand() % (p - 1) + 1; // 1 < r < p } while (gcd(r, p) != 1); C1 = FindT(e1, r, p); C2 = FindT(e2, r, p) * Plaintext % p; fprintf(out1, "%d ", C1); fprintf(out2, "%d ", C2); fclose(out1); fclose(out2); } int Decryption(int C1, int C2) { FILE* out = fopen("result.txt", "a+"); int decipher = C2 * FindT(C1, p - 1 - d, p) % p; fprintf(out, "%c", decipher); fclose(out); } int main() { FILE *out, *inp; // destroy contents of these files (from previous runs, if any) out = fopen("result.txt", "w+"); fclose(out); out = fopen("cipher1.txt", "w+"); fclose(out); out = fopen("cipher2.txt", "w+"); fclose(out); KeyGeneration(); inp = fopen("plain.txt", "r+"); if (inp == NULL) { printf("Error opening Source File.\n"); exit(1); } while (1) { char ch = getc(inp); if (ch == EOF) { break; // M < p } Encryption(toascii(ch)); } fclose(inp); FILE *inp1, *inp2; inp1 = fopen("cipher1.txt", "r"); inp2 = fopen("cipher2.txt", "r"); int C1, C2; while (1) { int ret = fscanf(inp1, "%d", &C1); fscanf(inp2, "%d", &C2); if (ret == -1) { break; } Decryption(C1, C2); } fclose(inp1); fclose(inp2); return 0; } |
The above program is tested in the Windows environment using Code::Blocks
16.01.
That’s all about ElGamal Encryption algorithm implementation in C.
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 :)