在PHP 7.2中替代mcrypt_encrypt [英] Exact alternate to mcrypt_encrypt in PHP 7.2

查看:105
本文介绍了在PHP 7.2中替代mcrypt_encrypt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

 函数encryptRJ256($ key,$ iv,$ string_to_encrypt){//$ rtn = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$ key,$ string_to_encrypt,MCRYPT_MODE_CBC,$ iv);$ rijndael =新的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);} 

我的密钥和IV是

  $ ky ='lkirwf897 + 22#bbtrm8814z5qq = 498j5';$ iv ='741952hheeyy66#cs!9hjv887mxx7 @ 8y'; 

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

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

mcrypt_encrypt输出:
3uw7mVZthiIPPNosvppZHd1jEau3Ul + 0BQ4AVS2t80skauq3Zv9z5uztvmiBpYqQcKGIDP5YHfdEBhPBfdVbxg ==

phpseclib输出:
3uw7mVZthiIPPNosvppZHd1jEau3Ul + 0BQ4AVS2t80tKnjjxVhuAwh3E1S5OnH1up5AujvQu1Grgyv16tNIEDw ==

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

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

解决方案

您可能需要填充所有内容"

如果您阅读过

Since mcrypt_encrypt is no longer supported in PHP 7.2, I am trying for exact alternate to this function.

After reading many SO answers I have found the following code which uses PHPSECLIB, but it's not producing the exact encrypted text as 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);
    }

My Key and IV is

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

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

The first 42 chars are equal but the rest is different as you can see

Text to encrypt: 57F0-ECD3-1A3B-341E-BA39-F81B-F020-0DE0

mcrypt_encrypt output:
3uw7mVZthiIPPNosvppZHd1jEau3Ul+0BQ4AVS2t80skauq3Zv9z5uztvmiBpYqQcKGIDP5YHfdEBhPBfdVbxg==

phpseclib output:
3uw7mVZthiIPPNosvppZHd1jEau3Ul+0BQ4AVS2t80tKnjjxVhuAwh3E1S5OnH1up5AujvQu1Grgyv16tNIEDw==

I need to produce the same encrypted text because this text is decrypted by another program which I can't change.

So my question is, is it possible to produce the same encrypted text as mcrypt_encrypt using phpseclib or any other way?

解决方案

You might need to "pad all the things"

If you read this entry on leaseweb it states over and over that mcrypt pads thing automatically to the correct block sizes that it can chew. That leads of course to different outputs because of all the null bytes.

So I'd suggest you make sure all your data you feed into your phpseclib code are padded with the correct amount of null bytes to simulate the input mcrypt feeds into the cipher.

Default if you look at the code of PHPSECLIB it pads by a character decided by the number of characters to pad.
mcrypt however pads with character 0.

So, let's fix this.

The changed code you need is:

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

I used the latest version of PHPSECLIB as avaialble on github, so my code differs from your example code. This is the full working example on my machine.

<?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 7.2中替代mcrypt_encrypt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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