diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0bdfd49 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +keys diff --git a/README.md b/README.md index b79e12b..a2e2db4 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file diff --git a/crypter.sh b/crypter.sh index 9a1e023..9978fac 100755 --- a/crypter.sh +++ b/crypter.sh @@ -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