symcrypt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  2. #
  3. # Graphical utility to produce symmetric encryption using openssl's aes-256
  4. # cipher.
  5. #
  6. # Does not sign the message or produce public-key encryption. You've been
  7. # warned!
  8. #
  9. # Copyright 2016-2017 - Klaus Zimmermann <https://quitter.se/kzimmermann>
  10. #
  11. # This program is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. #
  24. crash() {
  25. # shorthand function that displays an error message and quits
  26. # $1 is the message to be displayed.
  27. echo "Error: $1"
  28. exit 1
  29. }
  30. encrypt() {
  31. # encrypts a message using openssl's aes-256-cbc cipher
  32. # arguments: MESSAGE, KEY
  33. tmp=$(echo "$1" | openssl aes-256-cbc -a -salt -pass pass:"$2")
  34. echo "$tmp" | sed "s/$\n//g"
  35. }
  36. decrypt() {
  37. # decrypts a message encrypted with the encrypt() function
  38. # arguments: MESSAGE, KEY
  39. echo "$1" | openssl aes-256-cbc -d -a -salt -pass pass:"$2" ||
  40. crash "decryption failed."
  41. }
  42. which openssl > /dev/null || crash "OpenSSL not found!"
  43. which zenity > /dev/null || crash "zenity not found!"
  44. while [[ true ]]
  45. do
  46. form_data=$(zenity --forms \
  47. --title "Simmetric Encryption Applet" \
  48. --text="Enter some text to encrypt and the session password" \
  49. --add-entry="Message" \
  50. --add-password="Session key" \
  51. --add-combo="Action:" \
  52. --combo-values="encrypt|decrypt" \
  53. ) || exit 0
  54. text=$(echo "$form_data" | cut -d "|" -f 1)
  55. key=$(echo "$form_data" | cut -d "|" -f 2)
  56. action=$(echo "$form_data" | cut -d "|" -f 3)
  57. case "$action" in
  58. "encrypt" )
  59. echo "$(encrypt "$text" "$key")"
  60. echo "$(encrypt "$text" "$key")" |
  61. zenity --text-info \
  62. --width 700 \
  63. --height 400 \
  64. --title "Encrypted session message"
  65. ;;
  66. "decrypt" )
  67. echo "$(decrypt "$text" "$key")" |
  68. zenity --text-info \
  69. --width 700 \
  70. --height 400 \
  71. --title "Decrypted session message"
  72. ;;
  73. * )
  74. echo "Please choose an action from the menu."
  75. ;;
  76. esac
  77. done