quickencrypt 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/bash
  2. #
  3. # Quick encryption app. Given a roster of encryption public keys, select them
  4. # and paste some text to encrypt to them.
  5. #
  6. # See LICENSE for more information on how you can use this program.
  7. #
  8. encrypt() {
  9. # takes string arguments "cleartext", "recipient"
  10. # returns string PGP-encrypted data.
  11. echo "$1" | gpg --armor -r "$2" --encrypt
  12. }
  13. decrypt() {
  14. # takes string argument "ciphertext", "key"
  15. # returns string of attempted decrypted data
  16. # write ciphertext to a temporary file:
  17. echo "$1" > .quickencrypttempfile
  18. # --batch option does magic!
  19. echo "$2" | gpg --passphrase-fd 0 --batch --decrypt .quickencrypttempfile ||
  20. zenity --error --text "Could not decrypt message"
  21. rm .quickencrypttempfile
  22. }
  23. # List all public keys available in the system:
  24. KEYS=$(gpg --list-keys |
  25. cut -d ">" -f 1 |
  26. grep "<" |
  27. sed "s/.*<//g" |
  28. tr "\n" "|")
  29. form_data=$(zenity --forms \
  30. --title "quickencrypt by kzimmermann" \
  31. --text="Enter some text to encrypt. WARNING: does not sign the data!" \
  32. --add-entry="Message" \
  33. --add-combo="Key to encrypt to:" \
  34. --combo-values="$KEYS" \
  35. --add-list="Encrypt or decrypt?" \
  36. --list-values="Encrypt|Decrypt"
  37. )
  38. [[ $? -ne 0 ]] && exit 1
  39. text=$(echo "$form_data" | cut -d "|" -f 1)
  40. recipient=$(echo "$form_data" | tr "\n" "," | cut -d "|" -f 2 | sed "s/\,//g")
  41. action=$(echo "$form_data" | tr "\n" "," | sed "s/\,//g" | cut -d "|" -f 3) || ERROR=2
  42. case $ERROR in
  43. 1 )
  44. zenity --error \
  45. --text="Sorry, $recipient's public key is unusable (trust issue?)"
  46. exit 1
  47. ;;
  48. 2 )
  49. zenity --error \
  50. --text="Please choose an action (encrypt or decrypt) from the menu"
  51. exit 1
  52. ;;
  53. * )
  54. # Ok, got it!
  55. ;;
  56. esac
  57. case ${action} in
  58. "Encrypt" )
  59. encrypted=$(encrypt "$text" "$recipient")
  60. echo "$encrypted" |
  61. zenity --text-info \
  62. --width 700 \
  63. --height 400 \
  64. --title "Encrypted message to $recipient. Press OK to copy."
  65. [[ $? -ne 0 ]] && exit
  66. if [[ -n $(which xsel) ]]
  67. then
  68. echo "$encrypted" | xsel -i
  69. fi
  70. ;;
  71. "Decrypt" )
  72. passwd=$(zenity --password \
  73. --title="Attempting to decrypt" \
  74. --text="Enter the password to decrypt this text:"
  75. )
  76. echo $(decrypt "$text" "$passwd") |
  77. zenity --text-info \
  78. --title "Decrypted text output" \
  79. --width 500 --height 300 || ERROR=1
  80. ;;
  81. * )
  82. zenity --error \
  83. --text="Please choose an action (encrypt or decrypt) from the menu"
  84. exit 1
  85. ;;
  86. esac