Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Basic Crypt::Blowfish usage question

by danger (Priest)
on Mar 31, 2002 at 05:54 UTC ( [id://155565]=note: print w/replies, xml ) Need Help??


in reply to Basic Crypt::Blowfish usage question

Your problem is two-fold: First, as others have already pointed out: Blowfish is an 8-byte block encryption scheme, so you either need to pass it a string to encrypt of just 8 bytes, pad out a shorter string with nulls (\0), or work with longer data in 8-byte blocks (with padding when necessary) or get Crypt::CBC or Crypt::CBCeasy to deal with arbitrary length strings. Secondly (but your more immediate problem), your first encryption run prints out an unpacked version of the encrypted string (which is fine, makes it easier to read and type back in for the decrypt run right?) ... but in your decrypt run you try to decrypt that string as entered, you'll have to pack it up again before decrypting. Try this slight modification to your enc/dec pair:

# --- enc.pl --- #!/usr/bin/perl -w use strict; use Crypt::Blowfish; my $plaintext = <>; chomp $plaintext; my $key = "this is the pass phrase"; my $cipher = Crypt::Blowfish->new($key); my $ciphertext = $cipher->encrypt($plaintext); print unpack ("H16", $ciphertext),"\n"; # --- dec.pl --- #!/usr/bin/perl -w use strict; use Crypt::Blowfish; my $ciphertext = <>; chomp $ciphertext; my $key = "this is the pass phrase"; my $cipher = Crypt::Blowfish->new($key); my $plaintext = $cipher->decrypt(pack "H16",$ciphertext); print $plaintext,"\n"; # --- sample session (using 8-byte plaintext only) --- $ perl enc.pl noseeums <== what I type 8db8af77b828c382 <== what I get $ perl dec.pl 8db8af77b828c382 <== what I type noseeums <== what I get $ echo noseeums|perl enc.pl |perl dec.pl noseeums

Replies are listed 'Best First'.
Re: Re: Basic Crypt::Blowfish usage question
by DaWolf (Curate) on Mar 31, 2002 at 07:06 UTC
    Thanks a lot, danger.

    You've attacked the problem's core.

    Good aim! : )

    Er Galvão Abbott
    a.k.a. Lobo, DaWolf
    Webdeveloper

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-18 02:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found