How to Create a Bash File Decryption Utility

How to Create a Bash File Decryption Utility

How to Create a Bash File Decryption Utility

Encrypted files are only useful if decryption is just as easy as encryption. Yet in practice, decryption commands tend to be the ones people forget — the right cipher, the right flags, the right key file — usually right when it’s needed under time pressure. A dedicated Bash decryption utility removes that friction, wrapping standard, well-audited tools like openssl and gpg into a single, memorable command.

Before you proceed, make sure you have OpenSSL installed on your system.

Here’s a basic Bash script for decrypting a file:

#!/bin/bash

# Check if an encrypted file and password are provided
if [ $# -ne 2 ]; then
    echo "Usage: $0  "
    exit 1
fi

encrypted_file=$1
password=$2

# Check if the encrypted file exists
if [ ! -f "$encrypted_file" ]; then
    echo "Error: Encrypted file '$encrypted_file' not found."
    exit 1
fi

# Decrypt the file using AES-256-CBC
openssl enc -d -aes-256-cbc -in "$encrypted_file" -out "${encrypted_file%.enc}" -pass pass:"$password"

echo "File '$encrypted_file' decrypted."

Here’s how the script works:

  1. It checks if both an encrypted file and a password are provided as command-line arguments. If not, it prints a usage message and exits.
  2. The provided encrypted file and password are stored in the encrypted_file and password variables.
  3. It checks if the encrypted file exists. If not, it prints an error message and exits.
  4. It uses OpenSSL to decrypt the file (-d specifies decryption).
  5. The decrypted file is saved without the .enc extension.
  6. It prints a message indicating that the file has been decrypted.

Usage example:

./decryption_utility.sh file.txt.enc MySecretPassword

This will decrypt file.txt.enc using AES-256-CBC with the password “MySecretPassword” and save the decrypted file as file.txt.

Please note that this is a basic example. Depending on your requirements, you might want to add more features, error handling, or support for different encryption algorithms or methods. Always handle passwords and encryption keys securely.

Exit mobile version