Skip to content

Instantly share code, notes, and snippets.

@alvarow
Created October 24, 2017 13:20
Show Gist options
  • Save alvarow/4b41e8163b96c2a830ffe34844a0eee6 to your computer and use it in GitHub Desktop.
Save alvarow/4b41e8163b96c2a830ffe34844a0eee6 to your computer and use it in GitHub Desktop.
Encrypts & Decrypts single files using OpenSSL
# Source it on .bash_profile
# run with "encrypt a.file" or "decrypt a.file.aes-128-cbc"
# You can adjust encryption cipher (say aes-256-cbc) to your needs
function encrypt() {
if [ $# -eq 0 ]; then
echo "Provide a filename to encrypt"
exit 1
fi
# SHA256 the given file
openssl dgst -sha256 "$1" > "$1.sha256"
# Encrypt the given file
time openssl aes-128-cbc -salt -in "$1" -out "$1".aes-128-cbc
openssl dgst -sha256 "$1".aes-128-cbc >> "$1.sha256"
}
function decrypt() {
if [ $# -eq 0 ]; then
echo "Provide a filename to decrypt"
exit 1
fi
DEST=$(basename "$1" .aes-128-cbc)
if [ -e "$DEST" ]; then
echo "FOUND an existing copy of $DEST, moving it to $DEST.$(date +%Y%m%d%H%M%S)"
mv -v $DEST $DEST.$(date +%Y%m%d%H%M%S)
fi
# Decrypt the given file
time openssl aes-128-cbc -d -salt -in "$1" -out $(basename "$1" .aes-128-cbc)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment