A simple command-line utility for encrypting and decrypting files using the XChaCha20-Poly1305 Authenticated Encryption with Associated Data (AEAD) algorithm provided by libsodium.
You need a C++ compiler that supports C++20, the libsodium library and OpenSSL.
You can run this command to install everything the project requires:
sudo apt update && sudo apt upgrade && sudo apt install build-essentials ninja-build libsodium-dev pkg-configYou can choose from two available presets defined in the CMakePresets.json file: using MinGW or MSVC as the compiler.
- Install Visual Studio:
- Download and install Visual Studio from the Visual Studio website.
- During installation, select the "Desktop development with C++" workload. This will install the compiler and CMake.
- Install vcpkg:
- Choose a directory to install
vcpkg, such asC:\dev\vcpkg. Avoid paths with spaces likeProgram Files. - Using Windows Terminal, clone and bootstrap
vcpkg:
git clone https://github.com/microsoft/vcpkg.git; cd vcpkg; .\bootstrap-vcpkg.bat- Put the path to your
vcpkginstallation as a system environment variable and restart your terminal.
- Install
libsodium:
- If you are compiling for 64-bit Windows, execute the following in a terminal:
vcpkg install libsodium:x64-windows - If you are compiling for 32-bit Windows, execute the following in a terminal:
vcpkg install libsodium:x86-windows
- Set Environment Variable: In your system environment variables, create a new variable:
- Variable name:
CMAKE_TOOLCHAIN_FILE - Variable value:
[path-to-vcpkg]/scripts/buildsystems/vcpkg.cmakeRestart your terminal session for the new environment variable to take effect.
- Install MSYS2 and
g++:
- Download and install MSYS2 from their website.
- It is recommended to create the following system variable:
- Name:
MSYS64 - Value:
[path-to-you]\msys64(ex.C:\msys64)
- Name:
- If you want to compile on 64-bit system,
- Launch the UCRT64 terminal and execute
pacman -S mingw-w64-ucrt-x86_64-gcc. - Paste the following path to your system variable:
%MSYS64%\ucrt64\bin
- Launch the UCRT64 terminal and execute
- If you want to compile on 32-bit system,
- Launch the MINGW32 terminal and execute
pacman -S mingw-w64-i686-gcc. - Paste the following path to your system variable:
%MSYS64%\mingw32\bin
- Launch the MINGW32 terminal and execute
- Install
vcpkg:
- Follow the steps from above.
- Install
libsodium:
- If you are compiling for 64-bit Windows, execute the following in a terminal:
vcpkg install libsodium:x64-mingw-dynamic
- If you are compiling for 32-bit Windows, execute the following in a terminal:
vcpkg install libsodium:x86-mingw-dynamic libsodium:x86-mingw-static
- Clone the Repository: Run the following command in a PowerShell, Windows Terminal or Bash session:
git clone https://github.com/Omega493/crypto-utils.git- Set Up Build Environment:
- Select your desired preset from the list of available presets.
- If it uses MSVC, open the cloned folder in Visual Studio, select the preset and press Control-Shift-B to build.
- If it uses
g++, or is Linux,-
Configure CMake: Open a terminal session in the cloned folder and execute
cmake --preset <preset_name>
Replace
<preset_name>with your desired preset, ex.windows-mingw-x86-release. -
Build: Execute
cmake --build --preset=<preset_name>
where
<preset_name>is the same from the step before.
-
- Locate the Executable:
The executable will be in the
binaryDirspecified in the preset:
- Windows Example:
./build/windows/<preset_name>/encryptor.exe - Linux Example:
./build/linux/<preset_name>/encryptor
The program is a single executable named encryptor. It uses flags to determine the operation (encrypt or decrypt) and prompts for a secret key.
- Get a Secret Key
- For Encryption: Generate a secure 256-bit (32-byte) key. Do this once per secret and store the key in a safe place like a password manager.
openssl rand -hex 32
- For Decryption: You will need the exact same key that was used to encrypt the file.
- Run the Program
-
Synopsis:
encryptor <-e <input_file> | -d <input_file>> [-o <output_file>] [-h] -
Options:
-e, --encrypt <input_file>: Specifies the input file to be encrypted-d, --decrypt <input_file>: Specifies the input file to be decrypted-o, --output <output_file>: (Optional) Specifies the path for the output file (if not provided, the output will be[base_name].encor[base_name].dec)-h, --help: Show the help message
-
Examples:
- On Windows:
# To encrypt a file ./build/windows/x64-release/encryptor.exe -e "my_document.txt" -o "my_document.enc" # To decrypt a file ./build/windows/x64-release/encryptor.exe -d "my_document.enc" -o "my_document.txt" # To encrypt using the default output name (e.g., "secret.txt" -> "secret.enc") ./build/windows/x64-release/encryptor.exe -e "secret.txt"
- On Ubuntu / Debian:
# To encrypt a file ./build/linux/linux-debug/encryptor -e "my_document.txt" -o "my_document.enc" # To decrypt a file ./build/linux/linux-debug/encryptor -d "my_document.enc" -o "my_document.txt" # To encrypt using the default output name (e.g., "secret.txt" -> "secret.enc") ./build/linux/linux-debug/encryptor -e "secret.txt"
-
After running the command, you will be prompted:
Enter the secret key (hex):
Just paste or type your key and press Enter.
-
On Windows, you can even add the path to the executable to your system environment variables if you wish to call it from anywhere.
- Copy the path to the build directory which holds the executable (ex.
E:\Programs\crypto-utils\build\windows\<preset_name>\) - Or, you can place the executable to some other directory and copy the path to that directory. Make sure the executable itself and the required
libsodium.dllis present in your custom location. Initially they should be present in the build directory. - In both the cases, simply add that location to your system's environment paths.
- Examples:
The examples remain almost the same:
# To encrypt a file encryptor -e "my_document.txt" -o "my_document.enc" # To decrypt a file encryptor -d "my_document.enc" -o "my_document.txt" # To encrypt using the default output name (e.g., "secret.txt" -> "secret.enc") encryptor -e "secret.txt"
- Copy the path to the build directory which holds the executable (ex.
- 64-bit OS
windows-msvc-x64-debugwindows-msvc-x64-release
- 32-bit OS
windows-msvc-x86-debugwindows-msvc-x86-release
- 64-bit OS
windows-mingw-x64-debugwindows-mingw-x64-release
- 32-bit OS
windows-mingw-x86-debugwindows-mingw-x86-release
linux-debuglinux-release