-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyCode.cpp
More file actions
210 lines (167 loc) · 4.48 KB
/
Copy pathCyCode.cpp
File metadata and controls
210 lines (167 loc) · 4.48 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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include <chrono>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#include <sys/types.h>
#endif
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::fstream;
using std::ios;
using std::getline;
using std::numeric_limits;
using std::streamsize;
void clear_screen(int sec) {
std::this_thread::sleep_for(std::chrono::seconds(sec));
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void resize_file(string filepath, long long newSize) {
#ifdef _WIN32
int fd = _open(filepath.c_str(), _O_BINARY | _O_RDWR);
if (fd != -1) {
_chsize_s(fd, newSize);
_close(fd);
}
#else
truncate(filepath.c_str(), (off_t)newSize);
#endif
}
void cypher(fstream& file, string& filepath) {
try {
string password;
char extension[32] = { 'N', 'e', 'T', 'R', 'u', 'N', 'N', 'e', 'R', 'G', 'L', 'i', 'T', 'C', 'H', 0 };
int counter = 0;
char c;
file.open(filepath, ios::in | ios::out | ios::binary);
if (!file.is_open()) return;
cout << "what is the password you want to use ?" << endl;
getline(cin, password);
if (password.empty()) return;
// Encrypt the file
while (file.get(c)) {
c ^= password[counter];
counter = (counter + 1) % password.size();
file.seekp(-1, ios::cur);
file.put(c);
file.seekg(file.tellp());
}
// Store the file extension
size_t dotPos = filepath.find_last_of('.');
if (dotPos != string::npos) {
string ext = filepath.substr(dotPos + 1);
for (int i = 0; i < ext.size() && i < 16; i++) {
extension[15 + i] = ext[i];
}
}
file.clear();
file.seekp(0, ios::end);
file.write(extension, 32);
file.close();
string newPath = (dotPos != string::npos ? filepath.substr(0, dotPos) : filepath) + ".cry";
if (rename(filepath.c_str(), newPath.c_str()) == 0) {
filepath = newPath;
}
cout << "file cyphered successfully..." << endl;
}
catch (const std::exception& e) {
cout << "an error has occured : " << e.what() << endl;
}
}
void decypher(fstream& file, string& filepath) {
try {
string password;
char footer[32];
char c;
int counter = 0;
file.open(filepath, ios::in | ios::out | ios::binary);
if (!file.is_open()) {
cout << "Error: Could not open file!" << endl;
return;
}
file.seekg(-32, ios::end);
file.read(footer, 32);
string signature = "";
for (int i = 0; i < 15; i++) signature += footer[i];
if (signature != "NeTRuNNeRGLiTCH") {
cout << "Error: This file was not encrypted by CYCODE!" << endl;
file.close();
return;
}
string originalExt = "";
for (int i = 15; i < 32; i++) {
if (footer[i] == 0) break;
originalExt += footer[i];
}
cout << "Enter the password to decrypt: ";
getline(cin, password);
if (password.empty()) return;
file.clear();
file.seekg(0, ios::end);
long long fileSize = file.tellg();
long long dataSize = fileSize - 32;
file.seekg(0, ios::beg);
while (file.tellg() < dataSize && file.get(c)) {
c ^= password[counter];
counter = (counter + 1) % password.size();
file.seekp(-1, ios::cur);
file.put(c);
file.seekg(file.tellp());
}
file.close();
resize_file(filepath, dataSize);
size_t dotPos = filepath.find_last_of('.');
string newPath = filepath.substr(0, dotPos) + "." + originalExt;
if (rename(filepath.c_str(), newPath.c_str()) == 0) {
filepath = newPath;
}
cout << "File decyphered and restored successfully!" << endl;
}
catch (const std::exception& e) {
cout << "An error has occured: " << e.what() << endl;
}
}
int main() {
fstream file;
string filepath;
string password;
bool encryption;
bool again = true;
cout << "this is CYCODE, a program to Cypher and decypher anything..." << endl;
while(true){
if (!again) break;
cout << "please enter the file path that you want to work on : ";
cin.clear();
getline(cin, filepath);
cout << "please choose wheather to cypher or decypher the file ( 1 , 0 ) : ";
cin >> encryption;
clear_screen(3);
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (encryption) {
cout << "you have chosen to cypher the file : " << filepath << endl;
cypher(file, filepath);
}
else {
cout << "you have chosen to decypher the file : " << filepath << endl;
decypher(file, filepath);
}
cout << "do you want to work on another file ? ( 1 for yes , 0 for no ) : ";
cin >> again;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
clear_screen(1);
}
return 0;
}