#!/usr/bin/perl

# Copyright (C) 2000 Mark E. Nielsen at GNUJobs.com

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# Web-Encrypt-Example version 0, Copyright (C) 2000 Mark E. Nielsen at GNUJobs.com
# Web-Encrypt-Example comes with ABSOLUTELY NO WARRANTY.
# This is free software, and you are welcome
# to redistribute it under certain conditions.

# The Computer Underground, Inc., hereby disclaims all copyright
# interest in the program `Web-Encrypt-Example'
# written by Mark E. Nielsen.
# Mark E. Nielsen, President of The Computer Underground

package MyPackage::Misc;

use strict;
use Crypt::Blowfish;

my $Blowfish_Key = "An extremely dumb password you should change.";
my $Blowfish_Cipher = new Crypt::Blowfish $Blowfish_Key;

#-----------------------------------
sub Encrypt
{
my $self = shift;
my $String = shift;

my $Temp = $String;
my $Encrypted = "";
while (length $Temp > 0)  
  {
    ### If less than 8 characters, padd it with tabs
  while (length $Temp < 8) {$Temp .= "\t";}
    ### Ecnrypt the 8 length segment
  my $Temp2 = $Blowfish_Cipher->encrypt(substr($Temp,0,8));
    ### Add it to the end
  $Encrypted .= $Temp2; 
    ### If it is 8 or less, abort, otherwise get the next segment
  if (length $Temp > 8) {$Temp = substr($Temp,8);} else {$Temp = "";}
  }

my $Unpacked = unpack("H*",$Encrypted);

return ($Unpacked);
}

#--------------------------------
sub Decrypt
{
my $self = shift;
my $String = shift;

my $Packed = pack("H*",$String);

my $Temp = $Packed;
my $Decrypted = "";
while (length $Temp > 0)  
  {
  my $Temp2 = substr($Temp,0,8);
    ### In theory, we could up with less than 8 characters, check
  if (length $Temp2 == 8) 
    {
    my $Temp3 = $Blowfish_Cipher->decrypt($Temp2);
    $Decrypted .= $Temp3;
    } 
  if (length $Temp > 8) {$Temp = substr($Temp,8);} else {$Temp = "";}
  }
   ### Getting rid of tabs at the end, which could be a bad thing
   ### but is how I did it. 
$Decrypted =~ s/\t+$//g;

return ($Decrypted);
}
</pre>


<p>
NOTE: There is one special thing you ought to do when decrypting information. Check to see if it contains
valid data. If it is numeric, make sure it is a number.Usually a smart idea is to always assume the number is
positive and less than a billion, and do something like this, 
<p>
<pre>
my $Error = 1;
if (($Value >0) && ($Value < 1000000000)) {$Error = 0;}
if ($Error == 1) {print "Darn it, this sucks, no valid data, bye bye!"; exit;}

