由于PHP 7.2不再支持mcrypt_encrypt,因此我尝试替代此功能。

在阅读了许多SO答案之后,我发现以下使用PHPSECLIB的代码,但是没有像mcrypt那样产生确切的加密文本。

function encryptRJ256($key,$iv,$string_to_encrypt)
    {

       // $rtn = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_encrypt, MCRYPT_MODE_CBC, $iv);

      $rijndael = new Crypt_Rijndael(CRYPT_RIJNDAEL_MODE_CBC);
      $rijndael->setKey($key);
      $rijndael->setIV($iv);
      $rijndael->setKeyLength(256);
      $rijndael->disablePadding();
      $rijndael->setBlockLength(256);
      $rtn = $rijndael->encrypt($string_to_encrypt);
      $rtn = base64_encode($rtn);
      return($rtn);
    }

我的 key 和IV是
  $ky = 'lkirwf897+22#bbtrm8814z5qq=498j5';

  $iv = '741952hheeyy66#cs!9hjv887mxx7@8y';

您可以看到,前42个字符相等,但其余的则不同

要加密的文本:57F0-ECD3-1A3B-341E-BA39-F81B-F020-0DE0



我需要产生相同的加密文本,因为此文本已被另一个我无法更改的程序解密。

所以我的问题是,是否有可能使用phpseclib或其他任何方式产生与mcrypt_encrypt相同的加密文本?

最佳答案

您可能需要“垫上所有东西”

如果您阅读此entry on leaseweb,它将一遍又一遍地将mcrypt pads声明为可以咀嚼的正确块大小。当然,由于所有空字节,导致不同的输出。

因此,我建议您确保使用正确数量的空字节来填充输入到phpseclib代码中的所有数据,以模拟输入到密码的mcrypt输入。

如果您查看它要填充的code of PHPSECLIB字符,则为默认值,该字符由要填充的字符数决定。
但是mcrypt填充字符0。

因此,让我们修复此问题。

您需要更改的代码是:

    $cipher->disablePadding();
    $length = strlen($text);
    $pad = 32 - ($length % 32);
    $text = str_pad($text, $length + $pad, chr(0));

我使用了最新版本的PHPSECLIB as avaialble on github,因此我的代码与您的示例代码不同。这是我机器上的完整工作示例。
<?php
include "vendor/autoload.php";
include "phpseclib/bootstrap.php";
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Crypt/Rijndael.php');
include('Crypt/Random.php');
use phpseclib\Crypt\Rijndael as Crypt_Rijndael;

$text = '57F0-ECD3-1A3B-341E-BA39-F81B-F020-0DE0';

$secret = 'lkirwf897+22#bbtrm8814z5qq=498j5';

$iv = '741952hheeyy66#cs!9hjv887mxx7@8y';

function encrypt128($secret, $iv, $str)
{

    return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $secret, $str, MCRYPT_MODE_CBC, $iv));
}

function encryptRJ256($key,$iv,$text)
{
    $cipher = new Crypt_Rijndael('cbc');
    $cipher->setBlockLength(256);
    // keys are null-padded to the closest valid size
    // longer than the longest key and it's truncated
    $cipher->setKeyLength(256);
    $cipher->setKey($key);
    // the IV defaults to all-NULLs if not explicitly defined
    $cipher->setIV($iv);
    $cipher->disablePadding();
    $length = strlen($text);
    $pad = 32 - ($length % 32);
    $text = str_pad($text, $length + $pad, chr(0));
    return base64_encode($cipher->encrypt($text));
}
function decryptRJ256($key,$iv,$text)
{
    $cipher = new Crypt_Rijndael('cbc'); // could use CRYPT_RIJNDAEL_MODE_CBC
    $cipher->setBlockLength(256);
    // keys are null-padded to the closest valid size
    // longer than the longest key and it's truncated
    $cipher->setKeyLength(256);
    $cipher->setKey($key);
    // the IV defaults to all-NULLs if not explicitly defined
    $cipher->setIV($iv);
    $cipher->disablePadding();
    return $cipher->decrypt(base64_decode($text));
}

echo $text;
echo encrypt128($secret, $iv, $text);
echo "\n";
$text = encryptRJ256($secret, $iv, $text);
echo $text;
echo "\n";
echo decryptRJ256($secret, $iv, $text);

php - 完全替代PHP 7.2中的mcrypt_encrypt-LMLPHP

关于php - 完全替代PHP 7.2中的mcrypt_encrypt,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54887285/

10-11 03:32