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:
Functions
encrypt_file
anddecrypt_file
:encrypt_file(input_file, output_file)
: Encryptsinput_file
using AES-256-CBC encryption and saves the encrypted output tooutput_file
.decrypt_file(input_file, output_file)
: Decryptsinput_file
using AES-256-CBC decryption and saves the decrypted output tooutput_file
.
Argument Checking:
- Checks if the script is called with exactly four arguments (
$#
). If not, it displays usage instructions and exits.
- Checks if the script is called with exactly four arguments (
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.
- Parses command-line arguments:
Performing Encryption or Decryption:
- Uses a
case
statement to perform either encryption or decryption based on theoperation
argument. - Calls the appropriate function (
encrypt_file
ordecrypt_file
) with the parsed arguments.
- Uses a
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>
withencrypt
to encrypt ordecrypt
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.
- Replace
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.
0 Comments