Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Can't pack "unpacked" stuff...

by rupesh (Hermit)
on Jul 18, 2005 at 13:32 UTC ( #475751=perlquestion: print w/replies, xml ) Need Help??
rupesh has asked for the wisdom of the Perl Monks concerning the following question:


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...


Replies are listed 'Best First'.
Re: Can't pack "unpacked" stuff...
by jasonk (Parson) on Jul 18, 2005 at 13: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 14: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 08: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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://475751]
Approved by Tanalis
Front-paged by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2018-01-21 19:42 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (230 votes). Check out past polls.