Openssl decryption ( DES ) returns false in PHP

201
March 27, 2019, at 6:00 PM

I'm trying to decrypt data with DES-ECB encryption, but the response is always false.

When I decrypt the string through https://www.tools4noobs.com/online_tools/decrypt/ the response is correct. This website is using the function "mcrypt_encrypt()" in PHP, but this functionality is not available on my server.

The code that i'm working on should work on PHP 7.1+ version, so the mcrypt_encrypt() isn't available anymore in my system.

$password         = 'password'; // Example
$decryptedString  = 'ThisShouldBeAnTestToCheckIfTheStringIsCorrectDecryptedThroughDES-ECB'; 
// Encrypted the string through the online tool.
$encryptedString  = 'zOToWEkYOoDnNWZ/sWEgOQQAX97NTZami/3V18yeKmoKiuam3DL0+Pu/LIuvjJ52zbfEx/+6WR4JcCjIBojv0H1eYCDUwY3o';
$opensslDecrypt   = openssl_decrypt(base64_decode($encryptedString),'DES-ECB', $password);
var_dump($opensslDecrypt); // Returns false.

I also tried to decrypt without the base64_decode function, but its still returning false.

Anyone have any idea why this isn't decrypting as it should be?

Answer 1

You must precise the $options in you method call:

Just add the following parameter after your password: OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING , ''

<?php
$password         = 'password';
$decryptedString  = 
'ThisShouldBeAnTestToCheckIfTheStringIsCorrectDecryptedThroughDES-ECB'; 
// Encrypted the string through the online tool.
$encryptedString  =  'zOToWEkYOoDnNWZ/sWEgOQQAX97NTZami/3V18yeKmoKiuam3DL0+Pu/LIuvjJ52zbfEx/+6WR4JcCjIBojv0H1eYCDUwY3o';
$opensslDecrypt   = openssl_decrypt(base64_decode($encryptedString),'DES-ECB', $password, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING , '');
var_dump(trim($opensslDecrypt));

Output: string(68) "ThisShouldBeAnTestToCheckIfTheStringIsCorrectDecryptedThroughDES-ECB"

For more information about this options:

What does OPENSSL_RAW_DATA do?

$options as (as for 2016) two possible values OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING. Setting both can be done by OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING. If no OPENSSL_ZERO_PADDING is specify, default pading of PKCS#7 will be done as it's been observe by [openssl at mailismagic dot com]'s coment in openssl_encrypt()

https://www.php.net/manual/en/function.openssl-decrypt.php

Rent Charter Buses Company
READ ALSO
How to include &lt;?php echo esc_html($product-&gt;get_review_count()); ?&gt; in function.php?

How to include <?php echo esc_html($product->get_review_count()); ?> in function.php?

I'd like to add :<?php echo esc_html($product->get_review_count()); ?> in my function which is :

153
Can i set password for already generated pdf file using mpdf or any other php pdf libraries

Can i set password for already generated pdf file using mpdf or any other php pdf libraries

I need to set password for my already generated pdf file using mpdf in php

155
How to change last modified date for file on my Minio storage using Laravel S3 driver?

How to change last modified date for file on my Minio storage using Laravel S3 driver?

I am storing uploaded files on my Minio storage using Laravel S3 driverProblem is that all files have a current timestamp as their "Last modified" date

334
MQTT using HiveMQ into IIS

MQTT using HiveMQ into IIS

Window server 2008 php 7+

193