-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
149 lines (134 loc) · 4.1 KB
/
Copy pathmain.cpp
File metadata and controls
149 lines (134 loc) · 4.1 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
143
144
145
146
147
148
149
#include "RSA.h"
#include "RSA.cpp"
int main(){
cpp_int p,q,phi,n;
int e;
string msg;
cout << "RSA\n";
vector<cpp_int> dec;
cpp_int dec_push;
bool cp,cq,ce;
int choice;
char ch='n';
_re:
cout << "Generate Public and Private key\n";
cout << "Enter prime p: ";
cin >> p;
cp = primality_check(p);
cout << "Enter prime q: ";
cin >> q;
cq = primality_check(q);
try{
if(!cp)
throw(1);
if(!cq)
throw(1.1);
}
catch(int){
cout << "\nincorrect prime number: "<< p << "\n Redirecting...\n";
goto _re;
}
catch(double){
cout << "\nincorrect prime number: " << q << "\n Redirecting...\n";
goto _re;
}
phi = (p-1)*(q-1);
_re_:
cout << "Enter e such that e is co-prime of " << phi << " :";
cin >> e;
if(!e_check(e,phi)){
cout << "wrong exponent value, redirecting..\n";
goto _re_;
}
n = p*q;
system("cls");
msg_padding m(p,q,e);
cout << "Your public key n: " << n << " e: " << e << endl;
cout << "Your private key p: " << p << " q: " << q << endl;
re:
cout << "\n[-] MENU: \n";
cout << " 1. Encrypt Data\n"
<< " 2. Decrypt Data\n"
<< " 3. Encrypt file\n"
<< " 4. Decrypt file\n"
<< " 5. Exit\n";
cout << "Enter you choice: ";
cin >> choice;
if(choice==1){
cout << "Enter your message: ";
getline(cin>>ws,msg);
m.convert_to_int(msg);
m.encrypt();
m.encrypted_text();
goto re;
}
if(choice==2) {
cout << "Enter your private key \n";
cout << "Enter prime p: ";
cin >> p;
cout << "Enter prime q: ";
cin >> q;
_e:
cout << "Enter e: ";
cin >> e;
if(!e_check(e,phi)){
cout << "wrong exponent or prime value, redirecting\n";
goto _e;
}
cout << "Enter encrypted data(enter 0 to stop): ";
msg_padding m_dec(p,q,e);
do{
cin >> dec_push;
if(dec_push!=0)
dec.push_back(dec_push);
}while(!(dec_push == 0));
m_dec.decrypt(dec);
cout << "\nDecrypted text: ";
m_dec.convert_to_string();
goto re;
}
if(choice==3){
string line,file_name;
Encrypt_Decrypt_file edf(p,q,e);
cout << "Enter file name: ";
cin >> file_name;
edf.ifile.open(file_name+".txt");
if(!edf.ifile.is_open())
edf.ifile.open(file_name+".txt");
int m, n, size;
m = edf.ifile.tellg(); // tells where the pointer is now
edf.ifile.seekg(0, ios::end); // moves pointer to end of file
n = edf.ifile.tellg();
size = n - m;
if (size == 0)
cout << "The file is Empty or doesnt exist\n Exiting..",exit(0);
edf.ifile.seekg(0); // moving pointer back to start
getline(edf.ifile,line);
cout << line;
edf.convert_to_int(line);
cout << endl;
edf.encrypt();
edf.encryptfile(file_name);
goto re;
}
if(choice ==4){
string line,file_name;
cout << "Enter your private key \n";
cout << "Enter prime p: ";
cin >> p;
cout << "Enter prime q: ";
cin >> q;
cout << "Enter e: ";
cin >> e;
if(!e_check(e,phi))
cout << "wrong exponent or prime value, Exiting..",system("pause"), exit(0);
cout << "Enter file name: ";
cin >> file_name;
Encrypt_Decrypt_file df(p,q,e);
df.decryptfile(file_name);
goto re;
}
if(choice == 5)
exit(1);
system("pause");
}