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

Can't pack "unpacked" stuff...

by rupesh (Hermit)
on Jul 18, 2005 at 09:32 UTC ( [id://475751]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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


Monks,

I've been working with digests (thanks to DigitalKitty) and a few other encryption algorithms. One that I stumbled over is Crypt::CBC. I want a readable "encypted" value so that I can decrypt it at a later point of time.
The (very simple) code:
#!/usr/bin/perl use strict; use Crypt::CBC; my $cipher = Crypt::CBC->new( -key => 'test', -salt => 1, ); my $ciphertext = $cipher->encrypt("encrypt_me"); # my $plaintext = $cipher->decrypt($ciphertext); $print_this=unpack("H16", $ciphertext); #so far, so good $packed_stuff=pack("H16", $print_this); print $packed_stuff."\n";
Isn't $packed_stuff be equal to $ciphertext?
If not, having $print_this, how do I get $ciphertext?

Any help would do...

Thanks,
Rupesh.

Replies are listed 'Best First'.
Re: Can't pack "unpacked" stuff...
by jasonk (Parson) on Jul 18, 2005 at 09:50 UTC

    You should try printing out some of the intermediate steps and see if you get the values you are expecting. What you will find is that $print_this only contains one-quarter of the encrypted text, as $ciphertext is 32 bytes long, but you are only getting 8 of them the way you are calling unpack.

    What I would do in your shoes is use MIME::Base64, and replace your pack/unpack calls with encode_base64 and decode_base64. That will give you a readable string in a known encoding that is much easier to work with. Otherwise you need to figure out the length of the string before calling unpack, so you can pass the right count instead of hard-coding it to 16.


    We're not surrounded, we're in a target-rich environment!
Re: Can't pack "unpacked" stuff...
by socketdave (Curate) on Jul 18, 2005 at 10:02 UTC
    Just use encrypt_hex() and decrypt_hex(). These use a printable encrypted string. Hope this helps...
Re: Can't pack "unpacked" stuff...
by mkirank (Chaplain) on Jul 19, 2005 at 04:17 UTC
    #!/usr/bin/perl use Crypt::CBC; use strict ; use warnings; my $cipher = Crypt::CBC->new(-key => 'test', cipher=> 'DES', -salt => 1) || die "Couldn't create CBC +object"; my $cryptval = $cipher->encrypt_hex('secret word'); print " cryptval1 is $cryptval \n"; $cryptval = $cipher->decrypt_hex($cryptval); print " cryptval2 is $cryptval \n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://475751]
Approved by Tanalis
Front-paged by Old_Gray_Bear
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.