-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.cpp
More file actions
288 lines (273 loc) · 7.1 KB
/
Copy pathproject.cpp
File metadata and controls
288 lines (273 loc) · 7.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <bits/stdc++.h>
#include <map>
using namespace std;
// method 1
string encrypt1(string plaintext, int shift) {
string ciphertext = "";
for (char c : plaintext) {
if (isalpha(c)) {
if (isupper(c)){
char shifted = (c - 'A' + shift) % 26 + 'A';
ciphertext += shifted;
}
else if (islower(c)){
char shifted = (c - 'a' + shift) % 26 + 'a';
ciphertext += shifted;
}
} else {
ciphertext += c;
}
}
return ciphertext;
}
string decrypt1(string ciphertext, int shift) {
string plaintext = "";
for (char c : ciphertext) {
if (isalpha(c)) {
if (isupper(c)){
char shifted = (c - 'A' - shift + 26) % 26 + 'A';
plaintext += shifted;
}
else if(islower(c)){
char shifted = (c - 'a' - shift + 26) % 26 + 'a';
plaintext += shifted;
}
} else {
plaintext += c;
}
}
return plaintext;
}
// method 2
string generateKey(string str, string key)
{
int x = str.size();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.size() == str.size())
break;
key.push_back(key[i]);
}
return key;
}
string encrypt2(string str, string key)
{
string cipher_text;
for (int i = 0; i < str.size(); i++)
{
char x = (str[i] + key[i]) %26;
x += 'A';
cipher_text.push_back(x);
}
return cipher_text;
}
string decrypt2(string cipher_text, string key)
{
string orig_text;
for (int i = 0 ; i < cipher_text.size(); i++)
{
char x = (cipher_text[i] - key[i] + 26) %26;
x += 'A';
orig_text.push_back(x);
}
return orig_text;
}
//method 3
string encrypt3(string plaintext, string key)
{
map<char, char> encryptionKey;
for(int i = 0; i < 26; i++)
{
encryptionKey[char(i + 97)] = key[i];
}
string ciphertext = "";
for(int i = 0; i < plaintext.length(); i++)
{
if(isalpha(plaintext[i]))
{
if(isupper(plaintext[i]))
{
ciphertext += toupper(encryptionKey[tolower(plaintext[i])]);
}
else
{
ciphertext += encryptionKey[plaintext[i]];
}
}
else
{
ciphertext += plaintext[i];
}
}
return ciphertext;
}
string decrypt3(string ciphertext, string key)
{
map<char, char> decryptionKey;
for(int i = 0; i < 26; i++)
{
decryptionKey[key[i]] = char(i + 97);
}
string plaintext = "";
for(int i = 0; i < ciphertext.length(); i++)
{
if(isalpha(ciphertext[i]))
{
if(isupper(ciphertext[i]))
{
plaintext += toupper(decryptionKey[tolower(ciphertext[i])]);
}
else
{
plaintext += decryptionKey[ciphertext[i]];
}
}
else
{
plaintext += ciphertext[i];
}
}
return plaintext;
}
void signup(){
cout << "SIGN-UP\n";
fstream fout;
fout.open("signup.csv", ios::out | ios::app);
cout << "Enter username: ";
string username;
cin >> username;
cout << "Enter Password: ";
string password;
fflush(stdin);
cin >> password;
int tempRand = rand();
int encr = (tempRand % (3)) + 1 ;
cout << "Temp Value"<<tempRand;
if (encr == 1){
int shift = 3;
string encrpass = encrypt1(password, shift);
fout << username << ",";
fout << encrpass << ",";
fout << encr << ",";
fout << shift << "\n";
}
else if (encr == 2){
string keyword = "AYUSH";
string newpass;
for (int i=0; i<password.size();i++){
newpass += toupper(password[i]);
}
string key = generateKey(newpass, keyword);
string encrpass = encrypt2(newpass, key);
fout << username << ",";
fout << encrpass << ",";
fout << encr << ",";
fout << key << "\n";
}
else if (encr == 3){
string key = "zyxwvutsrqponmlkjihgfedcba";
string encrpass = encrypt3(password, key);
fout << username << ",";
fout << encrpass << ",";
fout << encr << ",";
fout << key << "\n";
}
}
void login(){
cout << "LOGIN\n";
string username, password;
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
fflush(stdin);
cin >> password;
fstream fin;
fin.open("signup.csv");
int count = 0 ;
vector<vector<string>> content;
vector<string> row;
string line,word;
fstream file("signup.csv", ios::in);
if (file.is_open()){
while (getline(file, line)){
row.clear();
stringstream str(line);
while(getline(str, word, ',')){
row.push_back(word);
}
content.push_back(row);
}
}
else{
cout << "Could not open the file.\n";
}
for (int i=0; i<content.size(); i++){
if (content[i][0].compare(username) == 0){
int key;
key = stoi(content[i][2]);
string decrpass;
if (key == 1){
decrpass = decrypt1(content[i][1], 3);
if (decrpass.compare(password) == 0){
cout << "Login successfully";
}
else{
cout << "Invalid credentials";
}
count = 1;
}
else if (key == 2){
decrpass = decrypt2(content[i][1], content[i][3]);
string newpass;
for (int i=0; i<password.size();i++){
newpass += toupper(password[i]);
}
if (decrpass.compare(newpass) == 0){
cout << "Login successfully";
}
else{
cout << "Invalid credentials";
}
count = 1;
}
else if (key == 3){
decrpass = decrypt3(content[i][1], content[i][3]);
cout << endl << decrpass;
if (decrpass.compare(password) == 0){
cout << "Login successfully";
}
else{
cout << "Invalid credentials";
}
count = 1;
}
}
}
if (count == 0){
cout << "Invalid Username";
}
}
int main(){
cout << "1. Sign Up\n";
cout << "2. Login\n";
int choice;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1){
signup();
}
else if (choice == 2){
login();
}
else {
cout << "Enter valid choice";
}
return 0;
}