Frederic Lemoine has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I wrote a script using Crypt::CBC to encrypt a file with Rijndael. The
script is "almost" working. I can encrypt and decrypt, however, in the
decrypted text, I always get two or three words "corrupted". You will find
the script here under, and an example of a decrypted text, so that you can
see what I mean by "corrupted" (the original plaintext contains lines of
0123456789).
Thanks for your help.
#!/usr/bin/perl use strict; use warnings; use Crypt::Rijndael; use Crypt::CBC; my $my_key = $ARGV[0]; my $plainfile="test2.txt"; my $cipher; my $buffer; my $decrypted="$plainfile.cry.txt"; # this sub makes all keysize of 32 bytes, in case the key you give is # smaller. However, remember to use a strong key !!! sub get32 { my $data = shift; return "\0" x ( 32 - length($data)%32 ) . $data; } #-------------------------------------------------------------------- # Parameters #-------------------------------------------------------------------- $cipher = Crypt::CBC->new( {'key' => get32($my_key), 'cipher' => 'Rijndael', 'iv' => '§%(Ml7!B', 'regenerate_key' => 1, 'padding' => 'standard', 'prepend_iv' => 0 }); #-------------------------------------------------------------------- # Encryption #-------------------------------------------------------------------- open(FH_plain,"./$plainfile"); open(FH_crypted, ">$plainfile.cry"); while (read(FH_plain,$buffer,1024)) { print FH_crypted $cipher->encrypt($buffer); } close FH_plain; close FH_crypted; #-------------------------------------------------------------------- # Decryption #-------------------------------------------------------------------- open(FH_crypted, "<$plainfile.cry"); open(FH_decrypted, ">$decrypted"); while (read(FH_crypted,$buffer,1024)) { print FH_decrypted $cipher->decrypt($buffer); } close FH_crypted; close FH_decrypted;
Example of decrypted text
0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012HüÐ3ùÇI5SËLó±Ÿïr g²Ï®Æ5678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 0123456789012345678901234567890123456789012345678901234567890123456789 01234567890123456789012345¹š°ê6t€Á€&hOÁ*ŠF%Œö8O>¶f Ë3456789012345678901234567890123456789012345678901234567890123456789
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Crypt::CBC, Crypt::Rijndael - "almost" working
by Thelonius (Priest) on May 26, 2003 at 14:05 UTC | |
by Frederic Lemoine (Novice) on May 26, 2003 at 14:45 UTC |