md5+des加密

加密步骤:
1.获取13位时间戳
2.对时间戳进行md5加密,获取32位小写key
3.对key进行加工,key=li+key
4.将key转换为byte数组,获取前8个字符作为key2
5.用key2对原body进行des加密,加密模式ECB,填充pkcs7padding,输出hex,字符集utf8body

 /**
     * 加密
     * @param $data
     * @return mixed
     */
    public function encrypt($data)
    {
        $timestamp = $this->msectime();//当前时间戳13位
        $key = 'li'.md5($timestamp);
        $key_arr = $this->getbytes($key);
        $key2 =array_slice($key_arr,0,8);
        $en = openssl_encrypt($data, 'DES−ECB',$key2,OPENSSL_RAW_DATA);    //des加密,模式ecb,OPENSSL_RAW_DATA方式【会用PKCS#7进行补位】
        $en = bin2hex($en);  //输出hex
        return $en;
    }

    /**
     * 转换一个string字符串为byte数组
     * @param $str 需要转换的字符串
     * @param $bytes 目标byte数组
     */
    protected function getbytes($str) {
        $len = strlen($str);
        $bytes = array();
        for($i=0;$i<$len;$i++) {
            if(ord($str[$i]) >= 128){
                $byte = ord($str[$i]) - 256;
            }else{
                $byte = ord($str[$i]);
            }
            $bytes[] =  $byte ;
        }
        return $bytes;
    }

    /**
     * 生成13位时间戳
     * @return int
     */
    protected function msectime() {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return intval($msectime);
    }
查看当前PHP版本支持的加密方式
return openssl_get_cipher_methods();
die;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值