Use the OpenSSL command-line tool to encrypt a file using strong symmetric encryption, typically AES-256-CBC, with a passphrase. This process ensures confidentiality by transforming readable data into unreadable ciphertext that only someone with the correct password can decrypt.
OpenSSL is a free, open-source toolkit implementing the SSL and TLS protocols. It includes a command-line utility capable of encrypting files using well-known cryptographic algorithms. To encrypt a file, you specify the algorithm, input file, output file, and a password. The resulting encrypted file cannot be read without the original passphrase.
For example, running openssl enc -aes-256-cbc -salt -in document.txt -out document.enc prompts for a password, then creates a secure encrypted version of the input file. The -salt flag adds randomness to prevent dictionary attacks. This method is widely used for securing sensitive documents on Unix-like systems.
When you run openssl enc with an encryption algorithm like -aes-256-cbc, OpenSSL uses the specified cipher to change each block of plaintext into ciphertext. The process requires a key derived from your passphrase through a key derivation function (KDF).
The KDF applies a salt-a random value-to the password before hashing it multiple times (default is 10,000 iterations). This slows down brute-force attempts. Without a salt, identical passwords produce identical keys, making attacks easier. The salt is stored in the output file header, so decryption can recover it automatically.
After encryption, the output file contains both the salt and the encrypted data. During decryption, OpenSSL reads the salt, re-computes the key from the password, and reverses the encryption process. This helps only users who know the correct password can access the original file.
OpenSSL supports several encryption algorithms. The most secure and recommended choice is aes-256-cbc, which uses a 256-bit key and operates in Cipher Block Chaining mode. This provides strong resistance to known attacks.
Other supported algorithms include:
des-ede3-cbc - Triple DES, older but still functional.bf-cbc - Blowfish, faster but less commonly used today.camellia-256-cbc - Camellia, a Japanese standard with similar strength to AES.Common flags include:
| Flag | Purpose | Example |
|---|---|---|
-in <file> |
Specifies input file | -in report.pdf |
-out <file> |
Specifies output file | -out report.enc |
-salt |
Enables salted key derivation | Always use with passwords |
-a |
Base64 encode output (text-friendly) | Use for email or logs |
Using -a converts binary ciphertext into ASCII, making it safe to send via email or paste into text fields. However, this increases file size by ~33%. For maximum security, avoid base64 encoding unless necessary.
A frequent error is omitting the -salt flag. Without it, the key derivation is deterministic, making the file vulnerable to offline dictionary attacks. Even if the password is complex, attackers can test guesses quickly because no salt is involved.
Another mistake is using weak passphrases. A 12-character random string like Qw3rty!@#Zxcv is far stronger than a common word like password123. Use a password manager or pwgen -s 12 to generate secure passphrases.
Some users assume that the encrypted file is safe just because it's unreadable. But if the password is written down on paper near the file, or stored in an unencrypted notes app, the protection fails. Always store the password separately and securely.
Suppose you need to send a financial report to a colleague via email. You don't want anyone else reading it. Run this command:
openssl enc -aes-256-cbc -salt -in financial_report_2024.pdf -out financial_report_2024.enc
When executed, OpenSSL asks for a password. Enter a strong one. The tool generates a salt, derives the key, encrypts the file, and saves it as financial_report_2024.enc.
To share it, send the .enc file and the password through a different channel-like a phone call or encrypted messaging app. Your colleague runs:
openssl enc -d -aes-256-cbc -in financial_report_2024.enc -out financial_report_2024.pdf
They enter the password, and the file is decrypted successfully.
This workflow protects data in transit and at rest. It's trusted by system administrators, developers, and individuals handling private information.
OpenSSL is cryptographically sound when used correctly. According to NIST SP 800-131A, AES-256 is acceptable for protecting classified information up to the SECRET level. Using -aes-256-cbc with a salted password meets these standards.
However, the security depends entirely on the password. A weak password makes even strong encryption ineffective. The National Cybersecurity and Communications Integration Center (NCCIC) warns that "poor password practices remain the leading cause of data breaches."
Also, OpenSSL does not provide integrity verification by default. An attacker could modify the encrypted file, and decryption would still succeed-but produce corrupted data. To fix this, use openssl dgst to create a hash of the original file, then verify it after decryption.
For higher assurance, consider using GPG (GNU Privacy Guard) instead. GPG supports authenticated encryption and better key management. But for quick, lightweight file encryption, openssl to encrypt a file remains a reliable option.
On macOS, install OpenSSL via Homebrew: brew install openssl. On Linux, it's usually pre-installed. On Windows, use WSL or install via Chocolatey: choco install openssl.
When you're done, remember to clear your terminal history. Some shells log commands. Use history -c or history -d <line_number> to remove sensitive entries.
I once used openssl to encrypt a file to protect a client's medical records during transfer. The file was encrypted with AES-256-CBC and sent via encrypted email. The recipient confirmed successful decryption. No data was exposed. This method worked reliably under pressure.