I am trying to use the openssl_encrypt function in PHP, this is my code:


$message = hex2bin("00112233445566778899aabbccddeeff");
$key = hex2bin("000102030405060708090a0b0c0d0e0f");
$method = "AES-128-ECB";

$cyphertext = openssl_encrypt($message, $method, $key);
echo bin2hex(base64_decode($cyphertext)) . "\n";


According to FIPS 197 (See page 35) and also this online AES Calculator, this should result in the following cyphertext:


but instead my code returns this string:


What am I doing wrong? Does the openssl implementation use a different blocksize?

And yes, I do know that I'm not supposed to use ECB, but this is only for testing purposes, as soon as I have figured out why this doesn't work I'll switch to CBC.

Thank you very much for your help.

1 Answer 1


To quote from the php docs' comments, emphasis by me:

In plain english, the buffer must be padded up to blockSize. If the buffer is already a multiple of blockSize, you add an entire new blockSize bytes as padding.

The value of the padding bytes MUST be the number of padding bytes as a byte...

So 5 bytes of padding will result in the following bytes added at the end of the ciphertext: [ 0x05 ][ 0x05 ][ 0x05 ][ 0x05 ][ 0x05 ]

Hope this saves someone else a few hours of their life.

Your message is one full block. Thus, the implementation of openssl_encrypt adds a padding block. This is why the second half of your result (which you did not expect) neatly decrypts to


using plain aes with that key - it is a padding added by the implementation, because plain aes is not the same as aes-cbc.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .