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

Crypt::CBC how does the header/salt work?

by Anonymous Monk
on Nov 19, 2012 at 14:55 UTC ( #1004555=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've noticed that when I encrypt something using Crypt::CBC in the resulting string are always the same over multiple executions (using the same passphrase). The remainder of the string is always random. What is going on here? :)
#!/usr/bin/perl use Crypt::CBC; my $cipher = Crypt::CBC->new( -key => 'my secret key blah blah', -cipher => 'Blowfish', ); my $ciphertext = $cipher->encrypt_hex("This data is hush hush blah bla +h"); my $plaintext = $cipher->decrypt_hex($ciphertext); print "$ciphertext\n$plaintext\n";
$ perl t.pl 53616c7465645f5f27aad01783d47f045a963683bb06e3aa959fc83d7c94e1f5aad894 +d37c8df0d93abaaa80fb9e073b9555761f85204457 This data is hush hush blah blah $ perl t.pl 53616c7465645f5fe7e7d95a83e54fb5f8bfc966d07bff9c3db436a17693bad1c31a85 +766026d5f107d5c17adb017b0eeb46afa6f2d6e462 This data is hush hush blah blah $ perl t.pl 53616c7465645f5fec4cc8f6fc8ecbd4ebdeb938b7d4fe0cf028d3eed7a4dce694e001 +0e8cd99a94e799e3241a41d466a6755999bdb58e5f This data is hush hush blah blah $ perl t.pl 53616c7465645f5f639d36c6f916256557b485abb9c26837b235764faa361d2df38484 +42e58ea52bbbff329a3deb6d182d2b4cf04573e016 This data is hush hush blah blah $ perl t.pl 53616c7465645f5fda138a9a74e5c8c3081430fd3b895de307c1c303fbf48afa2d12bd +8c7097d2a3d600819f4c46fb16702536e97d27211c This data is hush hush blah blah

Comment on Crypt::CBC how does the header/salt work?
Select or Download Code
Re: Crypt::CBC how does the header/salt work?
by Anonymous Monk on Nov 19, 2012 at 14:59 UTC
    Another 2 questions. If I always want the resulting encrypted string to be unique/random what must I "keep" in addition to the passphrase so that I can decrypt the completely random bytes? And how must I call the Crypt::CBC->new() method to support that encryption and decryption?
Re: Crypt::CBC how does the header/salt work?
by Anonymous Monk on Nov 19, 2012 at 15:00 UTC
Re: Crypt::CBC how does the header/salt work?
by jethro (Monsignor) on Nov 19, 2012 at 15:47 UTC

    If I set the salt to something constant, I get something constant out. If I then change the plaintext, everything after the first 16 bytes changes. This seems to indicate that salt=>'1' is really the default and that the first 8 bytes are a constant prefix that probably defines the crypto algorithm. The next 8 bytes after the prefix are the salt.

Re: Crypt::CBC how does the header/salt work?
by Anonymous Monk on Nov 19, 2012 at 16:16 UTC
    Think I've got it figured out:
    my $cipher = Crypt::CBC->new( -cipher => 'Blowfish', -key => "blahblahblah", -iv => "01234567", -header => 'none' );
    this gives you a random encrypt_hex result with every run because the header isn't included.
Re: Crypt::CBC how does the header/salt work?
by zentara (Archbishop) on Nov 19, 2012 at 16:18 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2014-08-21 03:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (127 votes), past polls