【问题标题】:Decrypt data in php 7.2, which is Encrypted in php 5.6 using AES在 php 7.2 中解密数据,在 php 5.6 中使用 AES 加密
【发布时间】:2019-03-07 07:34:03
【问题描述】:

我使用以下方法在 php 应用程序和 android 应用程序之间加密和解密数据,它在 php 5.6 中工作正常,升级到 php 7.2 后它停止工作,现在我暂时移回 php 5.6。 我知道 php mcrypt 已贬值。 现在我的问题是我无法更新 Android 应用程序,我必须在服务器端以某种方式解决这个问题,我已经预定义了 keyiv ,两者都在服务器端作为应用程序,我该怎么做才能使用相同的密钥和 iv 并在 php 7.2 中的服务器端加密和解密数据。 提前致谢

 class MCrypt
        {
                private $key = 'Some Key'; 
                private $iv = 'Some IV'; 


                function __construct()
                {
                }

                function encrypt($str) {

                  //$key = $this->hex2bin($key);    
                  $iv = $this->iv;

                  $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);

                  mcrypt_generic_init($td, $this->key, $iv);
                  $encrypted = mcrypt_generic($td, $str);

                  mcrypt_generic_deinit($td);
                  mcrypt_module_close($td);

                  return bin2hex($encrypted);
                }

                function decrypt($code) {
                  //$key = $this->hex2bin($key);
                  $code = $this->hex2bin($code);
                  $iv = $this->iv;

                  $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);

                  mcrypt_generic_init($td, $this->key, $iv);
                  $decrypted = mdecrypt_generic($td, $code);

                  mcrypt_generic_deinit($td);
                  mcrypt_module_close($td);

                  return utf8_encode(trim($decrypted));
                }

                protected function hex2bin($hexdata) {
                  $bindata = '';

                  for ($i = 0; $i < strlen($hexdata); $i += 2) {
                        $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
                  }

                  return $bindata;
                }

        }

【问题讨论】:

    标签: php encryption


    【解决方案1】:

    您需要分析存储此类加密数据的影响表列表。

    现在创建一个类似于 select * from that table 的程序,解密这些数据并使用更新查询对其进行更新。

    在 5.6 PHP 版本中运行此程序,现在尝试使用 OpenSSL 修改加密逻辑并重新更新表。

    没有像函数或类方法那样简单的方法来实现从 5.6 到 7.2 的转换。

    【讨论】:

      【解决方案2】:

      mcrypt 在 7.2 中已弃用,默认情况下不包括在内。但是,如果您愿意,您仍然可以手动安装 mcrypt。您也可以改用 openssl_encrypt() 和 openssl_decrypt() 。

      【讨论】:

      • 我可以将相同的密钥和 iv 与 openssl_encrypt() 和 openssl_decrypt() 用于相同的方法吗?
      猜你喜欢
      • 2020-12-23
      • 1970-01-01
      • 2012-08-15
      • 2016-09-22
      • 1970-01-01
      • 2017-09-28
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多