为什么AES解密代码不';不能在PHP7.2中工作

为什么AES解密代码不';不能在PHP7.2中工作,php,encryption,aes,Php,Encryption,Aes,我使用AES对从java发送到服务器的post参数进行加密。所以我在我的服务器中使用了below的类来解密post参数 <?php class MCrypt { private $hex_iv = '31323334353637383930616263646566'; # converted Java byte code in to HEX and placed it here private $key = '0FDOUZ.Qz'; #Same as in

我使用AES对从java发送到服务器的post参数进行加密。所以我在我的服务器中使用了below的类来解密post参数

<?php

class MCrypt {

private $hex_iv = '31323334353637383930616263646566'; # converted Java byte code in to HEX and placed it here               
private $key = '0FDOUZ.Qz'; #Same as in JAVA

function __construct() {
    $this->key = hash('sha256', $this->key, true);
    //echo $this->key.'<br/>';
}

function encrypt($str) {       
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
    $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $pad = $block - (strlen($str) % $block);
    $str .= str_repeat(chr($pad), $pad);
    $encrypted = mcrypt_generic($td, $str);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);        
    return base64_encode($encrypted);
}

function decrypt($code) {        
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

    mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
    $str = mdecrypt_generic($td, base64_decode($code));
    $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);        
    return $this->strippadding($str);      


}

/*
  For PKCS7 padding
 */

private function addpadding($string, $blocksize = 16) {
    $len = strlen($string);
    $pad = $blocksize - ($len % $blocksize);
    $string .= str_repeat(chr($pad), $pad);
    return $string;
}

private function strippadding($string) {
    $slast = ord(substr($string, -1));
    $slastc = chr($slast);
    $pcheck = substr($string, -$slast);
    if (preg_match("/$slastc{" . $slast . "}/", $string)) {
        $string = substr($string, 0, strlen($string) - $slast);
        return $string;
    } else {
        return false;
    }
}
function hexToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}
}

?>

我还在newuser.php文件中使用了下面的代码

 <?php
 .....
//decrypt 
 $encryption = new MCrypt();
 $phone= $encryption->decrypt($phoneenc);
 $password= $encryption->decrypt($passwordenc);
 $serialdivice= $encryption->decrypt($serialdiviceenc);
 $sequretyQustion= $encryption->decrypt($sequretyQustionenc);
 $sequretyAnsewr= $encryption->decrypt($sequretyAnsewrenc);
 .... ?>


在将php更新为php7.2之前,我的代码工作正常。但现在,当我更新php时,解密方法出现了错误。那么我该如何修复它呢?

我对java和php都使用了openssl。它现在工作正常。

我对java和php都使用了openssl。它现在工作正常。

可能是因为mcrypt被PHP7.2删除了?您应该使用openssl或Na钠扩展。我必须告诉支持主机进行检查吗@LLJ97你可以问他们,但mcrypt根本不应该被使用。它是不安全的,从2007年起就没有维护过!您应该将代码更改为使用openssl/Na钠,或者使用加密库。谢谢@llj97可能是因为mcrypt是用PHP7.2删除的?您应该使用openssl或Na钠扩展。我必须告诉支持主机进行检查吗@LLJ97你可以问他们,但mcrypt根本不应该被使用。它是不安全的,从2007年起就没有维护过!您应该将代码更改为使用openssl/Na钠,或者使用加密库。谢谢@LLJ97