将Mcrypt Rinjdael-128-CBC函数转换为php-7.0

时间:2019-03-25 14:40:40

标签: php aes

我正在尝试对php进行加密解密。同事编写的用于使用MCRYPT RINJDAEL-128-CBC进行加密和解密的旧功能如下。 加密

        $key = pack('H*', $salt);    
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $iv);
        return base64_encode($iv . $ciphertext);
    }

解密

    $key = pack('H*', $salt);
    $ciphertext_dec = base64_decode($encodedText);
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv_dec = substr($ciphertext_dec, 0, $iv_size);
    $ciphertext_dec = substr($ciphertext_dec, $iv_size);
    return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);
}```

Due to php deprecating Mcrypt and only uses Openssl, how would I write a function that can do exactly the same?

0 个答案:

没有答案