我正在尝试使用DES-ECB加密来解密数据,但响应总是错误的。
当我通过https://www.tools4noobs.com/online_tools/decrypt/解密字符串时,响应是正确的。本网站在PHP中使用“mcrypt_encrypt()”功能,但我的服务器上没有此功能。
我正在处理的代码应该适用于PHP 7.1+版本,因此mcrypt_encrypt()在我的系统中不再可用。
$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。
任何人都知道为什么这不应该解密?
您必须在方法调用中精确调整$options
:
只需在密码后添加以下参数: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"
有关此选项的更多信息:
What does OPENSSL_RAW_DATA do?
$ options(2016年)两个可能的值OPENSSL_RAW_DATA和OPENSSL_ZERO_PADDING。设置两者都可以通过OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING完成。如果没有指定OPENSSL_ZERO_PADDING,那么PKCS#7的默认pading将会在openssl_encrypt()的[openssl at mailismagic dot com]的观察中完成。