-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.cpp
240 lines (232 loc) · 5.07 KB
/
driver.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <gmp.h>
#include <bits/stdc++.h>
#include <sstream>
#include <fstream>
#include <iostream>
#include "encryption.cpp"
#include "decryption.cpp"
string form_message_string(string m,int k_size);
string clean_string(string m);
int main(){
string line;int companies = 0;
string s2 = "Company";
ifstream infile("directory.txt");
while(getline(infile,line))
{
if(line.find(s2)!=string::npos){
companies+=1;
}
}
infile.close();
mpz_t dict[companies][2],p_key[companies];
FILE *data;
char p[500];char q[500];*p = '\0';*q = '\0';
data = fopen("directory.txt","r");
int i=0,j=0,index = 2;
while(fscanf(data,"%[^\n]\n",p)!=EOF)
{
// printf("%s\n", p);
if(p[0]=='e')
{
mpz_inits(dict[i][0], NULL);
while(index<500)
{
q[index-2] = p[index];
index = index+1;
}
q[498] = '\0';q[499] = '\0';
int h = mpz_set_str(dict[i][0],q,10);
// gmp_printf("%Zd\n",dict[i][0]);
i = i+1;
index = 2;
}
if(p[0]=='n')
{
mpz_inits(dict[j][1], NULL);
while(index<500)
{
q[index-2] = p[index];
index = index+1;
}
q[498] = '\0';q[499] = '\0';
int h = mpz_set_str(dict[j][1],q,10);
// gmp_printf("%Zd\n",dict[j][1]);
j = j+1;
index = 2;
}
}
fclose(data);
FILE *secret;
secret = fopen("private.txt","r");
i=0,index = 2;
while(fscanf(secret,"%[^\n]\n",p)!=EOF)
{
// printf("%s\n", p);
if(p[0]=='d')
{
mpz_inits(p_key[i], NULL);
while(index<500)
{
q[index-2] = p[index];
index = index+1;
}
q[498] = '\0';q[499] = '\0';
int h = mpz_set_str(p_key[i],q,10);
// gmp_printf("%Zd\n",p_key[i]);
i = i+1;
index = 2;
}
}
fclose(secret);
int sm,rm;
while(true){
printf("Select sender company:");
cin>>sm;
if(sm>companies || sm<=0)
{
printf("Enter the valid company name\n");
}
else
break;
}
while(true){
printf("Select receiver company:");
cin>>rm;
if(sm == rm)
{
printf("Sender and Receiver cannot be the same. Try again.\n");
}
else if(rm>companies || rm<=0)
{
printf("Enter the valid company name\n");
}
else
break;
}
sm = sm-1;
rm = rm-1;
mpz_t es,ds,ns;
mpz_t er,dr,nr;
mpz_inits(es,ds,ns,er,dr,nr,NULL);
mpz_set(es,dict[sm][0]);mpz_set(ns,dict[sm][1]);mpz_set(ds,p_key[sm]);
mpz_set(er,dict[rm][0]);mpz_set(nr,dict[rm][1]);mpz_set(dr,p_key[rm]);
int kr = determine_block_size(26,nr);
int ks = determine_block_size(26,ns);
// cout<<ks<<" "<<kr<<"\n";
string m = "",tmp = "",c = "";int count = 0;
printf("Enter the message to be encrypted\n");
cin.ignore();
getline(cin,m);
// m = "she doesnt have anything to prove but she is going ANYWAY THATS her Character sHe kNoWs she doesnt have to but still i know that she is a very crafty person and i do not like to be in her influence";
transform(m.begin(), m.end(), m.begin(), ::tolower);
m = clean_string(m);
int clean_len = m.length();
string vig_key="";
while(true)
{
printf("Enter the key for vigenere cipher(of length 10): ");
cin>>vig_key;
if(vig_key.length() != 10)
{
printf("\nInvalid key\n");
}
else
break;
}
// vig_key = "Hellofella";
transform(vig_key.begin(), vig_key.end(), vig_key.begin(), ::tolower);
m = vigenere_encrypt(m,vig_key);
m = vig_key+m;
m = form_message_string(m,ks);
int rep = m.length()/ks;
// cout<<rep<<"\n";
string c1,c2,m1,m2;
if(ks==kr)
{
char p[rep];
m2 = form_encrypted_string(m,ks,ds,ns);
for(int i=0;i<rep;i++)
{
p[i] = m2.at(m2.length()-(i+1));
}
m2 = m2.substr(0,m2.length()-rep);
c1 = form_encrypted_string(m2,kr,er,nr);
m1 = form_decrypted_string(c1,kr,dr,nr);
for(int i=0;i<rep;i++)
{
m1 = m1+p[rep-1-i];
}
c2 = form_decrypted_string(m1,kr,es,ns);
}
else if (ks<kr)
{
m2 = form_encrypted_string(m,ks,ds,ns);
c1 = form_encrypted_string(m2,kr,er,nr);
m1 = form_decrypted_string(c1,kr,dr,nr);
c2 = form_decrypted_string(m1,ks,es,ns);
}
else
{
char p1[2*rep];
m2 = form_encrypted_string(m,ks,ds,ns);
for(int i=0;i<2*rep;i++)
{
p[i] = m2.at(m2.length()-(i+1));
}
m2 = m2.substr(0,m2.length()-(2*rep));
c1 = form_encrypted_string(m2,kr,er,nr);
m1 = form_decrypted_string(c1,kr,dr,nr);
for(int i=0;i<2*rep;i++)
{
m1 = m1+p[2*rep-1-i];
}
c2 = form_decrypted_string(m1,kr+1,es,ns);
}
cout<<"Encrypted string: \n";
// cout<<m2<<" "<<m2.length()<<"\n";
// cout<<c1<<" "<<c1.length()<<"\n";
cout<<c1<<"\n\n";
// cout<<m1<<" "<<m1.length()<<"\n";
// cout<<c2<<" "<<c2.length()<<"\n";
vig_key = c2.substr(0,10);
c2 = c2.substr(10,c2.length()-10);
m = vigenere_decrypt(c2,vig_key);
cout<<"Decrypted string: ";
cout<<m.substr(0,clean_len)<<"\n";
mpz_clears(es,ds,ns,er,dr,nr,NULL);
}
string form_message_string(string m,int k_size)
{
int count = 0;
string temp = "",c = "";
for(int i=0;i<m.length();i++)
{
count = count+1;
temp += m.at(i);
if(count>=k_size)
{
c +=temp;
count = 0;
temp="";
}
}
if(temp != "")
{
for(int i = count+1;i<(k_size+1);i++)
{
temp+='a';
}
}
c += temp;
return c;
}
string clean_string(string m){
string temp = "";
for(int i=0;i<m.length();i++)
{
if(!(m.at(i)>='a' && m.at(i)<='z'))
continue;
temp += m.at(i);
}
return temp;
}