Created
June 24, 2023 01:32
-
-
Save ibrezm1/ebe19f9435cb3321a4e56ff0ffd4b79a to your computer and use it in GitHub Desktop.
encrypt local files
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/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