Search code examples
phpopensslmcryptrijndael

Decrypting data with openssl commandline tool


I have to following code and as far as I know it is correct, but it does not work. I am trying to encode data with PHP's Mcrpyt and then decode it with the openssl commandline tool.

This is my PHP code:

/*
 * Convert a normal ascii string to a hexadecimal string.
 * Complement of hexToString().
*/
function stringToHex($str)
{
    $hex_str = "";
    for ($i = 0; $i < strlen($str); ++$i)
    {
        $hex_str .= sprintf("%02X", ord($str[$i]));
    }

    return $hex_str;
}


    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM);

    $block_size = mcrypt_get_block_size("rijndael-128", "cbc");
    $pad = $block_size - (strlen($data) % $block_size);
    $data .= str_repeat(chr($pad), $pad);

    $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, "1234567812345678", $data, MCRYPT_MODE_CBC, $iv);

    $message = stringToHex($iv) . base64_encode($encrypted);

I append the IV to the encoded message. Say for example the IV is 00000000000000000000000000000000 (size is 32), then I use the following command for decryption:

openssl enc -d -aes-128-cbc -A -nosalt -K 31323334353637383132333435363738 -iv 00000000000000000000000000000000 -in file_in > file_out

Also note that 1234567812345678 is hex is 31323334353637383132333435363738. But I keep getting the same error message:

bad decrypt 1340:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:./crypto/evp/evp_enc.c:454:

Anyone?

Thanks in advance, all love, Jori.


Solution

  • Well, I tested your code and it worked with a couple of changes.

    1) Input for openssl should include only the ciphertext, not the prepended IV (as your code was incomplete I was not sure if you indeed stripped the IV from the ciphertext before processing it with openssl).

    2) Your openssl command was missing a parameter (-a), required to actually do the Base64 decoding (just using -A won't enable this). Again, as your description was incomplete I was not sure if you indeed Base64-decoded the message before storing it in file_in.

    Just to be complete, this is the code I used to test your code (I run it from the command line, not using the web server).

    <?php
    
    $data = "
    This is a test. This is only a test.
    Stack Overflow is collaboratively built and maintained
    by your fellow programmers.
    ";
    $keybin = "1234567812345678";
    
    
    //$iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM);
    $iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
    $block_size = mcrypt_get_block_size ("rijndael-128", "cbc");
    $pad = $block_size - (strlen ($data) % $block_size);
    $data .= str_repeat (chr ($pad), $pad);
    $encrypted = mcrypt_encrypt (MCRYPT_RIJNDAEL_128, $keybin, $data, MCRYPT_MODE_CBC, $iv);
    $message = base64_encode ($encrypted);
    
    echo "CIPHERTEXT=  " . $message . "\n";
    echo "IV=  " . bin2hex ($iv) . "\n";
    echo "KEY=  " . bin2hex ($keybin) . "\n";
    
    echo "\nTest with:\n\necho $message | openssl enc -d -aes-128-cbc -nosalt -a -A -K " . bin2hex ($keybin) . " -iv " . bin2hex ($iv) . "\n\n";
    
    ?>
    

    Other minor differences was I used PHP's bin2hex.

    It will produce an output like:

    CIPHERTEXT=  /e81Ua/0jxgff3j5GjKXaNilv5WqPYV7yRYy4AzsTUmGQeK0hcMjuUYp1Yrfthaox9zTI0DeDQT4fba+y/qTQahZpYRAKcZa209RVg4W1HrySfZPMRCxE+y8r8scL3Xmj+oMGFpS+cDo111OPqwHhNwWSHbMlsoJLvMr70ZiQmE=
    IV=  56c7c7248c68127cee8f0e54d89b4fc1
    KEY=  31323334353637383132333435363738
    
    Test with:
    
    echo /e81Ua/0jxgff3j5GjKXaNilv5WqPYV7yRYy4AzsTUmGQeK0hcMjuUYp1Yrfthaox9zTI0DeDQT4fba+y/qTQahZpYRAKcZa209RVg4W1HrySfZPMRCxE+y8r8scL3Xmj+oMGFpS+cDo111OPqwHhNwWSHbMlsoJLvMr70ZiQmE= | openssl enc -d -aes-128-cbc -nosalt -a -A -K 31323334353637383132333435363738 -iv 56c7c7248c68127cee8f0e54d89b4fc1
    

    The error you had (bad decrypt, digital envelope routines EVP_DecryptFinal_ex) usually means a wrong key or a corrupted ciphertext. I think in your example the problem was a corrupted ciphertext, caused by the prepended IV and/or lack of Base64 decoding.