Skip to content

Instantly share code, notes, and snippets.

@nyteshade
Last active July 6, 2017 02:12
Show Gist options
  • Save nyteshade/adc8a5643f76a8dcf6b50de539f2f5d3 to your computer and use it in GitHub Desktop.
Save nyteshade/adc8a5643f76a8dcf6b50de539f2f5d3 to your computer and use it in GitHub Desktop.
File (de)encryption using openssl
# Encryption and decryption BASH functions using openssl
function decrypt() {
# The first param doesn't contain .enc, write output plus .decrypted if no 2nd
if [ "${1/.enc/}" = "${1}" -a -e "${1}" ]; then
openssl enc -d -aes-256-cbc -in "${1}" -out "${2:-${1}.decrypted}"
# The first param contains .enc, write the output without if no 2nd param
elif [ "${1/.enc/}" != "${1}" -a -e "${1}" ]; then
openssl enc -d -aes-256-cbc -in "${1}" -out "${2:-${1/.enc/}}"
# Just accept first and second an fail if necessary
else
openssl enc -d -aes-256-cbc -in "${1}" -out "${2}"
fi
}
function encrypt() {
# The first param doesn't contain .enc, write output plus .enc if no 2nd
if [ "${1/.enc/}" = "${1}" -a -e "${1}" ]; then
openssl enc -salt -aes-256-cbc -in "${1}" -out "${2:-${1}.enc}"
# Just accept first and second an fail if necessary
else
openssl enc -salt -aes-256-cbc -in "${1}" -out "${2}"
fi
}
@nyteshade
Copy link
Author

To use,

cat functions.sh >> .bashrc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment