Skip to content
Advertisement

How to encrypt with openSLL using the DES-CBC method

Actually I’m using Mcrypt but I’m moving to OpenSSL and I need to be able to use it the exact same way as Mcrypt.

This is how I’m encrypting

mcrypt_encrypt(MCRYPT_DES, $key, $text, MCRYPT_MODE_cbc, "");

For the decryption, I already managed to do it in OpenSSL, both are working the same exact ways

//Using Mcrypt
mcrypt_decrypt(MCRYPT_DES, $key, $enc, MCRYPT_MODE_cbc, "");

//Using Openssl
openssl_decrypt($enc, 'des-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING); 

For the encryption using OpenSSL, this is my test code

$key = "123456SO";
$text = "name=louis&cp=75013";

$encMcrypt = mcrypt_encrypt(MCRYPT_DES, $key, $text, MCRYPT_MODE_cbc, "");
$encOpenssl = openssl_encrypt($text, "des-cbc", $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, "");

echo "Mcrypt : " . urlencode(base64_encode($encMcrypt));
echo " OpenSsl : " . urlencode(base64_encode($encOpenssl));

And this is the output :

Mcrypt : f0tF0ERITdKiI2SxrttYAJBVNBtoGR%2BD OpenSsl :

This is the official list of method, but I can’t find the DES methods.

I know that Openssl declared DES weak, but I still need to use it in my situation.

How can I encrypt in DES-CBC using OpenSSL and make it behave the same way as the Mcrypt fonction ?

Edit:

If I removed the OPENSSL_ZERO_PADDING option, the result is almost the one expected

Code:

openssl_encrypt($text, "des-cbc", $key, OPENSSL_RAW_DATA , "");

output:

Mcrypt  : f0tF0ERITdKiI2SxrttYAJBVNBtoGR%2BD
OpenSsl : f0tF0ERITdKiI2SxrttYANpJ%2BZaEiIFr

The first part of the string is correct but at the end it differs from the output of the Mcrypt encryption string.

Advertisement

Answer

Thanks to @Topaco I managed to make it work

I added the OPENSSL_ZERO_PADDING option to disable the PKCS7 padding, then I created a function to manually pad my string with 0x00

function zero_padding($text)
{
    if (strlen($text) % 8)
        $text = str_pad($text,strlen($text) + 8 - strlen($text) % 8, "");
    
    return $text;
}

$key = "123456SO";
$text = "name=louis&cp=75013";

$encMcrypt = mcrypt_encrypt(MCRYPT_DES, $key, $text, MCRYPT_MODE_cbc, "");
$encOpenssl = openssl_encrypt(zero_padding($text), "des-cbc", $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, "");

$encMcrypt = urlencode(base64_encode($encMcrypt));
$encOpenssl = urlencode(base64_encode($encOpenssl));

echo "Mcrypt :" . $encMcrypt;
echo "OpenSsl:" . $encOpenssl;

Output:

Mcrypt : f0tF0ERITdKiI2SxrttYAJBVNBtoGR%2BD
OpenSsl: f0tF0ERITdKiI2SxrttYAJBVNBtoGR%2BD
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement