Skip to content

Instantly share code, notes, and snippets.

@ibrezm1
Created June 24, 2023 01:32
Show Gist options
  • Save ibrezm1/ebe19f9435cb3321a4e56ff0ffd4b79a to your computer and use it in GitHub Desktop.
Save ibrezm1/ebe19f9435cb3321a4e56ff0ffd4b79a to your computer and use it in GitHub Desktop.
encrypt local files
#!/bin/bash
# chmod +x encrypt_decrypt.sh
#
# export ENCRYPTION_KEY="mysecretkey"
#
# Encrypt a file
#./encrypt_decrypt.sh encrypt sensitive_file.txt
#
# Decrypt a file
#./encrypt_decrypt.sh decrypt sensitive_file.txt.enc
# Usage: encrypt_decrypt.sh encrypt <file_path>
# encrypt_decrypt.sh decrypt <file_path>
# Check if the encryption key is set
if [[ -z "$ENCRYPTION_KEY" ]]; then
echo "Encryption key not set. Please set the 'ENCRYPTION_KEY' environment variable."
exit 1
fi
# Function to encrypt a file
encrypt_file() {
local file="$1"
openssl aes-256-cbc -a -salt -in "$file" -out "$file.enc" -pass "env:ENCRYPTION_KEY"
}
# Function to decrypt a file
decrypt_file() {
local file="$1"
openssl aes-256-cbc -a -d -in "$file" -out "${file%.enc}" -pass "env:ENCRYPTION_KEY"
}
# Check the command-line arguments
if [[ "$1" == "encrypt" ]]; then
encrypt_file "$2"
echo "File encrypted: $2.enc"
elif [[ "$1" == "decrypt" ]]; then
decrypt_file "$2"
echo "File decrypted: ${2%.enc}"
else
echo "Invalid command. Usage: encrypt_decrypt.sh encrypt <file_path> OR encrypt_decrypt.sh decrypt <file_path>"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment