Last active
October 26, 2024 15:13
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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