Guest User

YAD (ex zenity) frontend to mcrypt and mdecrypt

a guest
Feb 8th, 2016
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.46 KB | None | 0 0
  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.  
  37. # modify action do decrypt, if the script is called
  38. # as xcrrd
  39. if [ "xcrrd" == `basename "$0"` ]
  40. then
  41.     ACTION="decrypt"
  42. fi
  43.  
  44. # find the default algorithm and mark it
  45. # with '^' for yad
  46. AL=''
  47. for i in `seq 0 50`
  48. do
  49.     if [ ${ALGOS[$i]} != ' ' ]
  50.     then
  51.         a=${ALGOS[$i]}
  52.         if [ "$DEF_ALGO" == $a ]
  53.         then
  54.             a="^$a"
  55.         fi
  56.         AL="$a $AL"
  57.     else
  58.         break
  59.     fi
  60. done
  61.  
  62. # find the default hash and mark it with '^'
  63. # for yad
  64. HS=''
  65. for i in `seq 0 50`
  66. do
  67.     if [ ${HASHES[$i]} != ' ' ]
  68.     then
  69.         h=${HASHES[$i]}
  70.         if [ "sha256" == $h ]
  71.         then
  72.             h="^$h"
  73.         fi
  74.         HS="$h $HS"
  75.     else
  76.         break
  77.     fi
  78. done
  79.  
  80. # call yad and present the user-interface
  81. FILES=($(yad --title "$ACTION" --image="/usr/share/pixmaps/decrypt.png" --window-icon="/usr/share/pixmaps/decrypt.png" --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=' ' ))
  82.  
  83. # extrct the user-provided values
  84. FILE=${FILES[0]}
  85. KFILE=${FILES[1]}
  86. ALGO=${FILES[2]}
  87. MODE=${FILES[3]}
  88. HASH=${FILES[4]}
  89.  
  90. # verifications and error-handling
  91. if [ ! -e "$FILE" ]
  92. then
  93.     yad --image "dialog-error" --title "ERROR" --text="File $FILE does not exist"
  94.     exit 1
  95. fi
  96.  
  97. if [ ! -f "$FILE" ]
  98. then
  99.     yad --image "dialog-error" --title "ERROR" --text="$FILE is not a valid file for $ACTIONion! Aborting."
  100.     exit 0
  101. fi
  102.  
  103. if [ ! -e "$KFILE" ]
  104. then
  105.     yad --image "dialog-error"  --title "ERROR" --text="Key-file $KFILE does not exist"
  106.     exit 1
  107. fi
  108.  
  109. if [ ! -f "$KFILE" ]
  110. then
  111.     yad --image "dialog-error" --title "ERROR" --text="$KFILE is not a valid key-file! Aborting."
  112.     exit 0
  113. fi
  114.  
  115. # Do it.
  116. # Remove pre-existing files if we are about to replace them anyway.
  117.  
  118. if [ "encrypt" == "$ACTION" ]
  119. then
  120.     if [ -e "$FILE".nc ]
  121.     then
  122.         rm -f "$FILE".nc
  123.     fi
  124.  
  125.     mcrypt -a "$ALGO" -h "$HASH" -m "$MODE" -f "$KFILE" "$FILE"
  126.  
  127.     if [ -e "$FILE".nc ]
  128.     then
  129.         yad --image "dialog-information" --title "result" --text="encrypted file $FILE.nc has been written"
  130.     else
  131.         yad --image "dialog-warning"  --title "result" --text="An error occured, check console-output"
  132.     fi
  133. elif [ "decrypt" == "$ACTION" ]
  134. then
  135.     dir=`dirname $FILE`
  136.     out=$dir/`basename "$FILE" .nc`
  137.    
  138.     if [ -e "$out" ]
  139.     then
  140.         rm -f "$out"
  141.     fi
  142.     mdecrypt -a "$ALGO" -h "$HASH" -m "$MODE" -f "$KFILE" "$FILE"
  143.  
  144.     if [ -e "$out" ]
  145.     then
  146.         yad --image "dialog-information" --title "result" --text="File $out has been written"
  147.     fi
  148. fi
  149.  
  150. # EOF
Add Comment
Please, Sign In to add comment