Developing a File Encryption and Decryption Tool in C Language

Developing a File Encryption and Decryption Tool in C Language

In the modern information age, data security is particularly important. Today, we will learn how to develop a simple file encryption and decryption tool using the C language. This tool implements basic encryption and decryption functions through the XOR algorithm.

1. XOR Encryption Principle

XOR (exclusive or) is a commonly used basic encryption technique that compares two binary values bit by bit and returns the corresponding result:

  • If the two bits are the same, the result is 0.
  • If they are different, the result is 1.

For example:

  • 0 XOR 0 = 0
  • 1 XOR 1 = 0
  • 0 XOR 1 = 1
  • 1 XOR 0 = 1

Utilizing this property, we can achieve simple encryption and decryption by performing XOR operations on the file content. The same data, when XORed again with the previously used key, will restore the original data.

2. Project Preparation

Before starting to code, please ensure that you have installed a C language compilation environment, such as GCC or Clang.

File Structure

We will create the following two source code files in the project:

linefile_encrypt_decrypt.c    // Tool source code

3. Implementation Code

Below is the complete code we will write:

#include <stdio.h>
#include <stdlib.h>
void xorEncryptDecrypt(FILE *input, FILE *output, char *key) {
    int keyLength = strlen(key);
    int i = 0;
    char ch;
    // Read each character from the input file, perform XOR operation and write to output file
    while ((ch = fgetc(input)) != EOF) {
        fputc(ch ^ key[i % keyLength], output);
        i++;
    }
}
int main() {
    char filename[100];
    char key[100];
    printf("Please enter the filename to encrypt or decrypt:");
    scanf("%s", filename);
    printf("Please enter the key:");
    scanf("%s", key);
    // Open input and output files, check if opened successfully (read/write mode)
    FILE *inputFile = fopen(filename, "rb");
    if (inputFile == NULL) {
        perror("Failed to open input file!");
        return EXIT_FAILURE;
    }
    FILE *outputFile;
    // Check if this is the first run, if so, perform encryption, otherwise perform decryption
    outputFile = fopen("output.enc", "wb");
    if (outputFile == NULL) {
        fclose(inputFile);
        perror("Failed to create output file!");
        return EXIT_FAILURE;
    }
    xorEncryptDecrypt(inputFile, outputFile, key);
    printf("Processing complete, you can view the 'output.enc' file.
");
    // Close all opened files.
    fclose(inputFile);
fclose(outputFile);
    return EXIT_SUCCESS;
}

Notes:

  • When using this program, please ensure that there is a file to be encrypted in your working directory.
  • The output will default to generating a new encrypted document named<span>output.enc</span>.

Compilation and Running:

To compile, run the following command in the terminal:

gcc file_encrypt_decrypt.c -o encrypt_decrypt_tool

Then, start the program as follows:

./encrypt_decrypt_tool

Follow the prompts to enter the filename and the required key to complete the subsequent processing.

In this way, a mobile and stable code platform for information protection can be obtained through simple encryption.

Conclusion

This article introduced how to build a simple and effective file protection tool using the C language. Although we adopted a basic and straightforward method, this approach still holds significant importance in ensuring basic security performance in practical applications. I hope this article helps you gain a deeper understanding of the C language and its potential in data security.

Leave a Comment