OpenSSL解密(DES)在PHP中返回false [英] Openssl decryption ( DES ) returns false in PHP

查看:155
本文介绍了OpenSSL解密(DES)在PHP中返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用DES-ECB加密解密数据,但响应始终为假.

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

当我通过> https://www.tools4noobs.com/online_tools/decrypt/解密字符串时响应是正确的.该网站正在使用PHP中的函数"mcrypt_encrypt()",但是该功能在我的服务器上不可用.

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.

我正在处理的代码应该可以在PHP 7.1+版本上使用,因此mcrypt_encrypt()在我的系统中不再可用.

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.

我还尝试了在没有 base64_decode 函数的情况下进行解密,但是它仍然返回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?

推荐答案

您必须在方法调用中精确调整 $ options :

You must precise the $options in you method call:

只需在密码后添加以下参数: OPENSSL_RAW_DATA |OPENSSL_ZERO_PADDING,''

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));

输出: string(68)"ThisShouldBeAnTestToCheckIfTheStringIsCorrectDecryptedThroughDES-ECB"

有关此选项的更多信息:

For more information about this options:

OPENSSL_RAW_DATA是做什么的?

$ options作为(与2016年一样)两个可能的值OPENSSL_RAW_DATA和OPENSSL_ZERO_PADDING.可以通过OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING设置两者.如果未指定OPENSSL_ZERO_PADDING,则将执行PKCS#7的默认填充,因为[openssl at mailismagic dot com]的注释在openssl_encrypt()

$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/zh-CN/function.openssl-decrypt.php

这篇关于OpenSSL解密(DES)在PHP中返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆