Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 707
Vijay Boyapati Avatar asked Feb 27 '26 14:02

Vijay Boyapati


1 Answers

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");
like image 115
Vijay Boyapati Avatar answered Mar 02 '26 05:03

Vijay Boyapati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!