This is a C program that demonstrates file encryption using the libsodium library. The program encrypts a plaintext file with a user-provided password using the XChaCha20-Poly1305 encryption algorithm.
Before running the code, you need to have the libsodium library installed on your system.
The code consists of two primary functions: display_error_message and encrypt_file, and a main function for user interaction.
This function is responsible for displaying error messages on the standard error output. It is used to report errors encountered during initialization, file operations, and encryption.
This function handles the core file encryption process:
-
It initializes the libsodium library using
sodium_init()to ensure it's ready for cryptographic operations. -
Generates a random salt to be used for key derivation using
randombytes_buf. The salt is also written to the beginning of the output file. -
Opens the input and output files in binary mode, checks if they opened successfully, and reports an error if not.
-
Derives an encryption key from the user-provided password and the generated salt using
crypto_pwhash. -
Initializes a
crypto_secretstream_xchacha20poly1305state for encryption with a random nonce. -
Reads data from the input file in chunks, encrypts each chunk, and writes the encrypted data to the output file. It uses
crypto_secretstream_xchacha20poly1305_pushfor encryption.
The main function is responsible for user interaction. It does the following:
-
Defines the input and output filenames and a password buffer.
-
Prompts the user to enter their password securely. It reads the password using
fgetsand removes the newline character. -
Calls the
encrypt_filefunction to perform file encryption with the provided password. -
Displays a success message if the encryption process is completed successfully.
-
Handles any errors that may occur during the process by calling
display_error_message.
Before running the code, make sure you have the libsodium library installed. After compilation, execute the program. You'll be prompted to enter a password, and the plaintext file will be encrypted using XChaCha20-Poly1305.