-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrsa.c
More file actions
142 lines (136 loc) · 2.16 KB
/
Copy pathrsa.c
File metadata and controls
142 lines (136 loc) · 2.16 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
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
137
138
139
140
141
142
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int x, y, n, t, i, flag;
long int e[50], d[50], temp[50], j, m[50], en[50];
char msg[100];
int prime(long int);
void encryption_key();
long int cd(long int);
void encrypt();
void decrypt();
int main()
{
printf("\nEnter first prime number: \n");
scanf("%d", &x);
flag = prime(x);
if(flag == 0)
{
printf("\nINVALID INPUT\n");
exit(0);
}
printf("\nEnter second prime number\n");
scanf("%d", &y);
flag = prime(y);
if(flag == 0 || x == y)
{
printf("\nINVALID INPUT\n");
exit(0);
}
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();
printf("\nPossible values of e and d are:\n");
for(i = 0; i < j-1; i++)
printf("\n%ld\t%ld", e[i], d[i]);
encrypt();
decrypt();
return 0;
}
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 encryption_key()
{
int k;
k = 0;
for(i = 2; i < t; i++)
{
if(t % i == 0)
continue;
flag = prime(i);
if(flag == 1 && i != x && i != y)
{
e[k] = i;
flag = cd(e[k]);
if(flag > 0)
{
d[k] = flag;
k++;
}
if(k == 99)
break;
}
}
}
long int cd(long int a)
{
long int k = 1;
while(1)
{
k = k + t;
if(k % a == 0)
return(k / a);
}
}
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]);
}
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");
}