Skip to content

Instantly share code, notes, and snippets.

@mtbiker-s
Created June 21, 2019 07:31
Show Gist options
  • Save mtbiker-s/7c0c988fdb542641f7d09663a5778e0e to your computer and use it in GitHub Desktop.
Save mtbiker-s/7c0c988fdb542641f7d09663a5778e0e to your computer and use it in GitHub Desktop.
Bash functions for encrypting and decrypting tar.gz
# These functions can be added to your .bash_aliases or your .bashrc
# So that it can help you encrypt or decrypt .tar.gz files
# Takes 2 params
# $1 is name of file or dir you want to tar
# $2 is name tar file, does not need the tar.gz extension it will added it.
function encrypt-tar-gz(){
dataToEncrypt=$1
fileName=$2".tar.gz"
echo "Attempting to encrypt data to "$fileName
# Old way which would warn about better encryption using -iter or -pbkdf2
#tar -cvzf - $dataToEncrypt | openssl enc -aes-256-cbc -e > $fileName
# Better way of encrypting with -pbkdf2
tar -cvzf - $dataToEncrypt | openssl enc -aes-256-cbc -pbkdf2 > $fileName
}
# Can take 2 params
# $1 is name of tar.gz to decrypt (If only this param is passed, will extract all contents of tar.gz to current dir)
# $2 is the name of the directory where you want to extract tar.gz contents to, if dir does not exist it will create it.
function decrypt-tar-gz(){
fileToDecrypt=$1
placeInDir=$2
echo "Attempting to decrypt "$fileToDecrypt
if [[ $placeInDir == "" ]];then
# Will extract contents from tar.gz to current directory
# Old way to decrypt when not using pbkdf2
#openssl enc -d -aes256 -in $fileToDecrypt | tar xz
# Need to decrypt files encrypted with pbkdf2 cipher
openssl aes-256-cbc -d -pbkdf2 -in $fileToDecrypt | tar xz
else
# Will decrypt the tar.gz file and place in a directory passed
# Old way to decrypt
# openssl enc -d -aes256 -in $fileToDecrypt | tar xz -C $placeInDir
if [[ ! -d $placeInDir ]];then
echo "Directory "$placeInDir" does not exist, attempting to create it..."
mkdir -p $placeInDir
fi
# Decrypts files encrypted with pbkdf2 cipher
openssl aes-256-cbc -d -pbkdf2 -in $fileToDecrypt | tar xz -C $placeInDir
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment