thanks $Bill, i had to add binmode on $ifh and $ofh (see below) to make it
work


> -----Original Message-----
> From: $Bill Luebkert [mailto:dbe@;wgn.net]
> Sent: Wednesday, October 23, 2002 4:56 PM
> To: Toby Stuart
> Cc: '[EMAIL PROTECTED]'
> Subject: Re: FW: Crypt::CBC w/Crypt-DES (small correction)
> 
> 
> Toby Stuart wrote:
> > see corrections below
> > 
> > 
> >>-----Original Message-----
> >>From: Toby Stuart [mailto:toby.stuart@;figtreesys.com.au]
> >>Sent: Wednesday, October 23, 2002 3:35 PM
> >>To: '[EMAIL PROTECTED]'
> >>Subject: Crypt::CBC w/Crypt-DES
> >>
> >>
> >>Hi All,
> >>
> >>I'm trying to encrypt/decrypt a text file using Crypt::CBC with DES.
> >>
> >>The encryption works fine but the decryption seems to yield 
> >>only the first
> >>few bytes of the original text.  I'm sure i'm missing 
> >>something simple.
> >>
> >>Any help appreciated.
> >>
> >>Example follows:
> >>
> >><enc.pl>
> >>use strict;
> >>use Crypt::CBC;
> >>
> >>my $key = pack "H16", "1122334455667788";
> >>my $cipher = new Crypt::CBC ($key, 'IDEA');
> > 
> > 
> > my $cipher = new Crypt::CBC ($key, 'DES');
> > 
> > 
> > 
> >>my $in;
> >>open(IN,'e:\some_largish_text_file') || die $!;
> >>print $cipher->encrypt($in) while read(IN,$in,1024);
> >>print $cipher->finish;
> >>close(IN);
> >></enc.pl>
> >>
> >>
> >>
> >><dec.pl>
> >>use strict;
> >>use Crypt::CBC;
> >>
> >>if (!@ARGV) { die "Usage: perl $0 encrypted_file > 
> >>decrypted_output_file"; }
> >>
> >>my $key = pack "H16", "1122334455667788";
> >>my $cipher = new Crypt::CBC ($key, 'IDEA');
> > 
> > 
> > my $cipher = new Crypt::CBC ($key, 'DES');
> > 
> > 
> >>my $in;
> >>open(IN,$ARGV[0]) || die $!;
> >>print $cipher->decrypt($in) while read(IN,$in,1024);
> >>print $cipher->finish;
> >>close(IN);
> >></dec.pl>
> 
> Try this one:
> 
> use strict;
> use Crypt::CBC;
> use IO::File;
> 
> my $key = pack "H16", "1122334455667788";
> my $cipher = new Crypt::CBC ($key, 'DES');
> 
> die "Usage: perl $0 file-to-encrypt [encrypted_output_file]\n" .
>    "or:    perl $0 -decrypt file-to-decrypt 
> [decrypted_output_file]\n" .
>    "  Second file arg defaults to STDIN for encrypt; STDOUT 
> for decrypt\n\n"
>    if not @ARGV;
> 
> my $decrypt = 0;
> for (my $ii = 0; $ii < @ARGV; ) {
>       if ($ARGV[$ii] =~ /^--?de/) {
>               $decrypt = 1;
>               splice @ARGV, $ii, 1;
>               next;
>       }
>       $ii++;
> }
> 
> my $ifh = new IO::File;
> if (@ARGV) {
>       $ifh->open("<$ARGV[0]") or die "open $ARGV[0]: $!";
> } else {
>       $ifh->open("<&STDIN") or die "open $ARGV[0]: $!";
> }
> 
> my $ofh = new IO::File;
> if (@ARGV > 1) {
>       $ofh->open(">$ARGV[1]") or die "open $ARGV[1]: $!";
> } else {
>       $ofh->open(">&STDOUT") or die "open $ARGV[1]: $!";
> }
> 
> my $in;
> if ($decrypt) {
>       $cipher->start('decrypting');
        
        binmode($ofh);

>       print $ofh $cipher->crypt($in) while read $ifh, $in, 1024;
>       print $ofh $cipher->finish;
> } else {
>       $cipher->start('encrypting');

        binmode($ifh);
        
>       print $ofh $cipher->crypt($in) while read $ifh, $in, 1024;
>       print $ofh $cipher->finish;
> }
> $ifh->close;
> $ofh->close;
> 
> __END__
> 
> 
> -- 
>    ,-/-  __      _  _         $Bill Luebkert   ICQ=162126130
>   (_/   /  )    // //       DBE Collectibles   Mailto:dbe@;todbe.com
>    / ) /--<  o // //      http://dbecoll.tripod.com/ (Free 
> site for Perl)
> -/-' /___/_<_</_</_     Castle of Medieval Myth & Magic 
http://www.todbe.com/

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to