How to encrypt and decrypt a file on the command line

Encrypt and decrypt files using bash and OpenSSL

1 minute read

Soon or later we may need to encrypt files, here a quick way to encrypt/decrypt file from the command line.

How to Encrypt

#!/bin/bash
set -eu
if [ $# -eq 0 ]
  then
    echo "No arguments supplied"
    echo "Usage $0 filename"
    exit 1
fi
ORIGINAL_FILE=$1
echo -n Password:
read -s ENCRYPTION_PASSWORD
echo
ENCRYPTED_EXTENSION=".enc"
ENCRYPTED_FILE="${ORIGINAL_FILE}${ENCRYPTED_EXTENSION}"
openssl enc -aes-256-cbc -in $ORIGINAL_FILE -out $ENCRYPTED_FILE -k "$ENCRYPTION_PASSWORD"
echo "$ENCRYPTED_FILE created"

How to Decrypt

#!/bin/bash
set -eu

if [ $# -eq 0 ]
  then
    echo "No arguments supplied"
    echo "Usage $0 filename"
    exit 1
fi
ENCRYPTED_FILE=$1
echo -n Password:
read -s ENCRYPTION_PASSWORD
echo
ENCRYPTED_EXTENSION=".enc"
DECRYPTED_FILE="${ENCRYPTED_FILE%.enc}"
openssl enc -aes-256-cbc -d -in $ENCRYPTED_FILE -k "$ENCRYPTION_PASSWORD" > $DECRYPTED_FILE
echo "$DECRYPTED_FILE"
comments powered by Disqus