How to write a simple file encryption program in C?
Introduction:
With the development of the Internet and the popularity of smart devices, the importance of protecting personal data and sensitive information has become increasingly important. In order to ensure the security of files, it is often necessary to encrypt them. This article will introduce how to write a simple file encryption program using C to protect your files from unauthorized access.
Build the program:
First create a new C source file and include the necessary header files:
#include <iostream> #include <fstream> #include <string> using namespace std;
Next, write the main function , and gradually realize the functions of the program according to the requirements in the demand analysis.
3.1 Receive user input:
int main() { string filePath; string key; cout << "请输入待加密文件的路径:"; cin >> filePath; cout << "请输入加密密钥:"; cin >> key; // 其它代码... return 0; }
In this code, we use cin
to receive the file path and encryption key entered by the user, and save them to the corresponding in variables.
3.2 Read the contents of the file to be encrypted:
ifstream inputFile(filePath, ios::binary); if(!inputFile) { cout << "无法打开文件:" << filePath << endl; return 1; } string fileContent((istreambuf_iterator<char>(inputFile)), (istreambuf_iterator<char>())); inputFile.close();
In this code, we use ifstream
to open the file to be encrypted and check whether the file is opened successfully. If the file cannot be opened, an error message is output and the program ends.
Next, we use istreambuf_iterator
to read the content of the file and save it to the fileContent
string.
3.3 Encrypt file content:
Before encrypting file content, we need to implement a simple encryption algorithm. Here we will implement a simple key-based encryption algorithm using simple bit operations (such as XOR).
string encryptedContent = fileContent; for(int i=0; i<encryptedContent.size(); i++) { encryptedContent[i] ^= key[i % key.size()]; }
In this code, we iterate over the fileContent
string and XOR it with the key to encrypt the file content.
3.4 Save the encrypted content to a new file:
string encryptedFilePath = filePath + ".encrypted"; ofstream outputFile(encryptedFilePath, ios::binary); if(!outputFile) { cout << "无法创建加密文件:" << encryptedFilePath << endl; return 1; } outputFile.write(encryptedContent.c_str(), encryptedContent.size()); outputFile.close();
In this code, we use ofstream
to create a new binary file and check whether the file Created successfully. If the file creation fails, an error message is output and the program ends.
Next, we use the write
function to write the encrypted content into a new file and close the file.
3.5 Delete or keep original files:
Users can choose whether to delete original files, or keep original files and delete encrypted files. According to the user's choice, write corresponding code to implement this function.
Before testing, please make sure you have backed up your files to avoid file loss due to improper testing.
Before compiling and running the program, please make sure you have set the correct input parameters so that the program can execute correctly.
The above is the detailed content of How to write a simple file encryption program in C++?. For more information, please follow other related articles on the PHP Chinese website!