Home  |  Linux  | Mysql  | PHP  | XML
From:zentara Date:Fri Jan  6 09:47:57 2006
Subject:Re: seeming failure with a specific word
On Thu, 05 Jan 2006 21:54:02 -0500, mlandman@face2interface.com (Marty
Landman) wrote:

>I'm trying to fix a bug in my application which encrypts using the 
>
>Marty

Hi, here are a couple of snippets that work. The basic problem I saw
was that files needed to be slurped into a scalar before encrypting,
NOT done line by line, or by buffer-segments.

So here are 2 good ways, the first for files, the second for strings.

#!/usr/bin/perl
use warnings;
use strict;
use Crypt::CBC;

my $KEY = 'secret_foo';

encryptFile( $0 );
decryptFile( "$0.enc");

sub encryptFile {
   my $filename = shift;
   $| = 1;
   print "Encrypting $filename...";
   $| = 0;

   my $cipher = Crypt::CBC->new(
      -key        => $KEY,
      -cipher     => 'Blowfish',
      -padding  => 'space',
      -add_header => 1
   );

   $cipher->start( 'encrypting' );
   open( ORGINAL,   "<./$filename" );
   open( ENCRYPTED, ">./$filename.enc" );

   binmode ORGINAL;
   binmode ENCRYPTED;

   while ( sysread( ORGINAL, my $buffer, 1024 ) ) {
      syswrite( ENCRYPTED, $cipher->crypt( $buffer ) );
   }
   #$cipher->finish;  #wrong
   syswrite( ENCRYPTED, $cipher->finish );

   close ENCRYPTED;
   close ORGINAL;

   print "done.\n";
}

###################################################################

sub decryptFile {

   my $filename = shift;
   $| = 1;
   print "Decrypting $filename...";
   $| = 0;


   my $cipher = Crypt::CBC->new(
      -key        => $KEY,
      -cipher     => 'Blowfish',
      -padding  => 'space',
      -add_header => 1
   );


   $cipher->start( 'decrypting' );
   open( ENCRYPTED, "<./$filename" );
   open( DECRYPTED, ">./$filename.decrypted" );

   binmode ENCRYPTED;
   binmode DECRYPTED;

   while ( sysread( ENCRYPTED, my $buffer, 1024 ) ) {
      syswrite( DECRYPTED, $cipher->crypt( $buffer ) );
   }
#   $cipher->finish;   #wrong
    syswrite( DECRYPTED, $cipher->finish );

   close DECRYPTED;
   close ENCRYPTED;

   print "done.\n";
}

__END__
################################
#################################
###############################
##################################

#!/usr/bin/perl
use warnings;
use strict;
use Crypt::CBC;

my $KEY = 'secret_foo';
my $string = 'yadda yadda yadda yadda';
print "$string\n";

my $enc = encryptString( $string );
my $dec = decryptString( $enc );

print "$dec\n";

############################################################
sub encryptString {
   my $string = shift;
   my $cipher = Crypt::CBC->new(
      -key        => $KEY,
      -cipher     => 'Blowfish',
      -padding  => 'space',
      -add_header => 1
   );

   my $enc = $cipher->encrypt( $string  );
   return $enc; 
}

###################################################################

sub decryptString {
   my $string = shift;
   my $cipher = Crypt::CBC->new(
      -key        => $KEY,
      -cipher     => 'Blowfish',
      -padding  => 'space',
      -add_header => 1
   );

   my $dec = $cipher->decrypt( $string  );
   return $dec; 
}

__END__



-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Navigate in group perl.crypto at sever nntp.perl.org
Previous Next




  
© No Copyright
You are free to use Anything
Site Maintained by PHP Developer
Powered By PHP Consultants