Skip to content

Instantly share code, notes, and snippets.

@robertsinfosec
Created September 29, 2024 06:16
Show Gist options
  • Save robertsinfosec/ac78b20b89c3bce81a7d4100b513d01c to your computer and use it in GitHub Desktop.
Save robertsinfosec/ac78b20b89c3bce81a7d4100b513d01c to your computer and use it in GitHub Desktop.
Bash support for quick AES-256 encryption/decryption with a prompted password. Add to the end of your `~/.bashrc`
# Other .bashrc contents here...
encrypt() {
# Read password from /dev/tty
echo -n "Enter password: " > /dev/tty
read -s PASSWORD < /dev/tty
echo > /dev/tty
if [ -t 0 ]; then
INPUT="$*"
else
INPUT=$(cat)
fi
echo -n "$INPUT" | openssl enc -aes-256-cbc -pbkdf2 -iter 10000 -md sha512 -pass pass:"$PASSWORD" | base64 --wrap=0
echo ""
}
decrypt() {
# Read password from /dev/tty
echo -n "Enter password: " > /dev/tty
read -s PASSWORD < /dev/tty
echo > /dev/tty
if [ -t 0 ]; then
# If no input is piped, use the arguments
INPUT="$*"
else
# If input is piped, read from stdin
INPUT=$(cat)
fi
echo -n "$INPUT" | base64 --decode | openssl enc -d -aes-256-cbc -pbkdf2 -iter 10000 -md sha512 -pass pass:"$PASSWORD"
echo ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment