0

I was using the mcrypt_get_iv_size function with my website and now it seems that it's deprecated

function Encrypt($word){
    $key = '.......';
    $iv = mcrypt_create_iv(
        mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC),
        MCRYPT_DEV_URANDOM
    );

    $encrypted = base64_encode(
        $iv .
        mcrypt_encrypt(
            MCRYPT_RIJNDAEL_128,
            hash('sha256', $key, true),
            $word,
            MCRYPT_MODE_CBC,
            $iv
        )
    );
    return $encrypted;
}

function Decrypt($word){
    $key = '.......';
    $data = base64_decode($word);
    $iv = substr($data, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));

    $decrypted = rtrim(
        mcrypt_decrypt(
            MCRYPT_RIJNDAEL_128,
            hash('sha256', $key, true),
            substr($data, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)),
            MCRYPT_MODE_CBC,
            $iv
        ),
        "\0"
    );
    return $decrypted;
}

Now I found a new function from stackoverflow which is this one

function Encrypt($word) {
    $key = '.......';
  $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
  $encrypted = openssl_encrypt($word, 'aes-256-cbc', $key, 0, $iv);
  return base64_encode($encrypted . '::' . $iv);
}
function Decrypt($word) {
    $key = '.......';
    list($encrypted_data, $iv) = explode('::', base64_decode($word), 2);
    return openssl_decrypt($encrypted_data, 'aes-256-cbc', $key, 0, $iv);
}

My question is: Do I have to use my previously working Decrypt function to get export all my encrypted words from my database and then Encrypt them with the new function and then import/update them into my database? Or is there a way to be able to Decrypt my words from the new function only? Thanks!

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Look up `open_ssl` in the PHP manual – RiggsFolly Jul 01 '21 at 19:27
  • https://stackoverflow.com/questions/41272257/mcrypt-is-deprecated-what-is-the-alternative – Dharman Jul 01 '21 at 21:37
  • I would try to decrypt the ciphertext encrypted with "mcrypt" using the new "openssl" function - if it works, you could just use the "new/openssl" one without the hassle to decrypt and encrypt all data. MCRYPT_RIJNDAEL_128 can easily get migrated to openssl. – Michael Fehr Jul 02 '21 at 07:58

0 Answers0