Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # A Front-End to mcrypt and mdecrypt.
- # Store the executable script as xcrr and make a symbolic link
- # to the same file as xcrrd.
- # xcrr will encrypt, xcrrd will decrypt.
- # ©2016 Michael Uplawski <michael.uplawski@uplawski.eu>
- # Use at your own risk, modify as you please.
- # set your preferred crypto- and hash algorithm. Verify,
- # that they are supported by mcrypt
- DEF_ALGO='rijndael-256'
- DEF_HASH='sha256'
- # Some arrays will facilitate adaptations to the current
- # version of mcrypt
- declare -a FILES
- declare -a HASHES
- declare -a ALGOS
- # get the supported cryptographic algorithms
- ALGOS=($(mcrypt --list|cut -d\( -f1|tr -d "[:cntrl:]"))
- # get the supported hash algorithms
- HASHES=($(mcrypt --list-hash|cut -d':' -f2|tr -d "[:blank:]"|tr "\n" " "))
- # set the names of the supported modes in a string
- # variable. These values are not yet presented by
- # mcrypt itself. CFB is the default, hence the '^'.
- MODES="ECB ^CFB OFB nOFB CBX STREAM"
- # default action (script xcrr)
- ACTION="encrypt"
- ICON="encrypt.png"
- # modify action do decrypt, if the script is called
- # as xcrrd
- if [ "xcrrd" == `basename "$0"` ]
- then
- ACTION="decrypt"
- ICON="decrypt.png"
- fi
- # find the default algorithm and mark it
- # with '^' for yad
- AL=''
- for i in `seq 0 50`
- do
- if [ ${ALGOS[$i]} != ' ' ]
- then
- a=${ALGOS[$i]}
- if [ "$DEF_ALGO" == $a ]
- then
- a="^$a"
- fi
- AL="$a $AL"
- else
- break
- fi
- done
- # find the default hash and mark it with '^'
- # for yad
- HS=''
- for i in `seq 0 50`
- do
- if [ ${HASHES[$i]} != ' ' ]
- then
- h=${HASHES[$i]}
- if [ "sha256" == $h ]
- then
- h="^$h"
- fi
- HS="$h $HS"
- else
- break
- fi
- done
- # call yad and present the user-interface
- FILES=($(yad --title "$ACTION" --image="$ICON" --window-icon="$ICON" --form --separator=' ' --field="File to $ACTION":FL " " --field="Key file":FL ' ' --field="Algorithm":CB "$AL" --field="Mode":CB "$MODES" --field="Hash":CB "$HS" --item-separator=' ' ))
- # extrct the user-provided values
- FILE=${FILES[0]}
- KFILE=${FILES[1]}
- ALGO=${FILES[2]}
- MODE=${FILES[3]}
- HASH=${FILES[4]}
- # verifications and error-handling
- if [ ! -e "$FILE" ]
- then
- yad --image "dialog-error" --title "ERROR" --text="File $FILE does not exist"
- exit 1
- fi
- if [ ! -f "$FILE" ]
- then
- yad --image "dialog-error" --title "ERROR" --text="$FILE is not a valid file for $ACTIONion! Aborting."
- exit 0
- fi
- if [ ! -e "$KFILE" ]
- then
- yad --image "dialog-error" --title "ERROR" --text="Key-file $KFILE does not exist"
- exit 1
- fi
- if [ ! -f "$KFILE" ]
- then
- yad --image "dialog-error" --title "ERROR" --text="$KFILE is not a valid key-file! Aborting."
- exit 0
- fi
- # Do it.
- # Remove pre-existing files if we are about to replace them anyway.
- if [ "encrypt" == "$ACTION" ]
- then
- if [ -e "$FILE".nc ]
- then
- rm -f "$FILE".nc
- fi
- mcrypt -a "$ALGO" -h "$HASH" -m "$MODE" -f "$KFILE" "$FILE"
- if [ -e "$FILE".nc ]
- then
- yad --image "dialog-information" --title "result" --text="encrypted file $FILE.nc has been written"
- else
- yad --image "dialog-warning" --title "result" --text="An error occured, check console-output"
- fi
- elif [ "decrypt" == "$ACTION" ]
- then
- dir=`dirname $FILE`
- out=$dir/`basename "$FILE" .nc`
- if [ -e "$out" ]
- then
- rm -f "$out"
- fi
- mdecrypt -a "$ALGO" -h "$HASH" -m "$MODE" -f "$KFILE" "$FILE"
- if [ -e "$out" ]
- then
- yad --image "dialog-information" --title "result" --text="File $out has been written"
- fi
- fi
- # EOF
Add Comment
Please, Sign In to add comment