-1

I have the following function that is written in php

function encrypt($string) {
        //Key
        $key = "key";

        //Encryption
        $cipher_alg = MCRYPT_TRIPLEDES;

        $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND); 

        $encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv);
        return base64_encode($encrypted_string);
        return $encrypted_string;
    }

A desktop application uses the same scheme to decrypt the generated string.Newer versions of PHP does not support mcrypt.How can i replace this code to achieve the same result?

techno
  • 6,100
  • 16
  • 86
  • 192
  • 4
    You could use [openssl](https://www.php.net/manual/en/function.openssl-encrypt.php). There are many posts on SO about migrating from mcrypt to openssl, e.g. [this post](https://stackoverflow.com/a/48157491/9014097). Note that TripleDES is outdated and ECB is generally insecure. An alternative is e.g. AES in GCM mode. – Topaco Feb 01 '21 at 08:36
  • @Topaco Thanks..I checked the post you have mentioned.The encrypted result is not the same. – techno Feb 01 '21 at 17:09
  • 1
    In the linked code, if you return the ciphertext in `_encrypt_openssl()` not hex, but base64 encoded, the ciphertext will be the same as that generated by the code you posted (assuming the same plaintext and key). If you get a different result, please post an example for a repro. – Topaco Feb 01 '21 at 17:39
  • @Topaco Thanks a lot :) It solved the issue. – techno Feb 01 '21 at 18:19

1 Answers1

-1

Based on this site mcrypt has been removed from PHP 7.2 (which I assume you're using) and instead added to PECL. Assuming you're on Ubuntu or similar the process to install it now is to install the following dependencies:

sudo apt-get -y install gcc make autoconf libc-dev pkg-config
sudo apt-get -y install php7.2-dev
sudo apt-get -y install libmcrypt-dev

Then install mcrypt via PECL:

sudo pecl install mcrypt-1.0.1

The video on that first link from TechRepublic has more information as well.

dave
  • 2,750
  • 1
  • 14
  • 22