Triple DES Mcrypt Replacement in PHP 7

392 Views Asked by At

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?

1

There are 1 best solutions below

1
dave On

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.