php7 mcrypt_encrypt弃用,openssl_en

2019-10-08  本文已影响0人  勿_5d7a
public function encrypt($text)
    {
       // $xcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC,$iv);
        if (strlen($text) % 16) {
            $text = str_pad($text,strlen($text) + 16 - strlen($text) % 16, "\0");
        }
        $xcrypt = openssl_encrypt($text, 'AES-128-CBC',$key, OPENSSL_NO_PADDING, $iv);
        return base64_encode($xcrypt);
    }

   public function decrypt($xcrypt)
    {
     //  $string = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($xcrypt), MCRYPT_MODE_CBC, $iv);        
        $string = openssl_decrypt(base64_decode($xcrypt), 'AES-128-CBC',$key, OPENSSL_NO_PADDING, $iv);
        $pcheck = trim($pcheck);
        return $pcheck;
    }

上面有对加解密字符串进行base64转换以便于存数据库 也可以用别的转换如bin2hex,hex2bin

上一篇下一篇

猜你喜欢

热点阅读