Guest User

YAD (ex zenity) front-end to mcrypt and mdecrypt

a guest
Feb 8th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # A Front-End to mcrypt and mdecrypt.
  4. # Store the executable script as xcrr and make a symbolic link
  5. # to the same file as xcrrd.
  6. # xcrr will encrypt, xcrrd will decrypt.
  7.  
  8. # ©2016 Michael Uplawski <michael.uplawski@uplawski.eu>
  9. # Use at your own risk, modify as you please.
  10.  
  11. # set your preferred crypto- and hash algorithm. Verify,
  12. # that they are supported by mcrypt
  13. DEF_ALGO='rijndael-256'
  14. DEF_HASH='sha256'
  15.  
  16. # Some arrays will facilitate adaptations to the current
  17. # version of mcrypt
  18.  
  19. declare -a FILES
  20. declare -a HASHES
  21. declare -a ALGOS
  22.  
  23. # get the supported cryptographic algorithms
  24. ALGOS=($(mcrypt --list|cut -d\( -f1|tr -d "[:cntrl:]"))
  25.  
  26. # get the supported hash algorithms
  27. HASHES=($(mcrypt --list-hash|cut -d':' -f2|tr -d "[:blank:]"|tr "\n" " "))
  28.  
  29. # set the names of the supported modes in a string
  30. # variable. These values are not yet presented by
  31. # mcrypt itself. CFB is the default, hence the '^'.
  32. MODES="ECB ^CFB OFB nOFB CBX STREAM"
  33.  
  34. # default action (script xcrr)
  35. ACTION="encrypt"
  36. ICON="encrypt.png"
  37.  
  38. # modify action do decrypt, if the script is called
  39. # as xcrrd
  40. if [ "xcrrd" == `basename "$0"` ]
  41. then
  42.     ACTION="decrypt"
  43.     ICON="decrypt.png"
  44. fi
  45.  
  46. # find the default algorithm and mark it
  47. # with '^' for yad
  48. AL=''
  49. for i in `seq 0 50`
  50. do
  51.     if [ ${ALGOS[$i]} != ' ' ]
  52.     then
  53.         a=${ALGOS[$i]}
  54.         if [ "$DEF_ALGO" == $a ]
  55.         then
  56.             a="^$a"
  57.         fi
  58.         AL="$a $AL"
  59.     else
  60.         break
  61.     fi
  62. done
  63.  
  64. # find the default hash and mark it with '^'
  65. # for yad
  66. HS=''
  67. for i in `seq 0 50`
  68. do
  69.     if [ ${HASHES[$i]} != ' ' ]
  70.     then
  71.         h=${HASHES[$i]}
  72.         if [ "sha256" == $h ]
  73.         then
  74.             h="^$h"
  75.         fi
  76.         HS="$h $HS"
  77.     else
  78.         break
  79.     fi
  80. done
  81.  
  82. # call yad and present the user-interface
  83. 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=' ' ))
  84.  
  85. # extrct the user-provided values
  86. FILE=${FILES[0]}
  87. KFILE=${FILES[1]}
  88. ALGO=${FILES[2]}
  89. MODE=${FILES[3]}
  90. HASH=${FILES[4]}
  91.  
  92. # verifications and error-handling
  93. if [ ! -e "$FILE" ]
  94. then
  95.     yad --image "dialog-error" --title "ERROR" --text="File $FILE does not exist"
  96.     exit 1
  97. fi
  98.  
  99. if [ ! -f "$FILE" ]
  100. then
  101.     yad --image "dialog-error" --title "ERROR" --text="$FILE is not a valid file for $ACTIONion! Aborting."
  102.     exit 0
  103. fi
  104.  
  105. if [ ! -e "$KFILE" ]
  106. then
  107.     yad --image "dialog-error"  --title "ERROR" --text="Key-file $KFILE does not exist"
  108.     exit 1
  109. fi
  110.  
  111. if [ ! -f "$KFILE" ]
  112. then
  113.     yad --image "dialog-error" --title "ERROR" --text="$KFILE is not a valid key-file! Aborting."
  114.     exit 0
  115. fi
  116.  
  117. # Do it.
  118. # Remove pre-existing files if we are about to replace them anyway.
  119.  
  120. if [ "encrypt" == "$ACTION" ]
  121. then
  122.     if [ -e "$FILE".nc ]
  123.     then
  124.         rm -f "$FILE".nc
  125.     fi
  126.  
  127.     mcrypt -a "$ALGO" -h "$HASH" -m "$MODE" -f "$KFILE" "$FILE"
  128.  
  129.     if [ -e "$FILE".nc ]
  130.     then
  131.         yad --image "dialog-information" --title "result" --text="encrypted file $FILE.nc has been written"
  132.     else
  133.         yad --image "dialog-warning"  --title "result" --text="An error occured, check console-output"
  134.     fi
  135. elif [ "decrypt" == "$ACTION" ]
  136. then
  137.     dir=`dirname $FILE`
  138.     out=$dir/`basename "$FILE" .nc`
  139.    
  140.     if [ -e "$out" ]
  141.     then
  142.         rm -f "$out"
  143.     fi
  144.     mdecrypt -a "$ALGO" -h "$HASH" -m "$MODE" -f "$KFILE" "$FILE"
  145.  
  146.     if [ -e "$out" ]
  147.     then
  148.         yad --image "dialog-information" --title "result" --text="File $out has been written"
  149.     fi
  150. fi
  151.  
  152. # EOF
Add Comment
Please, Sign In to add comment