PHP自定义密文加密解密类

感觉日后还会用到,先记一下

<?php
 namespace app\common\util;
/**
 * Class HidoveNumberEncode PHP自定义密文加密解密类
 * @package app\common\util
 */
class HidoveNumberEncode
{
    private $encodeStr, $key, $length, $codelen, $codenums, $codeext;
 
    // 初始化
    public function __construct($length = 9, $encodeStr = 'Flpvf70CsakVjqgeWUPXQxSyJizmNH6B1u3b8cAEKwTd54nRtZOMDhoG2YLrI', $key = 2543.5415412812)
    {
        $this->key = $key;
        $this->length = $length;
        $this->encodeStr = $encodeStr;
        $this->codelen = substr($this->encodeStr, 0, $this->length);
        $this->codenums = substr($this->encodeStr, $this->length, 10);
        $this->codeext = substr($this->encodeStr, $this->length + 10);
    }
 
    // 编码
    public function encode($nums)
    {
        $rtn = "";
        $numslen = strlen($nums);
        //密文第一位标记数字的长度
        $begin = substr($this->codelen, $numslen - 1, 1);
        //密文的扩展位
        $extlen = $this->length - $numslen - 1;
        $temp = str_replace('.', '', $nums / $this->key);
        $temp = substr($temp, -$extlen);
        $arrextTemp = str_split($this->codeext);
        $arrext = str_split($temp);
        foreach ($arrext as $v) {
            $rtn .= $arrextTemp[$v];
        }
        $arrnumsTemp = str_split($this->codenums);
        $arrnums = str_split($nums);
        foreach ($arrnums as $v) {
            $rtn .= $arrnumsTemp[$v];
        }
        return $begin . $rtn;
    }
 
    //解码
    public function decode($code)
    {
        $begin = substr($code, 0, 1);
        $rtn = '';
        $len = strpos($this->codelen, $begin);
        if ($len !== false) {
            $len++;
            $arrnums = str_split(substr($code, -$len));
            foreach ($arrnums as $v) {
                $rtn .= strpos($this->codenums, $v);
            }
        }
 
        return $rtn;
    }
}

人已赞赏
技术文档

PHP字符编码(UTF-8/GBK)与json_encode/json_decode的关系

2020-10-1 23:43:03

源码分享

乐视视频解析源码

2018-12-25 22:32:47

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
有新私信 私信列表
搜索