-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.hpp
More file actions
117 lines (116 loc) · 2.58 KB
/
logic.hpp
File metadata and controls
117 lines (116 loc) · 2.58 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
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
std::string dec(std::string data)
{
int key = data[0] - 'A' + 1;
int n = data.size();
for (int i = 1; i < n; i++)
{
data[i] = data[i] - key;
}
return data;
}
std::string enc(std::string data)
{
int key = data[0] - 'A' + 1;
int n = data.size();
for (int i = 1; i < n; i++)
{
data[i] = data[i] + key;
}
return data;
}
void writer(std::string file, std::string s)
{
std::ofstream outputFile(file, std::ios::app | std::ios::out);
if (outputFile.is_open())
{
outputFile << s << "\n";
outputFile.close();
}
else
{
std::cout << "\n\033[31m\tSome Error occured while opening file! \n\tMake sure the filename is correct and have .txt extension.\033[0m\n";
}
outputFile.close();
std::cout << "\033[0m";
}
short reader(std::string file, short op)
{
std::fstream new_file;
new_file.open(file, std::ios::in);
if (new_file.is_open())
{
if (op == 1)
{
std::string s;
while (getline(new_file, s))
{
s = enc(s);
writer("encrypt.txt", s);
}
}
else if (op == 2)
{
std::string s;
while (getline(new_file, s))
{
s = dec(s);
writer("decrypt.txt", s);
}
}
}
else
{
std::cout << "\n\t\033[31mSome Error occured while opening file! \n\tMake sure the filename is correct and have .txt extension.\033[0m\n";
return 0;
}
new_file.close();
std::cout << "\033[0m";
return 1;
}
void runner()
{
short choice = 1;
std::cout << "\033[1m";
std::cout << "\n\t\033[4mWelcome to File Encryption and Decryption.\n\tYou can encrypt and decrypt text files easily.\n\tMake sure to use correct names for files.\033[0m\n";
do
{
std::cout << "\n\tChoose your operation:\n";
std::cout << "\t1) Encrypt a text file\n";
std::cout << "\t2) Decrypt a text file\n";
std::cout << "\t3) Close\n\n\t>\033[34m";
std::cin >> choice;
std::string file;
if (choice == 1)
{
std::cout << "\n\t\033[0mEnter file-name : \n\t>\033[34m";
std::cin >> file;
if (reader(file + ".txt", choice))
{
std::cout << "\n\t\033[3;104;30mFile Encrypted Successfully with name 'encrypt.txt'\033[0m\n";
}
}
else if (choice == 2)
{
std::cout << "\n\t\033[0mEnter file-name : \n\t>\033[34m";
std::cin >> file;
if (reader(file + ".txt", choice))
{
std::cout << "\n\t\033[3;104;30mFile Decrypted Successfully with name 'decrypt.txt'\033[0m\n";
}
}
else if (choice == 3)
{
std::cout << "\n\033[35mKeep your files safe from others :)\n\033[0m";
break;
}
else
{
std::cout << "\n\t\033[33mChoose a valid operation!\033[0m\n";
}
} while (choice != INT_MIN);
getch();
}