-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP5-File-Encryption.cpp
More file actions
60 lines (47 loc) · 1.49 KB
/
P5-File-Encryption.cpp
File metadata and controls
60 lines (47 loc) · 1.49 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void processFile(const string &inputName, const string &outputName, int key) {
ifstream inputFile(inputName, ios::binary);
ofstream outputFile(outputName, ios::binary);
if (!inputFile || !outputFile) {
cerr << "Error: File access failure." << endl;
return;
}
const size_t BUFFER_SIZE = 64 * 1024;
vector<char> buffer(BUFFER_SIZE);
while (inputFile.read(buffer.data(), BUFFER_SIZE) || inputFile.gcount() > 0) {
streamsize bytesRead = inputFile.gcount();
for (streamsize i = 0; i < bytesRead; ++i) {
buffer[i] ^= key;
}
outputFile.write(buffer.data(), bytesRead);
}
inputFile.close();
outputFile.close();
cout << "Transformation complete: " << outputName << endl;
}
int main() {
int choice;
string filename;
int key;
while (true) {
cout << "\n1. Encrypt File\n2. Decrypt File\n3. Exit\nEnter choice: ";
if (!(cin >> choice)) break;
if (choice == 3) return 0;
cout << "Enter file name: ";
cin >> filename;
cout << "Enter numeric key: ";
cin >> key;
if (choice == 1) {
processFile(filename, filename + ".enc", key);
} else if (choice == 2) {
processFile(filename, "decrypted_" + filename, key);
} else {
cout << "Invalid choice." << endl;
}
}
return 0;
}