forked from BalajiG2000/Code_Dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRivestShamirAdleman.cpp
128 lines (95 loc) · 1.93 KB
/
RivestShamirAdleman.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
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
/*
Maitreya Kanitkar
BE-IT 8084
ICS Assignment 2 : RSA Algorithm
*/
#include<iostream>
using namespace std;
class rsa
{
int p, q;
double n, phi, e=2, g, d;
long msg, encrypted, decrypted, enc;
public:
void pipeline()
{
input();
algorithm();
output();
}
void input()
{
cout<<"Enter the value of p : ";
cin>>p;
cout<<"Enter the value of q : ";
cin>>q;
cout<<"Enter the message : ";
cin>>msg;
}
int gcd(int a, int b)
{
if(a==0)
return b;
return gcd(b%a, a);
}
int mod(int m, int k, int n)
{
int result=1;
m=m%n;
if(m==0)
return 0;
while(k>0)
{
if(k%2==1)
result=(result*m)%n;
k/=2;
m=(m*m)%n;
}
return result;
}
void algorithm()
{
n=p*q;
phi=(p-1)*(q-1);
while(e<phi)
{
if((gcd(e, phi)==1))
break;
else
e++;
}
for(int i=1;i<phi;i++)
if(((int)e*i)%(int)phi==1)
d=i;
encrypted=mod(msg, e, n);
decrypted=mod(encrypted, d, n);
}
void output()
{
cout<<endl<<"Original message : "<<msg;
cout<<endl<<"Encrypted message : "<<encrypted;
cout<<endl<<"Decrypted message : "<<decrypted;
}
};
int main()
{
rsa obj;
obj.pipeline();
return 0;
}
/*
OUTPUT 1:-
Enter the value of p : 3
Enter the value of q : 7
Enter the message : 12
Original message : 12
Encrypted message : 3
Decrypted message : 12
OUTPUT 2:-
Enter the value of p : 91
Enter the value of q : 97
Enter the message : 1000
Original message : 1000
Encrypted message : 5095
Decrypted message : 1000
*/