Bash-Snippets/crypt/crypt

44 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
# Author: Alexander Epstein https://github.com/alexanderepstein
## uses openssl aes 256 cbc encryption to encrypt file salting it with password designated by user
encrypt()
{
openssl enc -aes-256-cbc -salt -a -in $1 -out $2 || { echo "File not found"; exit 1; }
echo "Successfully encrypted"
}
## uses openssl aes 256 cbc decryption to decrypt file
decrypt()
{
openssl enc -aes-256-cbc -d -a -in $1 -out $2 || { echo "File not found"; exit 1; }
echo "Successfully decrypted"
}
while getopts ":e:d::" opt; do ## alows for using options in bash
case $opt in
e) ## when trying to encrypt run this
if [[ $# -gt 3 ]]; then
echo "Option -e only accepts two arguments" <&2
exit 1
fi
encrypt $2 $3
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
d) ## when trying to decrypt run this
if [[ $# -gt 3 ]]; then
echo "Option -d only accepts two arguments" <&2
exit 1
fi
decrypt $2 $3
;;
:) ## will run when no arguments are provided to to e or d options
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done