-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsa.cpp
136 lines (125 loc) · 2.15 KB
/
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
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
129
130
131
132
133
134
135
136
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define li long int
int x, y, n, t, i, flag_int;
long int e[50], d[50], temp[50], j, m[50], en[50];
char msg[100];
int prime(long int pr)
{
int i;
j = sqrt(pr);
for(i = 2; i <= j; i++)
{
if(pr % i == 0)
return 0;
}
return 1;
}
void encrypt()
{
long int pt, ct, key = e[0], k, len;
i = 0;
len = strlen(msg);
while(i != len)
{
pt = m[i];
pt = pt - 96;
k = 1;
for(j = 0; j < key; j++)
{
k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
printf("\n\nEncrypted message: \n");
for(i = 0; en[i] != -1; i++)
printf("%c", en[i]);
}
long int test(long int a)
{
long int k = 1;
while(1)
{
k = k + t;
if(k % a == 0)
return(k / a);
}
}
void encryption_key()
{
int k;
k = 0;
for(i = 2; i < t; i++)
{
if(t % i == 0)
continue;
flag_int = prime(i);
if(flag_int == 1 && i != x && i != y)
{
e[k] = i;
flag_int = test(e[k]);
if(flag_int > 0)
{
d[k] = flag_int;
k++;
}
if(k == 99)
break;
}
}
}
void decrypt()
{
long int pt, ct, key = d[0], k;
i = 0;
while(en[i] != -1)
{
ct = temp[i];
k = 1;
for(j = 0; j < key; j++)
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\n\nDecrypted message:\n");
for(i = 0; m[i] != -1; i++)
printf("%c", m[i]);
printf("\n");
}
int main()
{
printf("\nEnter first prime number: \n");
scanf("%d", &x);
flag_int = prime(x);
if(flag_int == 0)
{
printf("\nINVALID INPUT\n");
exit(0);
}
printf("\nEnter second prime number\n");
scanf("%d", &y);
flag_int = prime(y);
printf("\nEnter your message: \n");
scanf("%s",msg);
for(i = 0; msg[i] != NULL; i++)
m[i] = msg[i];
n = x * y;
t = (x-1) * (y-1);
encryption_key();
cout << "\nValue of e and d are:\n";
cout << e[0] << " " << d[i] << endl;
encrypt();
decrypt();
return 0;
}