Advertisement

Shell Script For File Encryption/Decryption Script

Linux Shell Script For File Encryption /Decryption Script

File Encryption/Decryption Script:

#!/bin/bash
file="/path/to/file.txt"
# Encrypt file using AES-256-CBC
openssl enc -aes-256-cbc -salt -in "$file" -out "$file.enc"
echo "File encrypted: $file.enc"

Shell script that utilizes openssl for file encryption and decryption using symmetric encryption (AES):

#!/bin/bash

# Function to encrypt a file
encrypt_file() {
    local input_file="$1"
    local output_file="$2"
    openssl enc -aes-256-cbc -salt -in "$input_file" -out "$output_file"
}

# Function to decrypt a file
decrypt_file() {
    local input_file="$1"
    local output_file="$2"
    openssl enc -d -aes-256-cbc -in "$input_file" -out "$output_file"
}

# Check the number of arguments
if [ "$#" -ne 4 ]; then
    echo "Usage: $0 <encrypt/decrypt> <input_file> <output_file> <password>"
    exit 1
fi

# Parse arguments
operation="$1"
input_file="$2"
output_file="$3"
password="$4"

# Perform encryption or decryption based on operation
case "$operation" in
    encrypt)
        encrypt_file "$input_file" "$output_file" "$password"
        echo "Encryption complete. Encrypted file: $output_file"
        ;;
    decrypt)
        decrypt_file "$input_file" "$output_file" "$password"
        echo "Decryption complete. Decrypted file: $output_file"
        ;;
    *)
        echo "Invalid operation. Choose 'encrypt' or 'decrypt'."
        exit 1
        ;;
esac
#!/bin/bash

# Function to encrypt a file
encrypt_file() {
    local input_file="$1"
    local output_file="$2"
    openssl enc -aes-256-cbc -salt -in "$input_file" -out "$output_file"
}

# Function to decrypt a file
decrypt_file() {
    local input_file="$1"
    local output_file="$2"
    openssl enc -d -aes-256-cbc -in "$input_file" -out "$output_file"
}

# Check the number of arguments
if [ "$#" -ne 4 ]; then
    echo "Usage: $0 <encrypt/decrypt> <input_file> <output_file> <password>"
    exit 1
fi

# Parse arguments
operation="$1"
input_file="$2"
output_file="$3"
password="$4"

# Perform encryption or decryption based on operation
case "$operation" in
    encrypt)
        encrypt_file "$input_file" "$output_file" "$password"
        echo "Encryption complete. Encrypted file: $output_file"
        ;;
    decrypt)
        decrypt_file "$input_file" "$output_file" "$password"
        echo "Decryption complete. Decrypted file: $output_file"
        ;;
    *)
        echo "Invalid operation. Choose 'encrypt' or 'decrypt'."
        exit 1
        ;;
esac

Explanation:

  1. Functions encrypt_file and decrypt_file:

    • encrypt_file(input_file, output_file): Encrypts input_file using AES-256-CBC encryption and saves the encrypted output to output_file.
    • decrypt_file(input_file, output_file): Decrypts input_file using AES-256-CBC decryption and saves the decrypted output to output_file.
  2. Argument Checking:

    • Checks if the script is called with exactly four arguments ($#). If not, it displays usage instructions and exits.
  3. Parsing Arguments:

    • Parses command-line arguments:
      • operation: Specifies whether to "encrypt" or "decrypt".
      • input_file: Path to the input file to be encrypted or decrypted.
      • output_file: Path to save the encrypted or decrypted output.
      • password: Password used for encryption/decryption.
  4. Performing Encryption or Decryption:

    • Uses a case statement to perform either encryption or decryption based on the operation argument.
    • Calls the appropriate function (encrypt_file or decrypt_file) with the parsed arguments.

Usage:

  • Save the script in a file (e.g., encrypt_decrypt.sh) and make it executable (chmod +x encrypt_decrypt.sh).
  • Run the script with the following format:
    php
    ./encrypt_decrypt.sh <encrypt/decrypt> <input_file> <output_file> <password>
    • Replace <encrypt/decrypt> with encrypt to encrypt or decrypt to decrypt.
    • Replace <input_file> with the path to the file you want to encrypt or decrypt.
    • Replace <output_file> with the path where you want to save the encrypted or decrypted file.
    • Replace <password> with the password used for encryption/decryption.

Notes:

  • Ensure that openssl is installed on your system as it is used for encryption and decryption.
  • This script uses symmetric encryption (AES-256-CBC) with a password. Ensure you securely manage and share the password for decryption purposes.
  • Consider additional security measures such as securely storing passwords and managing access permissions to encrypted files.


Post a Comment

0 Comments