Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Blowfish.pm: "input must be * bytes long"

by phax (Initiate)
on Aug 23, 2003 at 17:49 UTC ( [id://286085]=perlquestion: print w/replies, xml ) Need Help??

phax has asked for the wisdom of the Perl Monks concerning the following question:

Here is my code. This is my first time using modules and I'm confused. It's supposed to print input to output.
#!/usr/bin/perl -w use strict; use diagnostics; print "Enter some text: "; my $text=<STDIN>; chomp($text); my $key=pack("H16","0123456789ABCDEF"); my $cipher=new Crypt::Blowfish $key; my $ciphertext=$cipher ->encrypt($text); while ($plaintext) { print "$ciphertext\n"; }
uncaught exception from user code: input must be * bytes long at /usr/local/lib/perl5/site_perl/5.005/i386-freebsd/Crypt/ Blowfish.pm line 51, <STDIN> chunk 1. Crypt::Blowfish::encrypt(`Crypt::Blowfish:Hash(0x804e054)' ,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) called at ./bfencryptout line 10.

It's not a bug-it's an undocumented feature :D

Title edit by tye

Replies are listed 'Best First'.
Re: Blowfish.pm
by mpd (Monk) on Aug 23, 2003 at 18:03 UTC
    Three problems that I found:

    1) You forgot to use Crypt::Blowfish; ;)
    2/3) In the line: while ($plaintext) { ...
    $plaintext should be $text, and while should be changed to for.

    After fixing those, I had success. Make sure the input is exactly 8 bytes long, or you will get the uncaught exception you wrote of.
Re: Blowfish.pm
by esh (Pilgrim) on Aug 23, 2003 at 17:58 UTC

    From the Crypt::Blowfish manpage:

    $plaintext and $ciphertext must be of "blocksize()" bytes. (hint: Blowfish is an 8 byte block cipher)
    If you want to encrypt/decrypt text which is not exactly 8 bytes, you will need to read up on Crypt:CBC

    Update: Here is something closer to working, but it still has a few problems:

    #!/usr/bin/perl -w use strict; use diagnostics; use Crypt::CBC; my $key=pack("H16","0123456789ABCDEF"); my $cipher = Crypt::CBC->new({ cipher => 'Blowfish', key => $key, regenerate_key => 0, }); $cipher->start('encrypting'); print "Enter some text: "; my $text=<STDIN>; chomp($text); print $cipher->crypt($text); print $cipher->finish; print "\n";
    The main problem I see is that it is sending a human prompt and encrypted text to stdout. The encrypted text will be in binary format and so could include high bit characters that mess up your ability to read your terminal output. It's also pretty useless to read.

    You're going to want to send the encrypted text to some file or other process, in which case you'll want to change the prompt to go to stderr or the terminal.

    Also, were you wanting to encrypt a single line of input? Multiple lines of input independently? Multiple lines of input as a single encrypted chunk?

    Then again, perhaps you wisely just provided a small test case which shows your core problem instead of including your entire application which does something else entirely.

    -- Eric Hammond

Re: Blowfish.pm: "input must be * bytes long"
by Sifmole (Chaplain) on Aug 26, 2003 at 15:13 UTC
    You will need to make sure that your input string has a number of characters that is evenly divisible by 8. If the starting string isn't then you will need to pad it. Choose a padding that you can programmatically strip off when you decrypt.
      Thanks Perl Monks for your feedback. I will post the app when finish with it.
      It's not a bug-it's an undocumented feature :D
        Hi, this is how I managed to get blowfish encryption and decryption working on strings that are less or more than 8 characters long. Hope it's of come use to whoever comes across this.
        #!/usr/bin/perl use strict; use warnings; use Crypt::Blowfish; my $key=pack("H16","0123456789ABCDEF"); sub encrypt_8_chars{ my $plain_text=$_[0]; my $encrypted_data; my $encrypted_text; my $cipher = new Crypt::Blowfish $key; # Enlarge the string to 8 characters by appending with NULL characte +rs while(length($plain_text) < 8){ $plain_text=sprintf("%s\0",$plain_text); } $encrypted_data=$cipher->encrypt($plain_text); # Take the string and + encrypt using blowfish $encrypted_text=unpack("H16",$encrypted_data); return $encrypted_text; } # encrypt_8_chars sub decrypt_8_chars{ my $encrypted_text=$_[0]; my $encrypted_data; my $plain_text; my $cipher = new Crypt::Blowfish $key; $encrypted_data=pack("H16",$encrypted_text); $plain_text=$cipher->decrypt($encrypted_data); # Take the string and + decrypt using blowfish $plain_text =~ s/\0//g; # Shrink the string by removing appended wit +h NULL characters return $plain_text; } # decrypt_8_chars sub encrypt_text{ my $plain_text=$_[0]; my @plain_chunks; my $plain_chunk; my $encrypted_text=""; @plain_chunks=($plain_text =~ /.{1,8}/g); # Chop any size of the tex +t into up to 8 charachers and put each chunk into an array foreach $plain_chunk (@plain_chunks){ $encrypted_text=sprintf("%s%s",$encrypted_text,encrypt_8_chars($pl +ain_chunk)); } return $encrypted_text; } # encrypt_text sub decrypt_text{ my $encrypted_text=$_[0]; my @encrypted_chunks; my $encrypted_chunk; my $plain_text=""; @encrypted_chunks=($encrypted_text =~ /.{1,16}/g); # Chop any size o +f the text into up to 16 charachers and put each chunk into an array foreach $encrypted_chunk (@encrypted_chunks){ $plain_text=sprintf("%s%s",$plain_text,decrypt_8_chars($encrypted_ +chunk)); } return $plain_text; } # decrypt_text my $input; my $encrypted; my $decrypted; printf("Enter the string to encrypt\n"); chomp($input=<STDIN>); $encrypted=encrypt_text($input); printf("The encrypted value of %s is %s\n",$input,$encrypted); $decrypted=decrypt_text($encrypted); printf("The decrypted value of %s is %s\n",$encrypted,$decrypted);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://286085]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found