-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrsa.c
More file actions
executable file
·127 lines (96 loc) · 2.3 KB
/
rsa.c
File metadata and controls
executable file
·127 lines (96 loc) · 2.3 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
/*
File : rsa.c
Desc : Implementation of RSA algorithm in C
Author : Diparth Shah <diparths@gmail.com>
*/
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
/* include our own header code "thug.h" which completely handles prime input operations */
#include"thug.h"
/* compute() function is used to generate private and public key */
void encrypt();
void decrypt();
void compute();
void display();
/* here p and q are two large prime numbers ,
d is decrypter private key and e is encrypter public key , n and fn is used for
computation , pt is entered plain text and ct is cipher text
*/
int p,q,n,fn,d,e,temp,dt;
int pt=1,ct=1;
bool x;
int main()
{
printf("\n ENTER FIRST PRIME NUMBER : ");
scanf("%d",&p);
/* using isPrime() function to check prime input , which is defined in our own header file "thug.h" */
x=isPrime(p);
if (x==false)
{
printf("\n Wrong input \n");
exit(0);
}
printf("\n ENTER SECOND PRIME NUMBER : ");
scanf("%d",&q);
x=isPrime(q);
if(x==false)
{
printf("\n Wrong input \n ");
exit(0);
}
compute();
printf("\n\n ENTER YOUR TEXT : ");
scanf("%d",&pt);
encrypt();
decrypt();
display();
return 0;
}
void compute()
{
n=p*q;
fn=(p-1)*(q-1);
/* generate encrypter(e) public key */
TryAgain:
printf("\n ENTER PUBLIC KEY WHICH MUST BE PRIME NUMBER AND LESS THAN %d : ",fn);
scanf("%d",&e);
x=isPrime(e);
if(x==false || e>=fn)
{
printf("\n INVALID INPUT \n");
goto TryAgain;
}
/* generate decrypter(d) private key */
d = 1;
do
{
temp = (d*e)%fn;
d++;
} while(temp!=1);
d = d-1;
printf("\n RIVEST-SHAMIR-ADLEMAN \n");
printf("\n PUBLIC KEY : %d , %d ",e,n);
printf("\n Generating PRIVATE KEY...............");
printf("\n PRIVATE KEY : %d , %d ",d,n);
}
void encrypt()
{
int k;
k=pow(pt,e);
ct=(k%n);
}
void decrypt()
{
long int l;
l=pow(ct,d);
dt=(l%n);
}
void display()
{
printf("\n -------------------------------------------------");
printf("\n PLAIN TEXT : %d ",pt);
printf("\n ENCRYPTED TEXT : %d ",ct);
printf("\n DECRYPTED TEXT : %d ",dt);
printf("\n -------------------------------------------------\n");
}