Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Basic Crypt::Blowfish usage question

by danger (Priest)
on Mar 31, 2002 at 05:54 UTC ( #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


Comment on Re: Basic Crypt::Blowfish usage question
Download Code
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
Node Status?
node history
Node Type: note [id://155565]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-07-24 03:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (156 votes), past polls