-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.cpp
79 lines (79 loc) · 1.53 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
#include<iostream>
#include<math.h>
#include <fstream>
using namespace std;
int gcd(int a, int h)
{
int temp;
while(1)
{
temp = a%h;
if(temp==0)
return h;
a = h;
h = temp;
}
}
int main()
{
double p,q;
cout<<"Enter two Prime Numbers";
cin>>p>>q;
double n=p*q;
double count;
double totient = (p-1)*(q-1);
double e=36;
while(e<totient){
count = gcd(e,totient);
if(count==1)
break;
else
e++;
}
double d;
double k = 36;
d = (1 + (k*totient))/e;
int ans;
double en;
string in;
cout<<"Enter File Name -"<<endl;
cin>>in;
char c,fc;
ifstream fin;
ofstream fout;
fin.open(in.c_str(), ios::binary);
in = "encryptedfile";
fout.open(in.c_str(), ios::binary);
while (!fin.eof())
{
fin>>noskipws>>c;
double temp = pow(c,e);
en = temp;
temp=fmod(temp,n);
fout<<(char)temp;
}
fin.close();
fout.close();
cout<<"THE FOLLOWING FILE IS ENCRYPTED"<<endl;
cout<<"Enter File Name to decrpt -"<<endl;
cin>>in;
string ft;
cout<<"Enter File Format Type -"<<endl;
cin>>ft;
ifstream finn;
ofstream foutt;
finn.open(in.c_str(), ios::binary);
in = "decryptedfile" + ft;
foutt.open(in.c_str(), ios::binary);
while (!finn.eof())
{
finn>>noskipws>>c;
double temp2= pow(c,d);
temp2=fmod(temp2,n);
foutt<<(char)temp2;
}
cout<<"THE FOLLOWING FILE IS DECRYPTED"<<endl;
finn.close();
foutt.close();
return 0;
}