RSA Algorithm Implementation in C
RSA is an asymmetric cryptographic algorithm used by modern computers to encrypt and decrypt messages. Asymmetric means that there are two different keys. This is also called public-key cryptography because one of the keys can be given to anyone. The other key must be kept private.
This article does not cover the operation of the RSA algorithm. We suggest going through the simple explanation given on Wikipedia for a detailed step-by-step explanation.
Implementation:
Following is the implementation of the RSA cryptographic algorithm in C. The program expects an input file, input.txt, which should contain the plain text, and generates an output file, decipher.txt, which contains our decrypted text. It also generates an intermediary file, cipher.txt, which contains the encrypted text in bits.
|
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 213 214 215 216 217 218 219 220 221 222 223 224 |
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <math.h> int e, d, n; 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 PrimarityTest(int a, int i) { int n = i - 1; int k = 0; int j, 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 (j = 0; j < k; j++) { T = FindT(T, 2, i); if (T == 1) { return 0; } if (T == i - 1) { return 1; } } return 0; } 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 FastExponention(int bit, int n, int* y, int* a) { if (bit == 1) { *y = (*y * (*a)) % n; } *a = (*a) * (*a) % n; } int inverse(int a, int b) { int inv; int q, r, r1 = a, 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 + a; } return inv; } int KeyGeneration() { int p, q; int phi_n; do { do p = rand(); while (p % 2 == 0); } while (!PrimarityTest(2, p)); do { do q = rand(); while (q % 2 == 0); } while (!PrimarityTest(2, q)); n = p * q; phi_n = (p - 1) * (q - 1); do e = rand() % (phi_n - 2) + 2; // 1 < e < phi_n while (gcd(e, phi_n) != 1); d = inverse(phi_n, e); } int Encryption(int value, FILE* out) { int cipher; cipher = FindT(value, e, n); fprintf(out, "%d ", cipher); } int Decryption(int value, FILE* out) { int decipher; decipher = FindT(value, d, n); fprintf(out, "%c", decipher); } int main(void) { FILE *inp, *out; // destroy contents of these files (from previous runs, if any) out = fopen("cipher.txt", "w+"); fclose(out); out = fopen("decipher.txt", "w+"); fclose(out); KeyGeneration(); inp = fopen("plain.txt", "r+"); if (inp == NULL) { printf("Error opening Source File.\n"); exit(1); } out = fopen("cipher.txt", "w+"); if (out == NULL) { printf("Error opening Destination File.\n"); exit(1); } // encryption starts while (1) { char ch = getc(inp); if (ch == -1) { break; } int value = toascii(ch); Encryption(value, out); } fclose(inp); fclose(out); // decryption starts inp = fopen("cipher.txt", "r"); if (inp == NULL) { printf("Error opening Cipher Text.\n"); exit(1); } out = fopen("decipher.txt", "w+"); if (out == NULL) { printf("Error opening File.\n"); exit(1); } while (1) { int cip; if (fscanf(inp, "%d", &cip) == -1) { break; } Decryption(cip, out); } fclose(out); return 0; } |
The above program is tested in the Windows environment using Code::Blocks 16.01.
That’s all about RSA 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 :)