Home > Backend Development > C++ > body text

How to write a simple file encryption program in C++?

WBOY
Release: 2023-11-03 15:40:59
Original
766 people have browsed it

How to write a simple file encryption program in C++?

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.

  1. Requirements analysis:
    Before we start writing a file encryption program, we need to clarify the basic functions and requirements of the program. In this simple program, we will use symmetric encryption algorithm to encrypt and decrypt files. The specific requirements are as follows:
  2. The program should be able to receive user input, including the path of the file to be encrypted and the encryption key.
  3. The program should be able to read the contents of the file to be encrypted and encrypt it using the key.
  4. The encrypted content should be saved to a new file, retaining the original file extension.
  5. Users can choose whether to keep the original files, or delete the original files and keep the encrypted files.
  6. Development environment and preparations:
    Before writing a C program, we need to ensure that we have the following tools and environment:
  7. Compiler: It is recommended to use GCC or MinGW to compile C programs.
  8. Text editor: such as Visual Studio Code, Dev-C or Sublime Text, etc.
  9. Be familiar with the basic knowledge of C language and related knowledge of file operations.
  10. Build the program:
    First create a new C source file and include the necessary header files:

    #include 
    #include 
    #include 
    using namespace std;
    Copy after login

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;
}
Copy after login

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(inputFile)), (istreambuf_iterator()));
inputFile.close();
Copy after login

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
Copy after login

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();
Copy after login

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.

  1. Test program:
    After completing the code writing, we can conduct some simple tests to verify the function of the program.

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.

  1. Summary:
    This article introduces how to use C to write a simple file encryption program. By analyzing requirements, writing code and conducting simple tests, we can implement a basic file encryption program. However, this is just a simple example, if you have higher requirements (such as using more secure encryption algorithms), you can further expand and improve this program.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!