简体   繁体   中英

Decrypt data in php 7.2, which is Encrypted in php 5.6 using AES

I used a following method to Encrypt and Decrypt data between a php application and android app, It was working fine in php 5.6, After upgrading to php 7.2 it stopped working, Now I have moved back to php 5.6 temporarily. I know php mcrypt is depreciated. Now my problem is I cannot give update to Android App, I have to fix this somehow in the server side, I have predefined key and iv , Both in server side as well as app, What do I do so that I can use same key and iv and encrypt and decrypt data on sever side in php 7.2. Thanks in advance

 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;
                }

        }

You need analyze the list of impact tables in which you have stored such encrypted data.

Now create a program something like select * from that table and decrypt those data and update it using the update query.

Run this program in 5.6 PHP version and now try to modify the encryption logic using OpenSSL and re-update the table.

There is no easy way to achieve that conversion from 5.6 to 7.2 like a function or class method.

mcrypt is deprecated in 7.2 and is not included by default. However, you can still install mcrypt manually if you'd like. You could also use openssl_encrypt() and openssl_decrypt() instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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