Skip to content

Instantly share code, notes, and snippets.

@michaelbutler
Last active December 15, 2021 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelbutler/2b4982eb67bfc591f6669b217457e9e2 to your computer and use it in GitHub Desktop.
Save michaelbutler/2b4982eb67bfc591f6669b217457e9e2 to your computer and use it in GitHub Desktop.
Bash/shell script to Encrypt and Decrypt an arbitrary file using a passphrase
#!/bin/sh
set -e
# Required ENV variable: ENC_PASSPHRASE
# Usage: ENC_PASSPHRASE=my_super_long_ascii_pass_phrase123 ./decrypt_file.sh encrypted.enc secretfile.json
if [ -z "$ENC_PASSPHRASE" ]; then
echo "ERROR: Required ENC_PASSPHRASE environment variable NOT passed in."
exit 1
fi
decrypt () {
gpg --batch --pinentry-mode loopback --passphrase "$ENC_PASSPHRASE" \
-o "$2" -d "$1" >/dev/null 2>/dev/null
}
decrypt "$1" "$2"
echo "Decrypted $1 to file $2"
#!/bin/sh
set -e
# Required ENV variable: ENC_PASSPHRASE
# Usage: ENC_PASSPHRASE=my_super_long_ascii_pass_phrase123 ./encrypt_file.sh secretfile.json encrypted.enc
if [ -z "$ENC_PASSPHRASE" ]; then
echo "ERROR: Required ENC_PASSPHRASE environment variable NOT passed in."
exit 1
fi
encr () {
gpg --symmetric --cipher-algo AES256 --passphrase-repeat 0 --batch --pinentry-mode loopback \
--passphrase "$ENC_PASSPHRASE" -o "$2" \
"$1" >/dev/null 2>/dev/null
}
encr "$1" "$2"
echo "Encrypted $1 to file $2"
@michaelbutler
Copy link
Author

michaelbutler commented Nov 16, 2020

Encrypt Example

ENC_PASSPHRASE=my_super_long_ascii_pass_phrase123 ./encrypt_file.sh secretfile.json encrypted.enc

cat encrypted.enc # completely unreadable

Decrypt Example

ENC_PASSPHRASE=my_super_long_ascii_pass_phrase123 ./decrypt_file.sh encrypted.enc secretfile.json

cat secretfile.json # original file

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