Making Perl Crypt::CBC work with PHP mcrypt_cbc()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • josh.kuo@gmail.com

    Making Perl Crypt::CBC work with PHP mcrypt_cbc()

    First of all, I know this is a PHP news group, so my question may not
    be completely appropriate here since it involves some perl.

    I have read this post that dealt with a similar problem that I am
    having now:
    http://groups.google.com/group/comp....se_thread/thre...

    Summary of my problem:
    I need to have a perl script that encrypts a string, write the output
    to a file,
    and then have a PHP script read the file, and decrypts it. I took most
    of the code from this URL:

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    Below is my perl code used to encrypt

    <perl>
    #!/usr/bin/perl
    use MIME::Base64;
    use Crypt::CBC;
    my $key = '01234567890123 456789012345678 9';
    my $iv = '12345678';
    my $text = 'This is my plain text';
    $cipher = Crypt::CBC->new({'literal_ key' => 0,
    'key' => $key,
    'cipher' => 'Blowfish',
    'iv' => $iv,
    'padding' => 'null',
    'prepend_iv' => 0});
    $encrypted = $cipher->encrypt($text) ;
    $encoded = encode_base64($ encrypted);
    open(FILE, ">encrypt.txt") ;
    print FILE $encoded;
    close(FILE);
    </perl>

    And here's my PHP script used to decrypt:

    <?php
    $key = '01234567890123 456789012345678 9';
    $iv = '12345678';
    $lines = file('encrypt.t xt');
    $encoded = '';
    foreach ($lines as $line) {
    $encoded .= $line;
    }

    $encrypted = base64_decode($ encoded);
    $decrypted = mcrypt_cbc(MCRY PT_BLOWFISH, $key, $bin_encrypted,
    MCRYPT_DECRYPT, $iv);
    echo "decrypted : [$decrypted]";
    exit();
    ?>

    But the decryption doesn't seem to work... If I printed out the base64
    encoded strings, they match. So I suspect it's something that I did
    not do correctly either in the encryption or decryption of the message.

    Any help is appreciated.

  • josh.kuo@gmail.com

    #2
    Re: Making Perl Crypt::CBC work with PHP mcrypt_cbc()

    Answer my own question...

    The perl Crypt::CBC documentation states that the '-regenerate_key' and
    '-prepent_iv' are deprecated, that's why I used '-literal_key' instead
    of '-regenerate_key' .

    However, when I switch back to use the old '-regenerate_key' , I got a
    different error:

    If specified by -literal_key, then the key length must be equal to the
    chosen cipher's key length of 56 bytes at ./blow.pl line 9

    So I changed my key to something like this in both perl and PHP:

    my $long_key = "01234567890123 456789012345678 901234567890123 456789"
    my $key = substr($long_ke y, 0, 56);

    And while decrypting on the PHP end, I needed to use rtrim() on the
    decrypted string.

    After these changes everything is working.

    Comment

    Working...