-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path17_RSA.cpp
82 lines (70 loc) · 1.8 KB
/
17_RSA.cpp
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
/*
* Cpp program to implement RSA Algorithm
*/
#include <iostream>
#include <math.h>
// #include "./returnName.h"
using namespace std;
// to find gcd
int gcd(int a, int h)
{
int temp;
while (1)
{
temp = a % h;
if (temp == 0)
return h;
a = h;
h = temp;
}
}
int main()
{
// 2 random prime numbers
double p, q;
char choice;
do
{
cout << "Enter two prime numbers: ";
cin >> p >> q;
double n = p * q;
double count;
double totient = (p - 1) * (q - 1);
// public key - e stands for encrypt
double e = 2;
// for checking co-prime which satisfies e>1
while (e < totient)
{
count = gcd(e, totient);
if (count == 1)
break;
else
e++;
}
// private key - d stands for decrypt
double d;
// k can be any arbitrary value
double k = 2;
// choosing d such that it satisfies d*e = 1 + k * totient
d = (1 + (k * totient)) / e;
double msg;
cout << "Enter Message to be encrypted(integer): ";
cin >> msg;
double c = pow(msg, e);
double m = pow(c, d);
c = fmod(c, n);
m = fmod(m, n);
// cout << "Message data = " << msg
cout << "p = " << p << endl;
cout << "q = " << q << endl;
cout << "n = pq = " << n << endl;
cout << "totient = " << totient << endl;
cout << "e = " << e << endl;
cout << "d = " << d << endl;
cout << "Encrypted data = " << c << endl;
cout << "Original Message sent = " << m << endl;
cout << "Do you want to continue? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
return 0;
}