Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- readonly LOG_FILE="encryption_log.txt"
- readonly ENCRYPTED_EXT=".enc"
- log_activity() {
- local level=$1
- local message=$2
- exec 3>>"$LOG_FILE"
- echo "$(date '+%Y-%m-%d %H:%M:%S') - [$level] - $message" >&3
- exec 3>&-
- }
- file_exists() {
- if [ ! -f "$1" ]; then
- log_activity "ERROR" "File $1 does not exist."
- echo "Error: File does not exist."
- exit 1
- fi
- }
- generate_secure_pass() {
- openssl rand -base64 32
- }
- encrypt_file() {
- file_exists "$1"
- local encryption_pass=$(generate_secure_pass)
- echo "Encryption password: $encryption_pass"
- openssl enc -aes-256-cbc -salt -in "$1" -out "$1$ENCRYPTED_EXT" -pass pass:"$encryption_pass" 2> /dev/null
- if [ $? -eq 0 ]; then
- echo "File encrypted successfully: $1$ENCRYPTED_EXT"
- log_activity "INFO" "File $1 encrypted successfully."
- else
- echo "Encryption failed. Please check the file and try again."
- log_activity "ERROR" "Encryption failed for file $1."
- fi
- }
- decrypt_file() {
- file_exists "$1$ENCRYPTED_EXT"
- read -s -p "Please enter the decryption password: " decryption_pass
- echo
- openssl enc -aes-256-cbc -d -salt -in "$1$ENCRYPTED_EXT" -out "$1" -pass pass:"$decryption_pass" 2> /dev/null
- if [ $? -eq 0 ]; then
- echo "File decrypted successfully: $1"
- log_activity "INFO" "File $1 decrypted successfully."
- else
- echo "Decryption failed. Please check the password or file and try again."
- log_activity "ERROR" "Decryption failed for file $1."
- fi
- }
- if [ $# -eq 0 ]; then
- log_activity "ERROR" "No file specified."
- echo "Usage: $0 --encrypt|--decrypt filename"
- exit 1
- fi
- action=$1
- file=$2
- case $action in
- -e|--encrypt)
- encrypt_file "$file"
- ;;
- -d|--decrypt)
- decrypt_file "$file"
- ;;
- *)
- log_activity "ERROR" "Invalid option: $action"
- echo "Invalid option: $action"
- echo "Usage: $0 --encrypt|--decrypt filename"
- exit 1
- ;;
- esac
Add Comment
Please, Sign In to add comment