Re: FW: Crypt::CBC w/Crypt-DES

[email protected] (Dave Paris) Wed, 23 Oct 2002 08:08:51 -0400
Newsgroups perl.crypto
Message-ID <[email protected]>
You're not calling using Crypt::CBC's API correctly.  $cipher->finish() 
 is only called if you use  $cipher->start(...) and  $cipher->crypt(...)

See   perldoc Crypt::CBC  for the correct usage of the module.

This one-liner may help you as well:

user@host]$ perl -wle 'use Crypt::CBC; my $c=new 
Crypt::CBC("12345678","IDEA"); $p="foo"x100; $q=$c->encrypt($p); my 
$cx=new Crypt::CBC("12345678","IDEA"); $d=$cx->decrypt($q); if($p ne $d) 
{ print "something went wrong!" } else { print "all went well" }'

... of course, the creation of $cx there is simply to demonstrate that 
you can decrypt with a completely different Crypt::CBC object than the 
one that created your ciphertext.

I would re-write your script as this:

use Crypt::CBC;
my $cipher = new Crypt::CBC ({
    key => pack("H16","1122334455667788"),
    cipher => 'IDEA', });

open(READ,"e:/foo/bar/file");
binmode(READ);   # since you seem to be on Win32
my $contents = join '', (<READ>);
close(READ);

my $ciphertext = $cipher->encrypt($contents);

# if you're going to print, use:
my $printable_ciphertext = $cipher->encrypt_hex($contents);

# later, somewhere else...
my $recovered_contents = $cipher->decrypt($ciphertext);
# - or -
my $recovered_contents = $cipher->decrypt_hex($printable_ciphertext);

Regards,
-dsp

Toby Stuart wrote:

>
>>-----Original Message-----
>>From: Toby Stuart [mailto:[email protected]]
>>Sent: Wednesday, October 23, 2002 3:54 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 $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 $in;
>> open(IN,$ARGV[0]) || die $!;
>> print $cipher->decrypt($in) while read(IN,$in,1024);
>> print $cipher->finish;
>> close(IN);
>> </dec.pl>
>>
>>-- 
>>To unsubscribe, e-mail: [email protected]
>>For additional commands, e-mail: [email protected]
>>