C++ Program to Implement the RSA Algorithm

This C++ program encodes any message using RSA Algorithm. Input is case sensitive and works only for all characters. RSA is one of the first practicable public-key cryptosystems and is widely used for secure data transmission. In such a cryptosystem, the encryption key is public and differs from the decryption key which is kept secret. In RSA, this asymmetry is based on the practical difficulty of factoring the product of two large prime numbers, the factoring problem. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman.

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

Output:

$ g++ RSA.cpp
$ a.out
 
 
ENTER FIRST PRIME NUMBER
47
 
ENTER ANOTHER PRIME NUMBER
53
 
ENTER MESSAGE
Dharmendra
 
POSSIBLE VALUES OF e AND d ARE
3	1595
5	957
7	1367
11	435
17	985
19	1259
29	165
31	463
37	1293
41	2217
43	1947
59	1419
61	549
67	2035
71	1415
73	1409
79	1847
83	2075
89	2177
97	1233
101	1421
103	2183
 
THE ENCRYPTED MESSAGE IS
x`a???]??a
THE DECRYPTED MESSAGE IS
Dharmendra
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.