laravel5+版本aes128加解密

使用场景:
aes/cbc/pkcs5padding/128加解密
EncryptService.php代码示例如下

namespace App\Services;

/**
 * aes/cbc/pkcs5padding/128加解密
 */
class EncryptService
{
    //加密方法
    private static $sDefaultEncMethod = 'AES-128-CBC';
    //默认key值-自定义16位字符串长度
    private static $sDefaultEncKey = 'daSe3sa2sD4fw3dk';
    //随机字符串长度
    private static $iDefaultEncIvLen = 16;
    //随机字符串范围
    private static $sDefaultChars = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';

    private static $auth_encrypt = [
        'cipher' => 'aes-128-ecb',
        'key' => 'self-name'
    ];

    /**
     * 返回固定加解密串
     * @param $sData
     * @param $sProjectName
     * @param string $sType
     * @return bool|string
     */
    public static function getEncryptInfo($sData, $sType = 'encrypt')
    {
        //判断参数
        if (empty($sData)) {
            return false;
        }

        //存在时
        $sEncIv = self::$sDefaultEncKey;
        if ($sType == 'encrypt') {//加密
            $sEncryptStr = self::aesEncryptInfo($sData, $sEncIv, $sEncKey);
        } else if ($sType == 'decrypt') {//解密
            $sEncryptStr = self::aesDecryptInfo($sData, $sEncIv, $sEncKey);
        } else {
            return false;
        }

        return $sEncryptStr;
    }

    /**
     * 加密方法
     * @param string $sData 要加密的数据
     * @param string $sEncIv 加密使用的偏移量iv
     * @param string $sEncKey 加密使用的key
     * @param string $encryptMethod 加密使用的方法
     * @return string
     */
    public static function aesEncryptInfo($sData, $sEncIv = '', $sEncKey = '', $encryptMethod = 'AES-128-CBC')
    {
        if (empty($sData)) return '';

        if (empty($sEncIv)) $sEncIv = self::aesRandomStr(self::$iDefaultEncIvLen);  // 随机生成16位iv
        if (empty($sEncKey)) $sEncKey = self::$sDefaultEncKey;
        $sEncrypt = openssl_encrypt($sData, $encryptMethod, $sEncKey, 1, $sEncIv);

        // 返回iv拼密串之后再base64_encode, rawurlencode
        return rawurlencode(base64_encode($sEncIv . $sEncrypt));
    }

    /**
     * 解密方法
     * @param string $sData 加密过的数据
     * @param string $sDecIv 解密使用的偏移量iv
     * @param string $sDecKey 解密使用的key
     * @param string $decryptMethod 解密使用的方法
     * @return string
     */
    public static function aesDecryptInfo($sData, $sDecIv = '', $sDecKey = '', $decryptMethod = 'AES-128-CBC')
    {
        try {
            $iDecIvlen = $sDecIv ? strlen($sDecIv) : self::$iDefaultEncIvLen;
            if (empty($sDecKey)) $sDecKey = self::$sDefaultEncKey;
            $sData = base64_decode(rawurldecode($sData));
            $sDecIv = substr($sData, 0, $iDecIvlen);  // 获取IV
            $sData = substr($sData, $iDecIvlen);  // 获取数据

            return openssl_decrypt($sData, $decryptMethod, $sDecKey, 1, $sDecIv);  // 使用openssl进行解密
        } catch (\Exception $exception) {
            // 处理解密失败的逻辑
        }
        return '';
    }

    public static function medauth_encrypt($input)
    {
        $cipher = self::$auth_encrypt['cipher'];
        $key = self::$auth_encrypt['key'];
        $iv = "";
        $encrypt = openssl_encrypt($input, $cipher, $key, OPENSSL_RAW_DATA, $iv);
        return base64_encode($encrypt);
    }

    public static function medauth_decrypt($input)
    {
        $cipher = self::$auth_encrypt['cipher'];
        $key = self::$auth_encrypt['key'];
        $input = base64_decode($input);
        $iv = "";
        $decrypt = openssl_decrypt($input, $cipher, $key, OPENSSL_RAW_DATA, $iv);
        return $decrypt;
    }

    /**
     * 随机字符串
     * @param $iLength
     * @param string $sChars
     * @return string
     */
    public static function aesRandomStr($iLength, $sChars = '')
    {
        if (empty($sChars)) {
            $sChars = self::$sDefaultChars;
        }
        $sHashStr = '';
        $iMax = strlen($sChars) - 1;
        for ($i = 0; $i < $iLength; $i++) {
            $sHashStr .= $sChars[mt_rand(0, $iMax)];
        }
        return $sHashStr;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值