forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
File_encryption.cpp
43 lines (32 loc) · 1.14 KB
/
File_encryption.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
#include <iostream>
#include <fstream>
#include <string>
void encryptFile(const std::string& inputFileName, const std::string& outputFileName, int key) {
std::ifstream inputFile(inputFileName);
std::ofstream outputFile(outputFileName);
char ch;
while (inputFile.get(ch)) {
ch = ch + key;
outputFile.put(ch);
}
inputFile.close();
outputFile.close();
}
void decryptFile(const std::string& inputFileName, const std::string& outputFileName, int key) {
encryptFile(inputFileName, outputFileName, -key); // Decryption is the same as encryption with the negative key
}
int main() {
std::string inputFileName, outputFileName;
int key;
std::cout << "Enter input file name: ";
std::cin >> inputFileName;
std::cout << "Enter output file name: ";
std::cin >> outputFileName;
std::cout << "Enter encryption key: ";
std::cin >> key;
encryptFile(inputFileName, outputFileName, key);
std::cout << "File encrypted successfully." << std::endl;
decryptFile(outputFileName, "decrypted_" + outputFileName, key);
std::cout << "File decrypted successfully." << std::endl;
return 0;
}