How to Encrypt and Decrypt Files with OpenSSL
In this tutorial, you'll learn about How to Encrypt and Decrypt Files with OpenSSL. We cover key concepts, practical examples, and best practices.
The Problem
You need to encrypt a file containing sensitive data — API keys, database credentials, or customer information — but do not have a dedicated encryption tool. OpenSSL can encrypt files using strong symmetric ciphers, but incorrect usage leads to weak encryption or data loss.
Quick Fix
Encrypt a File with AES-256-CBC
WRONG — using a weak cipher or no salt:
openssl enc -des -in secret.txt -out secret.enc -k "password123"
# *** WARNING : deprecated key derivation used.
# Using -iter or -pbkdf2 would be better.
RIGHT — use AES-256-CBC with PBKDF2 key derivation:
openssl enc -aes-256-cbc -salt -pbkdf2 -iter 100000 -in secret.txt -out secret.enc
# enter aes-256-cbc encryption password:
# Verifying - enter aes-256-cbc encryption password:
ls -la secret.enc
# -rw-r--r-- 1 user user 832 Jun 24 12:00 secret.enc
Decrypt the File
openssl enc -d -aes-256-cbc -pbkdf2 -iter 100000 -in secret.enc -out secret.txt
# enter aes-256-cbc decryption password:
cat secret.txt
# This is my secret data
Encrypt with Base64 Encoding (Text-Safe Output)
openssl enc -aes-256-cbc -salt -pbkdf2 -a -in secret.txt -out secret.b64
# enter aes-256-cbc encryption password:
cat secret.b64
# U2FsdGVkX1+8zZ7Q3kHl2g4W5v6B7c8D9e0F1g2H3i4J5k6L7m8N9o0P1q2R3s4T5u6V7w8X9y0Z
WRONG — decrypting without -a for base64 input:
openssl enc -d -aes-256-cbc -pbkdf2 -in secret.b64 -out secret.txt
# bad decrypt
# 140735441479488:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
RIGHT — add -a for base64-decoded input:
openssl enc -d -aes-256-cbc -pbkdf2 -a -in secret.b64 -out secret.txt
# enter aes-256-cbc decryption password:
cat secret.txt
# This is my secret data
Encrypt with a File-Based Key
openssl rand -base64 32 > key.bin
openssl enc -aes-256-cbc -salt -pbkdf2 -in secret.txt -out secret.enc -pass file:key.bin
ls -la secret.enc key.bin
# -rw-r--r-- 1 user user 832 Jun 24 12:01 secret.enc
# -rw-r--r-- 1 user user 45 Jun 24 12:01 key.bin
Use DodaTech's File Vault to manage encrypted secrets across your team — it wraps OpenSSL with key rotation and audit logging built in.
Prevention
- Always use
-pbkdf2with at least 100000 iterations. - Use AES-256-GCM for authenticated encryption when available.
- Store encryption passwords in a password manager, never in code.
- Use file-based keys for automated decryption in pipelines.
- Test decryption immediately after encryption to confirm the file is recoverable.
Common Mistakes with encrypt decrypt
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world OPENSSL code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro