Skip to content

How to Encrypt and Decrypt Files with OpenSSL

DodaTech Updated 2026-06-24 2 min read

OpenSSL enc provides symmetric encryption for files using strong algorithms like AES-256. Proper key derivation with PBKDF2 is essential for security. This guide walks through the specific troubleshooting steps to diagnose and resolve file encryption issues.

Before You Begin

Before you begin, be sure to have the following in place:

  • A Linux server with the relevant software installed
  • Access to the command line interface
  • Appropriate permissions (root or sudo)

Quick Fix

Wrong

openssl enc -aes-256-cbc -in secret.txt -out secret.enc

Wrong: Encryption without salt or PBKDF2 key derivation

openssl enc -aes-256-cbc -salt -pbkdf2 -iter 100000 -in secret.txt -out secret.enc -pass pass:YourStrongPassword

Right: Encryption with salt, PBKDF2, and 100K iterations

Output

File encrypted successfully\nEncrypted file: secret.enc\nTo decrypt:\nopenssl enc -d -aes-256-cbc -pbkdf2 -iter 100000 -in secret.enc -out secret.txt

Prevention

To avoid future issues, follow these best practices:

  • Always use -salt to prevent rainbow table attacks on the encryption key
  • Use -pbkdf2 (available in OpenSSL 1.1.1+) for strong key derivation
  • Set a high iteration count (-iter 100000+) to slow down brute-force attacks
  • Use AEAD ciphers like AES-256-GCM when available for authenticated encryption
  • Use -base64 or -a for ASCII-armored output suitable for text-based transport

DodaTech Tools

For further assistance with any of the above issues, consider using DodaTech consulting services or DodaTech tutorials for more in-depth guidance.

Common Mistakes with encrypt file

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

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

What is the difference between AES-256-CBC and AES-256-GCM?|||AES-256-CBC encrypts data in blocks and requires a separate HMAC for integrity. AES-256-GCM is an AEAD cipher that provides both encryption and authentication in one algorithm.
How do I decrypt a file encrypted with openssl? Run openssl enc -d -aes-256-cbc -pbkdf2 -iter 100000 -in secret.enc -out secret.txt. Use the same algorithm and parameters used during encryption.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro