YaBoiSwayZ

EncryptX v3

May 26th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.93 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. readonly LOG_FILE="encryption_log.txt"
  4. readonly ENCRYPTED_EXT=".enc"
  5.  
  6. log_activity() {
  7.   local level=$1
  8.   local message=$2
  9.   exec 3>>"$LOG_FILE"
  10.   echo "$(date '+%Y-%m-%d %H:%M:%S') - [$level] - $message" >&3
  11.   exec 3>&-
  12. }
  13.  
  14. file_exists() {
  15.   if [ ! -f "$1" ]; then
  16.     log_activity "ERROR" "File $1 does not exist."
  17.     echo "Error: File does not exist."
  18.     exit 1
  19.   fi
  20. }
  21.  
  22. generate_secure_pass() {
  23.   openssl rand -base64 32
  24. }
  25.  
  26. encrypt_file() {
  27.   file_exists "$1"
  28.   local encryption_pass=$(generate_secure_pass)
  29.   echo "Encryption password: $encryption_pass"
  30.   openssl enc -aes-256-cbc -salt -in "$1" -out "$1$ENCRYPTED_EXT" -pass pass:"$encryption_pass" 2> /dev/null
  31.   if [ $? -eq 0 ]; then
  32.     echo "File encrypted successfully: $1$ENCRYPTED_EXT"
  33.     log_activity "INFO" "File $1 encrypted successfully."
  34.   else
  35.     echo "Encryption failed. Please check the file and try again."
  36.     log_activity "ERROR" "Encryption failed for file $1."
  37.   fi
  38. }
  39.  
  40. decrypt_file() {
  41.   file_exists "$1$ENCRYPTED_EXT"
  42.   read -s -p "Please enter the decryption password: " decryption_pass
  43.   echo
  44.   openssl enc -aes-256-cbc -d -salt -in "$1$ENCRYPTED_EXT" -out "$1" -pass pass:"$decryption_pass" 2> /dev/null
  45.   if [ $? -eq 0 ]; then
  46.     echo "File decrypted successfully: $1"
  47.     log_activity "INFO" "File $1 decrypted successfully."
  48.   else
  49.     echo "Decryption failed. Please check the password or file and try again."
  50.     log_activity "ERROR" "Decryption failed for file $1."
  51.   fi
  52. }
  53.  
  54. if [ $# -eq 0 ]; then
  55.   log_activity "ERROR" "No file specified."
  56.   echo "Usage: $0 --encrypt|--decrypt filename"
  57.   exit 1
  58. fi
  59.  
  60. action=$1
  61. file=$2
  62.  
  63. case $action in
  64.   -e|--encrypt)
  65.     encrypt_file "$file"
  66.     ;;
  67.   -d|--decrypt)
  68.     decrypt_file "$file"
  69.     ;;
  70.   *)
  71.     log_activity "ERROR" "Invalid option: $action"
  72.     echo "Invalid option: $action"
  73.     echo "Usage: $0 --encrypt|--decrypt filename"
  74.     exit 1
  75.     ;;
  76. esac
Add Comment
Please, Sign In to add comment