Skip to content

Instantly share code, notes, and snippets.

@Fastidious
Last active July 25, 2019 18:58
Show Gist options
  • Save Fastidious/c66b67bcd8933c43f1a8 to your computer and use it in GitHub Desktop.
Save Fastidious/c66b67bcd8933c43f1a8 to your computer and use it in GitHub Desktop.
OpenSSL encrypt/decrypt

Encryption with openssl

Encrypt:

openssl enc -e -aes256 -salt -in file-to-encrypt -out file-encrypted.encrypted

Use this one instead, if base64 is needed (send encrypted file as text on email, or messaging).

openssl enc -aes256 -a -in test.txt -out test.txt.enc

If openssl 1.1 or higher, use:

openssl enc -aes256 -a -iter 1000 -in test.txt -out test.txt.enc
#!/bin/bash
# Encrypt
clear
echo '+--------------------------------------------------------------------+'
echo '|If an encrypted file with same name exists, it will be overwritten. |'
echo '+--------------------------------------------------------------------+'
echo '|        REMEMBER YOUR PASSWORD, OR YOU WILL LOSE YOUR DATA!         |'
echo '+--------------------------------------------------------------------+'
if [ -z "$1" ]; then
me=`basename "$0"`
echo 'ERROR: Missing parameters. Enter file name to encrypt.'
echo ''
echo 'Usage:'
else 
openssl enc -e -aes256 -salt -p -in $1 -out $1.aes
fi

Decrypt:

openssl enc -d -aes256 -in file-encrypted.encrypted -out file-decrypted

Use this one instead, if aes256 and base64 was used.

openssl enc -d -aes256 -base64 -in file-encrypted.encrypted -out file-decrypted

If openssl 1.1 or higher was used, then:

openssl enc -d -aes256 -a -iter 1000 -in test.txt.enc -out test.txt
#!/bin/bash
# Decrypt
clear
echo '+--------------------------------------------------------------------+'
echo '|       If a file with same name exists, it will be overwritten.     |'
echo '+--------------------------------------------------------------------+'
read -p "Press CTRL C to abort, or any key to proceed..."
if [ -z "$1" ]; then
me=`basename "$0"`
echo 'ERROR: Missing parameters. Enter file name to decrypt.'
echo ''
echo 'Usage:'
echo "$me filetodecrypt"
else
encryptedfile=$1
unencryptedfile="${encryptedfile%.*}"
openssl enc -d -aes256 -in $encryptedfile -out $unencryptedfile
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment