Unbin

Unbin is a small program made of several bash and perl scripts.

The purpose of Unbin is to convert text and files to and from binary, hexadecimal, and reverse.

It can convert any file or string of text, numbers, special characters, or whatever.

History Originally created in 2008, Unbin is a small set of scripts (bash and perl) to convert text to and from binary, hex, and reverse.

Version 0.1 included only one file, and worked only from the command line. It could only convert to and from binary.

Version 0.5, written in 2009, introduced a graphical interface, using zenity. It still was only able to do binary.

Version 1.0, written in late 2009, further expanded the graphical interface, introducing an option to convert a file. This version was the first to do hex conversion. It was also the first version to introduce flags, although they were only functional on the command line.

Version 1.5, written in 2010, further expanded use of flags, making them available on the gui as well, and thoroughly separating the graphical interface from the command line interface. As of version 1.5, either interface can be summoned individually.

Version 2.0, written in early 2012, includes a reverse converter. It will reverse any string of text, numbers, or whatever. A help file and a man page were introduced.

Version 2.5, written in mid 2012, also gives the user the option to save the resulting file and encrypt it, using gnupg.
Source Code:
These are the individual scripts that make Unbin work.

Only the scripts that are strictly relevant to the program have been included.

There's an easter egg, but I won't reveal it :)
Unbin:
#!/bin/bash


### Get initial parameters

## help

if [ $1 = "-help" ]
 then
  echo
  echo
  echo "Unbin v 2.5, by Jon Tohrs "
  echo
  echo "Released under the terms of the GNU General Public License Version 3, or any later version published by the Free Software Foundation."
  echo
  echo "Usage:"
  echo "Unbin [-parameter]"
  echo "Unbin [-parameter] [text]"
  echo "Unbin [-parameter] [-f] [file]"
  echo
  echo "Parameters:"
  echo "-b   convert text to binary"
  echo "-bt  convert binary to text"
  echo "-h   convert text to hex"
  echo "-ht  convert hex to text"
  echo "-r   reverse a string of text"
  echo "-rt  return reversed text back to normal"
  echo "-f   name of the file to convert"
  echo "-nogui  run in terminal"
  echo "-l   prints a brief description of the license."
  echo
  echo "see man Unbin for further help"
  echo
  exit 0
fi

## terminal

if [ $1 = "-nogui" ]
 then
 clear
### new section so we can also choose bin/hex via -nogui
### i really couldn't figure out a way to do this within this same file
### so i just took that extra code and put it into two different files
### one for hex and one for binary.  if i ever get good at this i'll 
### combine it all into this single file, again.

read -p "Choose encoding type: Bin=\"b\", Hex=\"h\", Rev=\"r\"."
 parameter=$REPLY
 case $parameter in
 "b") parameter="Unbin-no-gui-bin";;
 "h") parameter="Unbin-no-gui-hex";;
 "r") parameter="Unbin-no-gui-rev";;
 *) echo "Invalid option.  Exiting...";;
 esac
 
exec /usr/bin/unbin-scripts/$parameter
exit 0
fi

## terminal, with predefined parameters
## reads input after Unbin is summoned, if any, and acts accordingly.
## this section gets parameters for /usr/bin/conv.pl, which is the binary encoder.

if [ $1 = "-b" ]
 then
  if [ $2 = "-f" ]
   then
    if [ $3 > /dev/null ]
     then
     clear
     /usr/bin/unbin-scripts/conv.pl $1 $2 $3
     exit 0
    else
     clear
     echo
     echo "Enter the name of the file to Bin"
     echo
     read -p "File:"
     echo
     /usr/bin/unbin-scripts/conv.pl $1 $2 $REPLY
     exit 0
    fi
  else
   if [ $2 > /dev/null ]
   then /usr/bin/unbin-scripts/conv.pl $1 $2
   exit 0
   else
   clear
   echo
   echo "Enter the message to Bin"
   echo
   read -p "Message:"
   echo
   /usr/bin/unbin-scripts/conv.pl $1 "$REPLY"
   exit 0
   fi
  fi
fi

## unbin from terminal with predefined options

if [ $1 = "-bt" ]
 then
  if [ $2 = "-f" ]
   then
    if [ $3 > /dev/null ]
     then
     clear
     /usr/bin/unbin-scripts/conv.pl -t $2 $3
     exit 0
    else
     clear
     echo
     echo "Enter the name of the file to Unbin"
     echo
     read -p "File:"
     echo
     /usr/bin/unbin-scripts/conv.pl -t $2 $REPLY
     exit 0
    fi
  else
   if [ $2 > /dev/null ]
   then /usr/bin/unbin-scripts/conv.pl -t $2
   exit 0
   else
   clear
   echo
   echo "Enter the message to Unbin"
   echo
   read -p "Message:"
   echo
   /usr/bin/unbin-scripts/conv.pl -t "$REPLY"
   exit 0
   fi
  fi
fi

## with predefined hex
## this next section will do the same as the section above
## the difference is that this section gathers parameters for /usr/bin/hconv.pl
## which is the hex encoder


if [ $1 = "-h" ]
 then
  if [ $2 = "-f" ]
   then
    if [ $3 > /dev/null ]
     then
     clear
     /usr/bin/unbin-scripts/hconv.pl $1 $2 $3
     exit 0
    else
     clear
     echo
     echo "Enter the name of the file to Hex"
     echo
     read -p "File:"
     echo
     /usr/bin/unbin-scripts/hconv.pl $1 $2 $REPLY
     exit 0
    fi
  else
   if [ $2 > /dev/null ]
   then //usr/bin/unbin-scripts/hconv.pl $1 $2
   exit 0
   else
   clear
   echo
   echo "Enter the message to Hex"
   echo
   read -p "Message:"
   echo
   /usr/bin/unbin-scripts/hconv.pl $1 "$REPLY"
   exit 0
   fi
  fi
fi


## unhex from terminal with predefined options

if [ $1 = "-ht" ]
 then
  if [ $2 = "-f" ]
   then
    if [ $3 > /dev/null ]
     then
     clear
     /usr/bin/unbin-scripts/hconv.pl -t $2 $3
     exit 0
    else
     clear
     echo
     echo "Enter the name of the file to Unhex"
     echo
     read -p "File:"
     echo
     /usr/bin/unbin-scripts/hconv.pl -t $2 $REPLY
     exit 0
    fi
  else
   if [ $2 > /dev/null ]
   then /usr/bin/unbin-scripts/hconv.pl -t $2
   exit 0
   else
   clear
   echo
   echo "Enter the message to Unhex"
   echo
   read -p "Message:"
   echo
   /usr/bin/unbin-scripts/hconv.pl -t "$REPLY"
   exit 0
   fi
  fi
fi


## next is a brief description of the license.

if [ $1 = "-l" ]
 then
  echo
  echo
  echo "Unbin is free software distributed under the terms of the GNU General Public License, version 3.  This means you have four basic freedoms:"
  echo
  echo " Freedom to use the program in any way you see fit."
  echo " Freedom to study and modify the source code."
  echo " Freedom to make backup copies."
  echo " Freedom to redistribute it."
  echo
  echo "The GPL is the legal mechanism that gives you these freedoms.  It also protects you from having them taken away: any derivative work based on the program must be under GPLv3 as well."
  echo
  echo "see /usr/share/Unbin/docs/gpl-3.0.txt for full details."
  echo
  echo
  echo "To view, study, or modify the code, go to /usr/bin/unbin-files and look at the files: Unbin-no-gui-bin, Unbin-no-gui-hex, Unbin-no-gui-rev, conv.pl,  conv.sh, and hconv.pl.  Also main executable /usr/bin/Unbin."
  exit 0
 fi


## new section for reverse text.

if [ $1 = "-r" ]
 then
  if [ $2 = "-f" ]
   then
    if [ $3 > /dev/null ]
     then
     clear
     rev $3
     exit 0
    else
     clear
     echo
     echo "Enter the name of the file to Hex"
     echo
     read -p "File:"
     echo
     rev $REPLY
     exit 0
    fi
  else
   if [ $2 > /dev/null ]
   then
       echo $2 > /tmp/reverse.txt
       rev /tmp/reverse.txt   
   exit 0
   else
   clear
   echo
   echo "Enter the message to Rev"
   echo
   read -p "Message:"
   echo
   echo "$REPLY" > /tmp/reverse.txt
   rev /tmp/reverse.txt
   exit 0
   fi
  fi
fi

if [ $1 = "-rt" ]
 then
  if [ $2 = "-f" ]
   then
    if [ $3 > /dev/null ]
     then
     clear
     rev $3
     exit 0
    else
     clear
     echo
     echo "Enter the name of the file to Unrev"
     echo
     read -p "File:"
     echo
     rev $REPLY
     exit 0
    fi
  else
   if [ $2 > /dev/null ]
   then
       echo $2 > /tmp/reverse.txt
       rev /tmp/reverse.txt   
   exit 0
   else
   clear
   echo
   echo "Enter the message to Hex"
   echo
   read -p "Message:"
   echo
   echo "$REPLY" > /tmp/reverse.txt
   rev /tmp/reverse.txt
   exit 0
   fi
  fi
fi

## the following code is completely unnecessary, but i like easter eggs, 
## and i won't have it any other way -_-.  that said, it's harmless. 

if [ $1 = "-moo" ]
    then
        if ps ax | grep -v grep | grep eggone > /dev/null
        then
            if ps ax | grep -v grep | grep eggtwo > /dev/null
            then
                if ps ax | grep -v grep | grep eggthree > /dev/null
                    then
                        if ps ax | grep -v grep | grep eggfour > /dev/null
                            then
                                if ps ax | grep -v grep | grep /usr/bin/unbin-scripts/easter/bunny > /dev/null
                                    then
                                        /usr/bin/unbin-scripts/easter/wolf
                                        exit 0
                                    else
                                        /usr/bin/unbin-scripts/easter/bunny
                                        exit 0
                                fi
                        else
                        /usr/bin/unbin-scripts/easter/eggfour
                        exit 0
                        fi
                    else
                    /usr/bin/unbin-scripts/easter/eggthree
                    exit 0
                fi
            else
            /usr/bin/unbin-scripts/easter/eggtwo
            exit 0
            fi
        else            
        /usr/bin/unbin-scripts/easter/eggone
        exit 0
        fi
fi

### Otherwise gui
## if no parameters are given, it will summon /usr/bin/conv.sh

clear

/usr/bin/unbin-scripts/conv.sh &


exit 0


conv.sh
#!/bin/bash

## This is the script that Unbin will summon if no further parameters are given.
## It uses zenity to read input from user, and to display output.
## The downside to using zenity is that it disappears right after reading input
## and reappears again to display the output, so there are windows going in and out
## of the screen constantly, making it slower.
## 
## Pending:  future version should use GTK instead of zenity, and have everything done 
## in a single window, where all the options are presented via buttons or context menus.
## Also planning to include an octal encoder/decoder.
## If I do succeed in porting the program to GTK, I'll reset the version numbering
## and call it version 1.0.

## enough babbling.  on with the code.

## Note how it's possible to cancel after every question.

### Determine encoding type.

choice=$(zenity --list --window-icon=/usr/share/pixmaps/Unbin.tga --title "Unbin" --width 200 --height 200  --text "Choose encoding type"\
  --radiolist \
  --column "Choice" --column "Value" \
  ONE Binary \
  TWO Hexadecimal \
  THREE Reverse \

)

if $choice
 then
 exit 0
fi

## this next section is what will happen if user selects option ONE -- bin

if [ $choice = "Binary" ]

then

## the user is presented with a list of options.  these options correspond to the same
## parameters that would have been used on the command line.

## each item from here on is pretty much self-explanatory.  the options presented in zenity are
## plain and simple.  everything that zenity reads from input is piped into a variable.

action=$(zenity --list --window-icon=/usr/share/pixmaps/Unbin.tga --title "Unbin" --width 300 --height 230  --text "Choose desired action"\
  --radiolist \
  --column "Choice" --column "Value" --column "Meaning" \
  ONE b "Bin" \
  TWO t "Unbin" \
  THREE u "Unbinfile" \
  FOUR f "Binfile" \
)
if $action
 then
 exit 0
fi

## Find file, if needed

if [ $action = "f" ]
 then
 file=$(zenity --file-selection --title="Choose File" --window-icon=/usr/share/pixmaps/Unbin.tga)
 if $file
     then
     exit 0
 fi
 /usr/bin/unbin-scripts/conv.pl -b -f $file > /tmp/unbin.txt
 zenity --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --width 300 --height 50 --filename=/tmp/unbin.txt

## added this whole new section to give the user an option to save and encrypt resulting file, with gpg passphrase.
## first we ask the user if he/she wants to save this stuff.  if no, exit.

     if ! $(zenity --question --no-wrap --window-icon=/usr/share/pixmaps/Unbin.tga --title "Unbin" --text "Would you like to save
\n the new file?") ; then
     exit 0
         else

## if the user does want to save, we ask for a new filename, to avoid overwriting $file.  if no name is given, exit.

        name=$(zenity --entry --window-icon=/usr/share/pixmaps/Unbin.tga --title="Unbin" --text "Enter name for new file.")

            if $name
               then
               exit 0
            fi

## now the user is presented with a gpg encryption dialog.  again, a negative, will exit the program.

     if $(zenity --question --no-wrap --window-icon=/usr/share/pixmaps/Unbin.tga --title "Unbin" --text "Would you like to add
\n GPG encryption") ; then

## if the user chooses to encrypt file, Unbin will ask for a passphrase to protect it.  It will be necessary in order to decrypt file.
## if no passphrase is given, exit.

     pass=$(zenity --entry --window-icon=/usr/share/pixmaps/Unbin.tga --title="Unbin" --text "Enter new passphrase." --hide-text)

            if $pass
                  then
                  exit 0
            fi

## new file is saved as $HOME/$name.gpg.  the name is the same as the original file, and is in the user's home directory.
            cat /tmp/unbin.txt > $HOME/$name
            gpg -c --passphrase $pass $HOME/$name
            rm $HOME/$name
            zenity --info --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text="File has been saved to home directory."
            exit 0
     else
     
## if the user dismisses encryption.  Unbin will simply pipe output to a text file in $HOME, and save it as such.
     
            cat /tmp/unbin.txt > $HOME/$name
            zenity --info --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text="File has been saved to home directory."
            exit 0
          fi
        fi
fi

###  this is just a guideline.  code is getting long and i was getting lost.  this separates option "f" from option "u"

if [ $action = "u" ]
 then
 
## look for the file.  if no file is given, exit.
 
 file=$(zenity --file-selection --title="Choose File" --window-icon=/usr/share/pixmaps/Unbin.tga)
     if $file
     then
         exit 0
      fi
      
## if the file name contains gpg in it, Unbin will ask for gpg passphrase.  if no passphrase is given.  it will exit.

     if ls -l $file | grep "gpg" > /dev/null
      then
         pass=$(zenity --entry --window-icon=/usr/share/pixmaps/Unbin.tga --title="Unbin" --text "Encrypted file. \n  Enter passphrase." --hide-text)

                if $pass
                then
                 exit 0
                fi

## we cannot pipe output of gpg directly into /tmp/unbin.txt, so we had to get the output file, which is the same
## as $file, just without .gpg at the end.  thus, nawk.  i suppose awk could do it, too.

             gpg --passphrase $pass $file
             goodname=$(ls $file | nawk '{sub(/.gpg/, "")};1')
             newfile=$(echo $goodname)
           /usr/bin/unbin-scripts/conv.pl -t -f $newfile > /tmp/unbin.txt
             zenity --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --width 300 --height 50 --filename=/tmp/unbin.txt
             rm $newfile
             exit 0

## next is what happens if "gpg" is not in $file.
             
     else
          
         /usr/bin/unbin-scripts/conv.pl -t -f $file > /tmp/unbin.txt
         zenity --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --width 300 --height 50 --filename=/tmp/unbin.txt
         exit 0

## once the file has been gpg encrypted, character encoding is only stored as metadata.  some text editors are still able to read 
## this, but not all.  it's readable enough for Unbin to pipe it through zenity and display it on the screen, but it cannot
## be saved onto a file.  If saved, many editors won't read it.  So, save option not included.  If vital, information can be
## copied directly from zenity, and pasted onto text editor.

          fi
fi


### Get message
## next, the option to convert a message on the fly.  if no message is given, exit.

message=$(zenity --entry --window-icon=/usr/share/pixmaps/Unbin.tga --title="Unbin" --width 300 --height 50 --text "Enter message to convert.")
 if $message
  then
  exit 0
 fi



/usr/bin/unbin-scripts/conv.pl -$action "$message" > /tmp/unbin.txt
zenity --title="Unbin"  --width 300 --height 20 --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --filename=/tmp/unbin.txt

exit 0

fi

## same options, now for encoding type TWO -- hex.

if [ $choice = "Hexadecimal" ]

then

action=$(zenity --list --window-icon=/usr/share/pixmaps/Unbin.tga --title "Unbin" --width 300 --height 230  --text "Choose desired action"\
  --radiolist \
  --column "Choice" --column "Value" --column "Meaning" \
  ONE h "Hex" \
  TWO t "Unhex" \
  THREE u "Unhexfile" \
  FOUR f "Hexfile" \
)
if $action
 then
 exit 0
fi

## Find file, if needed
## action "f"


if [ $action = "f" ]
 then
 file=$(zenity --file-selection --title="Choose File" --window-icon=/usr/share/pixmaps/Unbin.tga)
  if $file
      then
       exit 0
     fi
 /usr/bin/unbin-scripts/hconv.pl -h -f $file > /tmp/unbin.txt
 zenity --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --width 300 --height 50 --filename=/tmp/unbin.txt


     if ! $(zenity --question --no-wrap --window-icon=/usr/share/pixmaps/Unbin.tga --title "Unbin" --text "Would you like to save
\n the new file?") ; then
     exit 0
         else

## if the user does want to save, we ask for a new filename, to avoid overwriting $file.  if no name is given, exit.

        name=$(zenity --entry --window-icon=/usr/share/pixmaps/Unbin.tga --title="Unbin" --text "Enter name for new file.")

            if $name
               then
               exit 0
            fi

## now the user is presented with a gpg encryption dialog.  again, a negative, will exit the program.

     if $(zenity --question --no-wrap --window-icon=/usr/share/pixmaps/Unbin.tga --title "Unbin" --text "Would you like to add
\n GPG encryption") ; then

## if the user chooses to encrypt file, Unbin will ask for a passphrase to protect it.  It will be necessary in order to decrypt file.
## if no passphrase is given, exit.

     pass=$(zenity --entry --window-icon=/usr/share/pixmaps/Unbin.tga --title="Unbin" --text "Enter new passphrase." --hide-text)

            if $pass
                  then
                  exit 0
            fi

## new file is saved as $HOME/$name.gpg.  the name is the same as the original file, and is in the user's home directory.
            cat /tmp/unbin.txt > $HOME/$name
            gpg -c --passphrase $pass $HOME/$name
            rm $HOME/$name
            zenity --info --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text="File has been saved to home directory."
            exit 0
     else
     
## if the user dismisses encryption.  Unbin will simply pipe output to a text file in $HOME, and save it as such.
     
            cat /tmp/unbin.txt > $HOME/$name
            zenity --info --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text="File has been saved to home directory."
            exit 0
          fi
        fi
## this closes first if for action "f" (if action =f).
fi


## action "u"


if [ $action = "u" ]
 then
 file=$(zenity --file-selection --title="Choose File" --window-icon=/usr/share/pixmaps/Unbin.tga)
  if $file
      then
       exit 0
     fi


     if ls -l $file | grep "gpg" > /dev/null
      then
         pass=$(zenity --entry --window-icon=/usr/share/pixmaps/Unbin.tga --title="Unbin" --text "Encrypted file. \n  Enter passphrase." --hide-text)

                if $pass
                then
                 exit 0
                fi


             gpg --passphrase $pass $file
             goodname=$(ls $file | nawk '{sub(/.gpg/, "")};1')
             newfile=$(echo $goodname)
           /usr/bin/unbin-scripts/hconv.pl -t -f $newfile > /tmp/unbin.txt
             zenity --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --width 300 --height 50 --filename=/tmp/unbin.txt
             rm $newfile
             exit 0

             
     else
          
         /usr/bin/unbin-scripts/conv.pl -t -f $file > /tmp/unbin.txt
         zenity --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --width 300 --height 50 --filename=/tmp/unbin.txt
         exit 0

          fi

fi


### Get message, if no file.

message=$(zenity --entry --window-icon=/usr/share/pixmaps/Unbin.tga --title="Unbin" --width 300 --height 50 --text "Enter message to convert.")
 if $message
  then
  exit 0
 fi

/usr/bin/unbin-scripts/hconv.pl -$action "$message" > /tmp/unbin.txt
zenity --title="Unbin"  --width 300 --height 20 --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --filename=/tmp/unbin.txt

exit 0

fi

## new section added for "rev" option 3. this will reverse entered text.  this is much
## easier, as no external script has to be summoned.  it's all done within bash.

if [ $choice = "Reverse" ]

then

action=$(zenity --list --window-icon=/usr/share/pixmaps/Unbin.tga --title "Unbin" --width 300 --height 230  --text "Choose desired action"\
  --radiolist \
  --column "Choice" --column "Value" --column "Meaning" \
  ONE r "Rev" \
  TWO t "Unrev" \
  THREE u "Unrevfile" \
  FOUR f "Revfile" \
)
if $action
 then
 exit 0
fi


## Find file, if needed


if [ $action = "f" ]
 then
 file=$(zenity --file-selection --title="Choose File" --window-icon=/usr/share/pixmaps/Unbin.tga)
     if $file
         then
         exit 0
     fi
# encode     

 rev $file > /tmp/unbin.txt
 
# display 

 zenity --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --width 300 --height 50 --filename=/tmp/unbin.txt
 
# ask if it will be saved.  exit, if not.
 
    if ! $(zenity --question --no-wrap --window-icon=/usr/share/pixmaps/Unbin.tga --title "Unbin" --text "Would you like to save
\n the new file?") ; then
     exit 0
         else

## get new name.

        name=$(zenity --entry --window-icon=/usr/share/pixmaps/Unbin.tga --title="Unbin" --text "Enter name for new file.")

            if $name
               then
               exit 0
            fi

## ask if gpg

     if $(zenity --question --no-wrap --window-icon=/usr/share/pixmaps/Unbin.tga --title "Unbin" --text "Would you like to add
\n GPG encryption") ; then

## set gpg passphrase

     pass=$(zenity --entry --window-icon=/usr/share/pixmaps/Unbin.tga --title="Unbin" --text "Enter new passphrase." --hide-text)

            if $pass
                  then
                  exit 0
            fi

## new file is saved as $HOME/$name.gpg. 
            cat /tmp/unbin.txt > $HOME/$name
            gpg -c --passphrase $pass $HOME/$name
            rm $HOME/$name
            zenity --info --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text="File has been saved to home directory."
            exit 0
     else
     
## save.  no encryption.
     
            cat /tmp/unbin.txt > $HOME/$name
            zenity --info --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text="File has been saved to home directory."
            exit 0
          fi
        fi
fi


## end action "f" start action "u"



if [ $action = "u" ]
 then
 file=$(zenity --file-selection --title="Choose File" --window-icon=/usr/share/pixmaps/Unbin.tga)
     if $file
         then
         exit 0
     fi
          if ls -l $file | grep "gpg" > /dev/null
          then
              pass=$(zenity --entry --window-icon=/usr/share/pixmaps/Unbin.tga --title="Unbin" --text "Encrypted file. \n  Enter passphrase." --hide-text)
                    if $pass
                        then
                         exit 0
                    fi
                    gpg --passphrase $pass $file
                    goodname=$(ls $file | nawk '{sub(/.gpg/, "")};1')
                    newfile=$(echo $goodname)   
                 rev $newfile > /tmp/unbin.txt
                 zenity --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --width 300 --height 50 --filename=/tmp/unbin.txt
                 rm $newfile
                    exit 0             
           else          
             rev $file > /tmp/unbin.txt
             zenity --title="Unbin" --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --width 300 --height 50 --filename=/tmp/unbin.txt 
              exit 0
            fi
fi


### Get message


message=$(zenity --entry --window-icon=/usr/share/pixmaps/Unbin.tga --title="Unbin" --width 300 --height 50 --text "Enter message to convert.")
 if $message
  then
  exit 0
 fi


if [ $action = "r" ]

then
echo "$message" > /tmp/rev.txt
rev /tmp/rev.txt > /tmp/unbin.txt
zenity --title="Unbin"  --width 300 --height 20 --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --filename=/tmp/unbin.txt

exit 0
fi

if [ $action = "t" ]

then

echo "$message" > /tmp/rev.txt
rev /tmp/rev.txt > /tmp/unbin.txt
zenity --title="Unbin"  --width 300 --height 20 --window-icon=/usr/share/pixmaps/Unbin.tga --text-info --filename=/tmp/unbin.txt

exit 0
fi

fi


conv.pl
#!/usr/bin/perl -W

## This, and hconv.pl, are the heart of the program.  Everything that is piped in
## and out of Unbin goes through here.  Code is pretty straight forward.  It uses
## unpack to convert to binary, hex, or text.  If an invalid option is given, it
## prints out an error message.

use strict;
my @in;
my $file;

sub tobinary() {
 my @in;
 my $out;
 my $x;
 @in = @_;
 for($x=0;$x<=$#in;$x++) {
     $out .= unpack("B*", $in[$x]);
 }
 print("$out");
 print("\n");
}

sub totext() {
 my @in;
 my $out;
 my $x;
 @in = @_;
 for($x=0;$x<=$#in;$x++) {
     $out = pack("B*", $in[$x]);
  print($out);
 }
 print("\n");
}

sub help() {
 print("\nUsage: $0 [-b | -t] [-f filename | \"Text to convert\"]\n\n");
 print(" -b        : text to binary conversion\n");
 print(" -t        : binary to text conversion\n");
 print(" filename  : file to be converted\n");
 print(" message   : text to be converted\n");
}

if (@ARGV < 2) { 
 &help();
 exit(1);
}

if ($ARGV[0] eq '-b') {
  if ($ARGV[1] eq '-f') {
     $file = $::ARGV[2];
     open(F, "<$file") || die "Error: unable to open $file - $!\n";

     @in = ;
     close(F);
     if (!@in) {
        print("Error: no data in $file\n");
        exit(1);
     }
  } else {
     @in = $::ARGV[1];
  }
  &tobinary(@in);
}
elsif ($ARGV[0] eq '-t') {
  if ($ARGV[1] eq '-f') {
     $file = $::ARGV[2];
     open(F, "<$file") || die "Error: unable to open $file - $!\n";

     @in = ;
     close(F);
     if (!@in) {
        print("Error: no data in $file\n");
        exit(1);
     }
  } else {
     @in = $::ARGV[1];
  }
  &totext(@in);
}
else {
 &help();
 exit(1);
}



hconv.pl
#!/usr/bin/perl -W

use strict;
my @in;
my $file;

sub tobinary() {
 my @in;
 my $out;
 my $x;
 @in = @_;
 for($x=0;$x<=$#in;$x++) {
     $out .= unpack("H*", $in[$x]);
 }
 print("$out");
 print("\n");
}

sub totext() {
 my @in;
 my $out;
 my $x;
 @in = @_;
 for($x=0;$x<=$#in;$x++) {
     $out = pack("H*", $in[$x]);
  print($out);
 }
 print("\n");
}

sub help() {
 print("\nUsage: $0 [-h | -t] [-f filename | \"Text to convert\"]\n\n");
 print(" -h        : text to binary conversion\n");
 print(" -t        : binary to text conversion\n");
 print(" filename  : file to be converted\n");
 print(" message   : text to be converted\n");
}

if (@ARGV < 2) { 
 &help();
 exit(1);
}

if ($ARGV[0] eq '-h') {
  if ($ARGV[1] eq '-f') {
     $file = $::ARGV[2];
     open(F, "<$file") || die "Error: unable to open $file - $!\n";

     @in = ;
     close(F);
     if (!@in) {
        print("Error: no data in $file\n");
        exit(1);
     }
  } else {
     @in = $::ARGV[1];
  }
  &tobinary(@in);
}
elsif ($ARGV[0] eq '-t') {
  if ($ARGV[1] eq '-f') {
     $file = $::ARGV[2];
     open(F, "<$file") || die "Error: unable to open $file - $!\n";

     @in = ;
     close(F);
     if (!@in) {
        print("Error: no data in $file\n");
        exit(1);
     }
  } else {
     @in = $::ARGV[1];
  }
  &totext(@in);
}
else {
 &help();
 exit(1);
}


Unbin-no-gui-bin
#!/bin/bash
##new file created just so we can separate bin/hex in nogui mode

## determine whether there will be a file to decode/encode

read -p "Enter path to file to encode/decode (leave blank, if none)."
 if [ $REPLY > /dev/null ]
  then file=$REPLY
########################################################################

 if ls $file | grep gpg > /dev/null
  then
   echo "File is encrypted."
   echo "Type passphrase and press enter."
   read -s passphrase
   gpg --passphrase $passphrase $file
   clear
   goodname=$(ls $file | nawk '{sub(/.gpg/, "")};1')
   newfile=$(echo $goodname)
            cat $newfile > /tmp/encrypted.txt
            rm $newfile
            read -p "What do you want to do? View=\"v\", Bin=\"b\", Unbin=\"t\"  : "
            cryptans=$REPLY
            echo $cryptans > /tmp/unbin.txt
            if cat /tmp/unbin.txt | grep "v" > /dev/null
    then
     less /tmp/encrypted.txt
   fi
   if cat /tmp/unbin.txt | grep "b" > /dev/null
    then
     /usr/bin/unbin-scripts/conv.pl -b -f /tmp/encrypted.txt
   fi
   if cat /tmp/unbin.txt | grep "t" > /dev/null
    then
    var=$(cat /tmp/encrypted.txt)
    /usr/bin/unbin-scripts/conv.pl -t $var > /tmp/unbin.txt
    cat /tmp/unbin.txt
   fi
 else

########################################################################  
  read -p "What do you want to do? Bin=\"b\", Unbin=\"t\"  :"
  parameter="$REPLY"
  case $parameter in
   "b") parameter="b";;
   "t") parameter="t";;
   "B") parameter="b";;
   "T") parameter="t";;
   *) echo "Invalid argument.  Exiting...";;
  esac
 /usr/bin/unbin-scripts/conv.pl -$parameter -f $file
############################################################################################################################

read -p "Save? \"y\"/\"n\":  "
    save=$REPLY
    case $save in
    "y") save="y";;
    "n") save="n";;
    *) echo "Invalid argument.  Exiting...";;
    esac
    echo $save > /tmp/unbin.txt
        if cat /tmp/unbin.txt | grep y > /dev/null
            then
            read -p "Name new file:  "
            name=$REPLY
            /usr/bin/unbin-scripts/conv.pl -$parameter -f $file > $HOME/$name
            read -p "Encrypt? \"y\"/\"n\":  "
            encrypt=$REPLY
            case $encrypt in
            "y") encrypt="y";;
            "n") encrypt="n";;
            *) echo "Invalid argument.  Exiting...";;
            esac
            echo $encrypt > /tmp/unbin.txt
                if cat /tmp/unbin.txt | grep y > /dev/null
                    then
                    echo "Enter new gpg passphrase and press enter (pass not displayed)"
                    read -s pass
#                    pass=$REPLY
                    gpg -c --passphrase $pass $HOME/$name
                    rm $HOME/$name
                    echo "File has been saved to $HOME/$name.gpg"
                    exit 0
                else
                exit 0
                fi
          else
          exit 0
          fi
#############################################################################################################################
  exit 0
 fi

## if no file is given, it will proceed to ask for parameters.

  else  
 read -p "What do you want to do? Bin=\"b\", Unbin=\"t\":  "
  parameter="$REPLY"
  case $parameter in
   "b") parameter="b"
   action="Bin";;
   "t") parameter="t"
   action="Unbin";;
   "B") parameter="b";;
   "T") parameter="t";;
   *) echo "Invalid argument.  Exiting...";;
  esac
 echo
 echo
 echo "Enter the message to $action"
 echo
 echo
 read -p "Message:"
 echo
 echo

/usr/bin/unbin-scripts/conv.pl -$parameter "$REPLY"
exit 0
fi


Unbin-no-gui-hex
#!/bin/bash
##new file 2.  to separate bin/hex in nogui mode.  this is hex part

## determine whether there will be a file to decode/encode

read -p "Enter path to file to encode/decode (leave blank, if none)."
 if [ $REPLY > /dev/null ]
  then file=$REPLY
######################################################################
##  new section added to include gpg encryption
######################################################################

 if ls $file | grep gpg > /dev/null
  then
   echo "File is encrypted."
   echo "Type passphrase and press enter."
   read -s passphrase
   gpg --passphrase $passphrase $file
   clear
   goodname=$(ls $file | nawk '{sub(/.gpg/, "")};1')
   newfile=$(echo $goodname)
            cat $newfile > /tmp/encrypted.txt
            rm $newfile
            read -p "What do you want to do? View=\"v\", Hex=\"h\", Unhex=\"t\"  : "
            cryptans=$REPLY
            echo $cryptans > /tmp/unbin.txt
            if cat /tmp/unbin.txt | grep "v" > /dev/null
    then
     less /tmp/encrypted.txt
   fi
   if cat /tmp/unbin.txt | grep "h" > /dev/null
    then
     /usr/bin/unbin-scripts/hconv.pl -b -f /tmp/encrypted.txt
   fi
   if cat /tmp/unbin.txt | grep "t" > /dev/null
    then
    var=$(cat /tmp/encrypted.txt)
    /usr/bin/unbin-scripts/hconv.pl -t $var > /tmp/unbin.txt
    cat /tmp/unbin.txt
   fi
 else


######################################################################
##    these lines are just placeholders so i don't get lost.
######################################################################

  
  read -p "What do you want to do? Hex=\"h\", Unhex=\"t\":"
  parameter="$REPLY"
  case $parameter in
   "h") parameter="h";;
   "t") parameter="t";;
   "H") parameter="h";;
   "T") parameter="t";;
   *) echo "Invalid argument.  Exiting...";;
  esac
 /usr/bin/unbin-scripts/hconv.pl -$parameter -f $file

################################################################### 
## if no file is given, it proceeds to get parameers.
###################################################################

read -p "Save? \"y\"/\"n\":  "
    save=$REPLY
    case $save in
    "y") save="y";;
    "n") save="n";;
    *) echo "Invalid argument.  Exiting...";;
    esac
    echo $save > /tmp/unbin.txt
        if cat /tmp/unbin.txt | grep y > /dev/null
            then
            read -p "Name new file:  "
            name=$REPLY
            /usr/bin/unbin-scripts/hconv.pl -$parameter -f $file > $HOME/$name
            read -p "Encrypt? \"y\"/\"n\":  "
            encrypt=$REPLY
            case $encrypt in
            "y") encrypt="y";;
            "n") encrypt="n";;
            *) echo "Invalid argument.  Exiting...";;
            esac
            echo $encrypt > /tmp/unbin.txt
                if cat /tmp/unbin.txt | grep y > /dev/null
                    then
                    echo "Enter new gpg passphrase and press enter (pass not displayed)"
                    read -s pass
#                    pass=$REPLY
                    gpg -c --passphrase $pass $HOME/$name
                    rm $HOME/$name
                    echo "File has been saved to $HOME/$name.gpg"
                    exit 0
                else
                exit 0
                fi
          else
          exit 0
          fi
#############################################################################################################################
  exit 0
 fi


################################################################
 else

 read -p "What do you want to do? Hex=\"h\", Unhex=\"t\":"
  parameter="$REPLY"
  case $parameter in
   "h") parameter="h"
   action="Hex";;
   "t") parameter="t"
   action="Unhex";;
   "H") parameter="h";;
   "T") parameter="t";;
   *) echo "Invalid argument.  Exiting...";;
  esac
 echo
 echo
 echo "Enter the message to $action"
 echo
 echo
 read -p "Message:"
 echo
 echo
/usr/bin/unbin-scripts/hconv.pl -$parameter "$REPLY"
exit 0
fi


Unbin-no-gui-rev
#!/bin/bash

## this portion of the program reverses a string of text.  this is the 'nogui' part.  to be
## done all in terminal.


read -p "Enter path to file to encode/decode (leave blank, if none)."
 if [ $REPLY > /dev/null ]
  then file=$REPLY
  
###################################################################
## find out if gpg
###################################################################

if ls $file | grep gpg > /dev/null
  then
   echo "File is encrypted."
   echo "Type passphrase and press enter."
   read -s passphrase
   gpg --passphrase $passphrase $file
   clear
   goodname=$(ls $file | nawk '{sub(/.gpg/, "")};1')
   newfile=$(echo $goodname)
            cat $newfile > /tmp/encrypted.txt
            rm $newfile
            read -p "What do you want to do? View=\"v\", Rev=\"r\", Unrev=\"t\"  : "
            cryptans=$REPLY

### rev is just rev... it will reverse whatever it finds
### so reading options from user is merely a convention.
### the code will be rev /file regardless.             
            
            echo $cryptans > /tmp/unbin.txt
            if cat /tmp/unbin.txt | grep "v" > /dev/null
    then
     less /tmp/encrypted.txt
   else
     rev /tmp/encrypted.txt
   fi
 else


###################################################################  
  read -p "What do you want to do? Rev=\"r\", Unrev=\"t\":"
  parameter="$REPLY"
  case $parameter in
   "r") parameter="r";;
   "t") parameter="t";;
   "R") parameter="r";;
   "T") parameter="t";;
   *) echo "Invalid argument.  Exiting...";;
  esac
 rev $file
 
###################################################################

read -p "Save? \"y\"/\"n\":  "
    save=$REPLY
    case $save in
    "y") save="y";;
    "n") save="n";;
    *) echo "Invalid argument.  Exiting...";;
    esac
    echo $save > /tmp/unbin.txt
        if cat /tmp/unbin.txt | grep y > /dev/null
            then
            read -p "Name new file:  "
            name=$REPLY
            rev $file > $HOME/$name
            read -p "Encrypt? \"y\"/\"n\":  "
            encrypt=$REPLY
            case $encrypt in
            "y") encrypt="y";;
            "n") encrypt="n";;
            *) echo "Invalid argument.  Exiting...";;
            esac
            echo $encrypt > /tmp/unbin.txt
                if cat /tmp/unbin.txt | grep y > /dev/null
                    then
                    echo "Enter new gpg passphrase and press enter (pass not displayed)"
                    read -s pass
#                    pass=$REPLY
                    gpg -c --passphrase $pass $HOME/$name
                    rm $HOME/$name
                    echo "File has been saved to $HOME/$name.gpg"
                    exit 0
                else
                exit 0
                fi
          else
          exit 0
          fi
#############################################################################################################################
  exit 0
 fi 
 


## if no file is given, it will proceed to ask for parameters.

  else  
 read -p "What do you want to do? Rev=\"r\", Unrev=\"t\":"
  parameter="$REPLY"
  case $parameter in
   "r") parameter="r"
   action="Rev";;
   "t") parameter="t"
   action="Unrev";;
   "B") parameter="b";;
   "T") parameter="t";;
   *) echo "Invalid argument.  Exiting...";;
  esac
 echo
 echo
 echo "Enter the message to $action"
 echo
 echo
 read -p "Message:"
 echo
 echo

echo "$REPLY" > /tmp/unbin.txt
rev /tmp/unbin.txt
exit 0
fi