简体   繁体   English

如何在 PHP 7.4 中解密 MCRYPT_BLOWFISH +ECB

[英]How can I decrypt MCRYPT_BLOWFISH +ECB in PHP 7.4

We are upgrading a platform from PHP 5.6 to PHP 7.4 where MCRYPT has been removed.我们正在将一个平台从 PHP 5.6 升级到 PHP 7.4,其中 MCRYPT 已被移除。

We have many clients that have URLs that include mcrypt encrypted codes that we need to be able to decrypt using PHP 7.4 compatible code as not to break existing functionality.我们有许多客户端的 URL 包含 mcrypt 加密代码,我们需要能够使用 PHP 7.4 兼容代码对其进行解密,以免破坏现有功能。 I have not been able to find an equivalent decryption process to solve this issue.我一直无法找到一个等效的解密过程来解决这个问题。

Here is the PHP 5.6 code for encrypting and decrypting the data:这是用于加密和解密数据的 PHP 5.6 代码:

function encrypt($string, $encryption_key) {
    return mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($string), MCRYPT_MODE_ECB);
}
function decrypt($encrypted_string, $encryption_key) {
    return mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB);
}

This is being used to produce the necessary query string parameter:这用于生成必要的查询字符串参数:

define("ENCRYPTION_KEY", "_enc_key");
$example_string = "My_String|2|Encode";
$enc_url_param = urlencode(base64_encode(encrypt($example_string, ENCRYPTION_KEY)));

This is what is taking place to decrypt the QS param:这是解密 QS 参数的过程:

$decrypted = trim(decrypt(base64_decode($_GET['enc_param']), ENCRYPTION_KEY));
// trimming is required due to trailing empty characters

Some more background:更多背景:

  • In the PHP 5.6 version, new querystring parameters were being generated with every retrieval, so I cannot predict all the code variations that are out there.在 PHP 5.6 版本中,每次检索都会生成新的查询字符串参数,因此我无法预测所有代码变体。
  • This encryption is only used to obfuscate IDs in a sharable URL and there are other measures in place to ensure security is maintained.此加密仅用于混淆可共享 URL 中的 ID,并且还有其他措施可确保维护安全性。

I found the answer here: https://stackoverflow.com/a/54190706/9178609我在这里找到了答案: https ://stackoverflow.com/a/54190706/9178609

Previously I had tried using OpenSSL with bf-ecb as the cipher (also recommended in the comments by @Sammitch), but the results were failing due to the necessity for the undocumented OPENSSL_DONT_ZERO_PAD_KEY以前我曾尝试使用带有bf-ecb作为密码的 OpenSSL(@Sammitch 在评论中也推荐),但由于需要未记录的OPENSSL_DONT_ZERO_PAD_KEY ,结果失败了

This is the code that works:这是有效的代码:

$decrypted = openssl_decrypt(base64_decode($_GET['enc_param']), 'bf-ecb', ENCRYPTION_KEY, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING | OPENSSL_DONT_ZERO_PAD_KEY);

It's also worth noting that PHP 5.6 does not provide the same results as running this code in PHP 7.4.还值得注意的是,PHP 5.6 提供的结果与在 PHP 7.4 中运行此代码的结果不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM