Vijay Boyapati
Vijay Boyapati

Reputation: 7728

Decoding/encoding openssl aes cbc string between Perl and PHP

I'm writing some server code in PHP and I have an offline process written in Perl and they need to communicate via encrypted strings. In PHP I have been using:

$encrypted_string = openssl_encrypt($my_string, "aes-128-cbc", "my_password", true, "1234567812345678");

I'd basically like to achieve the exact same string output using Perl. Any help with how I would do this is appreciated. Thanks!

Upvotes: 3

Views: 3009

Answers (1)

Vijay Boyapati
Vijay Boyapati

Reputation: 7728

I figured it out and can now reproduce identical output encrypting a string in Perl and PHP:

Perl:

use Crypt::CBC;
use MIME::Base64;

my $cipher = Crypt::CBC->new(
    {
        'key'         => 'length16length16',
        'cipher'      => 'Crypt::OpenSSL::AES',
        'iv'          => '1234567812345678',
        'literal_key' => 1,
        'header'      => 'none',
        keysize       => 128 / 8
    }
);

print encode_base64($cipher->encrypt($my_string), "");

PHP:

echo openssl_encrypt($my_string, "aes-128-cbc", "length16length16", true, "1234567812345678");

Upvotes: 5

Related Questions