#!/bin/bash # Filename: encrypt_and_decrypt.sh # Author: GJS (homelab-alpha) # Date: 2024-05-26T11:59:48+02:00 # Version: 1.0 # Description: This script provides functions to encrypt and decrypt files or # directories using AES-256 encryption algorithm with OpenSSL. # Usage: file-encrypt # - The file or directory to be encrypted. # Function: file-encrypt # Function to encrypt files or directories with AES-256 encryption algorithm using OpenSSL. function file-encrypt() { if [ -z "$1" ]; then echo "Usage: file-encrypt " return 1 fi local input="$1" local output="${input}.aes256" if [ -e "$output" ]; then echo "Error: Output file already exists. Please choose a different name." return 1 fi openssl enc -aes-256-ctr -pbkdf2 -salt -in "$input" -out "$output" if openssl_exit_code=$? && [ $openssl_exit_code -eq 0 ]; then echo "" echo "${input} has been successfully encrypted as ${output}." chmod 644 "$output" else echo "Encryption failed." fi } # Function: file-decrypt # Function to decrypt files or directories encrypted with AES-256 encryption algorithm using OpenSSL. # Description: Decrypts files or directories encrypted with AES-256 encryption algorithm using OpenSSL. # Usage: file-decrypt # - The file to be decrypted. function file-decrypt() { if [ -z "$1" ]; then echo "Usage: file-decrypt " return 1 fi local input="$1" local output="${input%.aes256}" if [ -e "$output" ]; then echo "Error: Output file already exists. Please choose a different name." return 1 fi openssl enc -aes-256-ctr -pbkdf2 -d -salt -in "$input" -out "$output" if openssl_exit_code=$? && [ $openssl_exit_code -eq 0 ]; then echo "" echo "${input} has been successfully decrypted as ${output}." chmod 644 "$output" else echo "Decryption failed." fi }