This commit is contained in:
Frank Bischof 2024-02-21 11:29:21 +01:00
parent 8bb5fef6c7
commit b5dd1cf1d0
3 changed files with 39 additions and 27 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
keys

View File

@ -7,4 +7,4 @@ Script to encrypt and decrypt files easily using openssl
-d for decrypt
## Additional info
Uses des-ede3-cbc pbkdf2 with salt by default but can be configured in the $KEY variable.\
Files are converted using base64 encoding.
Files are converted using base64 encoding.

View File

@ -1,6 +1,5 @@
#!/bin/bash
# Author: F. Bischof (info@meer-web.nl)
KEYS="-des-ede3-cbc -pbkdf2"
# Check for input file
if [ -n "$2" ]
@ -12,44 +11,56 @@ then
exit 2
fi
else
echo "Please submit a input file"
echo "Usage: ./crypter.sh -[e/d] [inputfile]";
exit 1
fi
# Check if keys are set
if [ ! -f keys ]; then echo "KEYS=\"-des-ede3-cbc -pbkdf2\"" > keys; fi
source keys
# Check method
case "$1" in
"-e")
METHOD='encrypt'
echo "Encrypting file ${INPUT}"
OUTPUT="${INPUT}.crypto"
openssl enc $KEYS -salt -in ${INPUT} -out ${OUTPUT}.temp
base64 ${OUTPUT}.temp > ${OUTPUT}
rm -f ${OUTPUT}.temp
if [ "`stat ${OUTPUT} | grep Size | awk '{ print $2}'`" != 0 ]
then
# OK - safe to delete the original input file
rm -f ${INPUT}
exit 0
else
# NOK - remove empty output file
echo "Output file is 0 bytes! Not removing the source file"
rm -f ${OUTPUT}
exit 2
fi
;;
"-d")
METHOD='decrypt'
OUTPUT="`echo $INPUT | sed 's/\.crypto//'`"
echo "Decrypting file ${INPUT}";
OUTPUT="`echo $INPUT | sed 's/\.crypto//'`" #Tim of the last extension in future update"
base64 -d ${INPUT} > ${INPUT}.temp
openssl enc -d $KEYS -in ${INPUT}.temp -out ${OUTPUT}
rm -f ${INPUT}.temp
exit
if [ "`stat ${OUTPUT} | grep Size | awk '{ print $2}'`" != 0 ]
then
# OK - safe to delete the original input file
rm -f ${INPUT}
else
# NOK - remove empty output file
echo "Output file is 0 bytes! Not removing the source file"
rm -f ${OUTPUT}
exit 2
fi
exit 0
;;
*)
echo "Usage: ./crypter.sh -[e/d] [inputfile]";
exit 1
;;
esac
case "$METHOD" in
"encrypt")
echo "Encrypting";
openssl enc $KEYS -salt -in ${INPUT} -out ${OUTPUT}.temp
base64 ${OUTPUT}.temp > ${OUTPUT}
rm -f ${OUTPUT}.temp
rm -f ${INPUT}
echo "Done!"
exit 0
;;
"decrypt")
echo "Decrypting";
base64 -d ${INPUT} > ${INPUT}.temp
openssl enc -d $KEYS -in ${INPUT}.temp -out ${OUTPUT}
rm -f ${INPUT}.temp
rm -r ${INPUT}
echo "Done!"
exit 0
;;
esac
exit 3