为了账号安全,请及时绑定邮箱和手机立即绑定

php7.2后的aes加密类

标签:
PHP

<?php
namespace app\common\lib;
class Aes {

private $hex_iv = '878zxc321smn7d9s0wkjuytrvbnd6gaq'; 

private $key = '2j8s2jjugyuhgdnfijmwy36b7n8kmf8p'; 

function __construct($key) {
    $this->key = $key;
    $this->key = hash('sha256', $this->key, true);
}

public function encrypt($input)
{
$data = openssl_encrypt($input, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->hexToStr($this->hex_iv));
$data = base64_encode($data);
return $data;
}

public function decrypt($input)
{
    $decrypted = openssl_decrypt(base64_decode($input), 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->hexToStr($this->hex_iv));
    return $decrypted;
}

private function addpadding($string, $blocksize = 16) {

    $len = strlen($string);

    $pad = $blocksize - ($len % $blocksize);

    $string .= str_repeat(chr($pad), $pad);

    return $string;

}

private function strippadding($string) {

    $slast = ord(substr($string, -1));

    $slastc = chr($slast);

    $pcheck = substr($string, -$slast);

    if (preg_match("/$slastc{" . $slast . "}/", $string)) {

        $string = substr($string, 0, strlen($string) - $slast);

        return $string;

    } else {

        return false;

    }

}

function hexToStr($hex)
{

    $string='';

    for ($i=0; $i < strlen($hex)-1; $i+=2)

    {

        $string .= chr(hexdec($hex[$i].$hex[$i+1]));

    }

    return $string;
}

}

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消