-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrsa.cpp
More file actions
87 lines (82 loc) · 1.49 KB
/
rsa.cpp
File metadata and controls
87 lines (82 loc) · 1.49 KB
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
#include<bits/stdc++.h>
using namespace std;
long long cuspow(long long a, long long b, long long n)
{
long long i, temp;
temp = a;
for(i = 2; i <= b; i++)
{
a = (a * temp) % n;
}
return a;
}
bool isPrime(long long n)
{
long long i;
for(i=2; i * i <= n; i++)
{
if(n % i==0)
{
return 0;
}
}
return 1;
}
pair<long long , char> rsa(long long plaintext , long long e , long long d, long long n)
{
long long ciphertext;
char dplaintext;
ciphertext = cuspow(plaintext, e, n);
dplaintext = cuspow(ciphertext, d, n);
pair<long long , char> rpairs = make_pair(ciphertext , dplaintext);
return rpairs;
}
int main()
{
long long p , q , i, n, e, d, phi_of_n;
string plaintext;
vector<char> ciphertext;
do
{
cout << "\nEnter first prime number: ";
cin >> p;
}while(!isPrime(p));
do
{
cout << "\nEnter second prime number: ";
cin >> q;
}while(!isPrime(q));
n = p * q;
phi_of_n = (p - 1) * (q - 1);
for(i = 2; i < phi_of_n / 2; i++)
{
if(__gcd(i, phi_of_n) == 1)
{
e = i;
break;
}
}
for(i = 1; i <= phi_of_n; i++)
{
if(((i * e) % phi_of_n) == 1)
{
d = i;
break;
}
}
cout<<"\nEnter Plaintext ";
cin>>plaintext;
cout << "\nEncrypted Text ";
for(i = 0; i < plaintext.size(); i++)
{
pair <long long , char> rpairs = rsa(plaintext[i], e, d, n);
cout << rpairs.first;
ciphertext.push_back(rpairs.second);
}
cout << "\nDecrypted Text: ";
for(i = 0; i < ciphertext.size(); i++)
{
cout << (char)ciphertext[i];
}
cout << endl;
}