-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryptionDecryptionTool.cpp
60 lines (50 loc) · 1.53 KB
/
EncryptionDecryptionTool.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
// Encryption function
void encryptDecrypt(std::ifstream &inputFile, std::ofstream &outputFile, const std::string &key) {
char ch;
size_t keyLength = key.length();
size_t i = 0;
while (inputFile >> std::noskipws >> ch) {
ch = ch ^ key[i % keyLength];
outputFile << ch;
i++;
}
}
int main() {
std::string inputFile, outputFile, key;
int choice;
std::cout << "File Encryption/Decryption Tool\n";
std::cout << "1. Encrypt\n";
std::cout << "2. Decrypt\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
std::cout << "Enter input file name: ";
std::cin >> inputFile;
std::cout << "Enter output file name: ";
std::cin >> outputFile;
std::cout << "Enter encryption/decryption key: ";
std::cin >> key;
std::ifstream inFile(inputFile, std::ios::binary);
std::ofstream outFile(outputFile, std::ios::binary);
if (!inFile || !outFile) {
std::cerr << "Error opening files.\n";
return 1;
}
if (choice == 1) {
encryptDecrypt(inFile, outFile, key);
std::cout << "File encrypted successfully.\n";
} else if (choice == 2) {
encryptDecrypt(inFile, outFile, key);
std::cout << "File decrypted successfully.\n";
} else {
std::cerr << "Invalid choice.\n";
return 1;
}
inFile.close();
outFile.close();
return 0;
}