Oceans11

Cryptographic function, for use in a 'bash_functions' file.

Sep 19th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.76 KB | None | 0 0
  1. ## Slightly updated, by adding some comments.
  2. ## Last time I created this, I was truly just copying and pasting 'a quickie'. :-D
  3. crypto()
  4. {
  5. clear
  6. if [[ -x "$(which openssl)" ]] && [[ -x "$(which gpg)" ]]
  7. then
  8.   if [ -z "$2" ]; then
  9.   echo -e ${Yellow}"\tError: Providing a filename is MANDATORY!"${Nc}
  10.   echo -e ${Blue}"\tPlease choose a valid option!\n${Yellow}\tUsage:"${Nc}
  11.   echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--encrypt"${Nc}","${Cyan}"-e"${Nc}"}\t${Blue} Your-file "${Nc}
  12.   echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--decrypt"${Nc}","${Cyan}"-d"${Nc}"}\t${Blue} Your-file.aes "${Nc}
  13.   echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--sign"${Nc}","${Cyan}"-s"${Nc}"}\t${Blue} Your-file "${Nc}
  14.   echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--verify"${Nc}","${Cyan}"-v"${Nc}"}\t${Blue} Your-file.asc "${Nc}
  15.   elif [ -d "$2" ]; then
  16.   echo -e ${Yellow}"\tError: File is a directory!"${Nc}
  17.   else
  18.   case "$1" in
  19.   # head -c 48 /dev/urandom | openssl enc -base64 # <- For better entropy.
  20.   ## All are alternatives to the one above.
  21.   # echo "Password"| openssl dgst -whirlpool|awk -F '= ' '{print $2}'
  22.   # echo "Password"| openssl dgst -whirlpool|awk -F '= ' '{print $2}'|base64 -w0
  23.   # cat text.txt| openssl dgst -whirlpool|awk -F '= ' '{print $2}'
  24.   ## Default cipher.
  25.   # aes-256-cbc
  26.   "--encrypt" | "-e")
  27.   ## Create a good strong pass{phrase,word} and store it in a 'hidden' file.
  28.   ## The goal would be to pass along your message over one channel. E.g.: email
  29.   ## And your $passphrase over another. E.g.: text
  30.   #
  31.   ## Or use openpgp http://www.openpgp.org/
  32.   ## Also take a look here: http://ubuntuforums.org/showthread.php?t=939545
  33.   head -c 48 /dev/urandom | openssl enc -base64 > ."${2%.*}".txt
  34.   ## Define it, so openssl knows where to look.
  35.   passphrase="file://"$PWD"/."${2%.*}".txt"
  36.   ## Now encrypt your file/any file with the aforementioned $passphrase.
  37.   ## Its output will be a 'readable' text file.
  38.   #
  39.   ## Here I use the camellia cipher, but you are free to choose another of course!
  40.   ## http://www.madboa.com/geek/openssl/
  41.   openssl camellia-256-cbc -a -e -salt -in "$2" -out "$2".aes -pass "$passphrase"
  42.   ## Also have a look at 'ccrypt' : http://ccrypt.sourceforge.net/
  43.   ## Make it 'read only', sending it to someone over mail, should fix the perms.
  44.   chmod 400 "$2".aes
  45.   ## This file should only have +rw perms for the owner.
  46.   ## PS: Do NOT rename this file!
  47.   chmod 600 "${passphrase/file:\/\//}"
  48.   ## Optional!
  49.   ## Just remember, if you lose your $passphrase, then it's GONE 4ever!
  50.   \rm "$2"
  51.   ## There's no need to 'export' or to make it available to your env.
  52.   unset passphrase
  53.   ;;
  54.   "--decrypt" | "-d")
  55.   pfile=""$PWD"/."${2%.*.aes}".txt"
  56.   if [[ -e "$pfile" ]];then
  57.   passphrase="file://"$PWD"/."${2%.*.aes}".txt"
  58.   openssl camellia-256-cbc -a -d -salt -in "$2" -out "${2%.aes}" -pass "$passphrase"
  59.   chmod 600 "${2%.aes}"
  60.   unset passphrase
  61.   else
  62.   echo -e ${Yellow}"\tYou sure you got the right key?"${Nc}
  63.   fi
  64.   ;;
  65.   "--sign" | "-s")
  66.   ## to sign (-s):
  67.   echo -e ${Cyan}"\tgpg -a --detach-sign \""$2"\""${Nc}
  68.   gpg -a --detach-sign "$2"
  69.   ;;
  70.   "--verify" | "-v")
  71.   ## to verify (-v):
  72.   echo -e ${Cyan}"\tgpg --verify \""$2"\" \"${2%.asc}\""${Nc}
  73.   gpg --verify "$2" "${2%.asc}"
  74.   ;;
  75.   *)
  76.   echo -e ${Blue}"\tPlease choose a valid option!\n${Yellow}\tUsage:"${Nc}
  77.   echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--encrypt"${Nc}","${Cyan}"-e"${Nc}"}\t${Blue} Your-file "${Nc}
  78.   echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--decrypt"${Nc}","${Cyan}"-d"${Nc}"}\t${Blue} Your-file.aes "${Nc}
  79.   echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--sign"${Nc}","${Cyan}"-s"${Nc}"}\t${Blue} Your-file "${Nc}
  80.   echo -e ${Cyan}"\tcrypto"${Nc}" {"${Cyan}"--verify"${Nc}","${Cyan}"-v"${Nc}"}\t${Blue} Your-file.asc "${Nc}
  81.   ;;
  82.   esac
  83.   fi
  84. fi
  85. }
Add Comment
Please, Sign In to add comment