# C Program to Implement the RSA Algorithm

«
»
This is a C Program to implement RSA. This is a type of Public Key encryption or Asymmetric encryption/decryption algorithm, that uses two different but related keys. Plus knowing the one key will not help figure out the other is the key protection.

The RSA algorithm uses the fact that it’s easy to multiply two large prime numbers together and get a product. But you can’t take that product and reasonably guess the two original numbers, or guess one of the original primes if only the other is known.

Here is source code of the C Program to Implement the RSA Algorithm. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `#include<stdio.h>`
2. `#include<conio.h>`
3. `#include<stdlib.h>`
4. `#include<math.h>`
5. `#include<string.h>`
6. ` `
7. `long int p, q, n, t, flag, e, d, temp, j, m, en, i;`
8. `char msg;`
9. `int prime(long int);`
10. `void ce();`
11. `long int cd(long int);`
12. `void encrypt();`
13. `void decrypt();`
14. `void main() {`
15. `    printf("\nENTER FIRST PRIME NUMBER\n");`
16. `    scanf("%d", &p);`
17. `    flag = prime(p);`
18. `    if (flag == 0) {`
19. `        printf("\nWRONG INPUT\n");`
20. `        getch();`
21. `        exit(1);`
22. `    }`
23. `    printf("\nENTER ANOTHER PRIME NUMBER\n");`
24. `    scanf("%d", &q);`
25. `    flag = prime(q);`
26. `    if (flag == 0 || p == q) {`
27. `        printf("\nWRONG INPUT\n");`
28. `        getch();`
29. `        exit(1);`
30. `    }`
31. `    printf("\nENTER MESSAGE\n");`
32. `    fflush(stdin);`
33. `    scanf("%s", msg);`
34. `    for (i = 0; msg[i] != NULL; i++)`
35. `        m[i] = msg[i];`
36. `    n = p * q;`
37. `    t = (p - 1) * (q - 1);`
38. `    ce();`
39. `    printf("\nPOSSIBLE VALUES OF e AND d ARE\n");`
40. `    for (i = 0; i < j - 1; i++)`
41. `        printf("\n%ld\t%ld", e[i], d[i]);`
42. `    encrypt();`
43. `    decrypt();`
44. `}`
45. `int prime(long int pr) {`
46. `    int i;`
47. `    j = sqrt(pr);`
48. `    for (i = 2; i <= j; i++) {`
49. `        if (pr % i == 0)`
50. `            return 0;`
51. `    }`
52. `    return 1;`
53. `}`
54. `void ce() {`
55. `    int k;`
56. `    k = 0;`
57. `    for (i = 2; i < t; i++) {`
58. `        if (t % i == 0)`
59. `            continue;`
60. `        flag = prime(i);`
61. `        if (flag == 1 && i != p && i != q) {`
62. `            e[k] = i;`
63. `            flag = cd(e[k]);`
64. `            if (flag > 0) {`
65. `                d[k] = flag;`
66. `                k++;`
67. `            }`
68. `            if (k == 99)`
69. `                break;`
70. `        }`
71. `    }`
72. `}`
73. `long int cd(long int x) {`
74. `    long int k = 1;`
75. `    while (1) {`
76. `        k = k + t;`
77. `        if (k % x == 0)`
78. `            return (k / x);`
79. `    }`
80. `}`
81. `void encrypt() {`
82. `    long int pt, ct, key = e, k, len;`
83. `    i = 0;`
84. `    len = strlen(msg);`
85. `    while (i != len) {`
86. `        pt = m[i];`
87. `        pt = pt - 96;`
88. `        k = 1;`
89. `        for (j = 0; j < key; j++) {`
90. `            k = k * pt;`
91. `            k = k % n;`
92. `        }`
93. `        temp[i] = k;`
94. `        ct = k + 96;`
95. `        en[i] = ct;`
96. `        i++;`
97. `    }`
98. `    en[i] = -1;`
99. `    printf("\nTHE ENCRYPTED MESSAGE IS\n");`
100. `    for (i = 0; en[i] != -1; i++)`
101. `        printf("%c", en[i]);`
102. `}`
103. `void decrypt() {`
104. `    long int pt, ct, key = d, k;`
105. `    i = 0;`
106. `    while (en[i] != -1) {`
107. `        ct = temp[i];`
108. `        k = 1;`
109. `        for (j = 0; j < key; j++) {`
110. `            k = k * ct;`
111. `            k = k % n;`
112. `        }`
113. `        pt = k + 96;`
114. `        m[i] = pt;`
115. `        i++;`
116. `    }`
117. `    m[i] = -1;`
118. `    printf("\nTHE DECRYPTED MESSAGE IS\n");`
119. `    for (i = 0; m[i] != -1; i++)`
120. `        printf("%c", m[i]);`
121. `}`

Output:

```\$ gcc RSA.c
\$ ./a.out

ENTER FIRST PRIME NUMBER: 7

ENTER ANOTHER PRIME NUMBER: 19

ENTER MESSAGE: Dharmendra

POSSIBLE VALUES OF e AND d ARE

5	65
11	59
13	25
17	89
23	47
29	41
31	7
37	73
41	29
THE ENCRYPTED MESSAGE IS
=’a…º¢É½…a
THE DECRYPTED MESSAGE IS
Dharmendra```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now! 