Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php对称加密 #105

Open
mingyun opened this issue Oct 26, 2017 · 2 comments
Open

php对称加密 #105

mingyun opened this issue Oct 26, 2017 · 2 comments

Comments

@mingyun
Copy link
Owner

mingyun commented Oct 26, 2017

/**
 * 简单对称加密算法之加密
 * @param String $string 需要加密的字串
 * @param String $skey 加密EKY
 * @author Anyon Zou <[email protected]>
 * @date 2013-08-13 19:30
 * @update 2014-10-10 10:10
 * @return String
 */
function encode($string = '', $skey = 'cxphp') {
    $strArr = str_split(base64_encode($string));
    $strCount = count($strArr);
    foreach (str_split($skey) as $key => $value)
        $key < $strCount && $strArr[$key].=$value;
    return str_replace(array('=', '+', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
}
/**
 * 简单对称加密算法之解密http://www.thinkphp.cn/code/282.html
 * @param String $string 需要解密的字串
 * @param String $skey 解密KEY
 * @author Anyon Zou <[email protected]>
 * @date 2013-08-13 19:30
 * @update 2014-10-10 10:10
 * @return String
 */
function decode($string = '', $skey = 'cxphp') {
    $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2);
    $strCount = count($strArr);
    foreach (str_split($skey) as $key => $value)
        $key <= $strCount  && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
    return base64_decode(join('', $strArr));
}
echo '<pre>';
$str = '56,15123365247,54,四大古典风格';
echo "string : " . $str . " <br />";
echo "encode : " . ($enstring = encode($str)) . '<br />';
echo "decode : " . decode($enstring);
die();
使用base64安全处理方案解决URL传输问题
/**
 * 安全URL编码
 * @param string $data
 * @author Anyon <[email protected]>
 * @date 2017/06/26
 * @return string
 */
function encode($data) {
    return str_replace(array('+', '/', '='), array('-', '_', ''), base64_encode(serialize($data)));
}
/**
 * 安全URL解码
 * @param string $string
 * @author Anyon <[email protected]>
 * @date 2017/06/26
 * @return string
 */
function decode($string) {
    $data = str_replace(array('-', '_'), array('+', '/'), $string);
    $mod4 = strlen($data) % 4;
    ($mod4) && $data .= substr('====', $mod4);
    return unserialize(base64_decode($data));
}


/**
 * UTF8字符串加密
 * @param string $string
 * @author Anyon <[email protected]>
 * @date 2017/06/26
 * @return string
 */
function encode($string) {
    $chars = '';
    $len = strlen($string = iconv('utf-8', 'gbk', $string));
    for ($i = 0; $i < $len; $i++) {
        $chars .= str_pad(base_convert(ord($string[$i]), 10, 36), 2, 0, 0);
    }
    return strtoupper($chars);
}
/**
 * UTF8字符串解密
 * @param string $string
 * @author Anyon <[email protected]>
 * @date 2017/06/26
 * @return string
 */
function decode($string) {
    $chars = '';
    foreach (str_split($string, 2) as $char) {
        $chars .= chr(intval(base_convert($char, 36, 10)));
    }
    return iconv('gbk', 'utf-8', $chars);
}
//基本可以判断是base64编码
base64不适合直接放在URL里作为参数传输,发现base64编码中有“/” “=”符号。为解决此问题,可采用一种用于URL的改进Base64编码,它不在末尾填充'='号,并将标准Base64中的“+”和“/”分别改成了“_”和“-”,这样就免去了在URL编解码和数据库存储时所要作的转换。 URL传输会对这些进行编码,需要替换掉
//url base64编码
function urlsafe_b64encode($string) {
    $data = base64_encode($string);
    $data = str_replace(array('+','/','='),array('-','_',''),$data);
    return $data;
}
//url base64解码
function urlsafe_b64decode($string) {
    $data = str_replace(array('-','_'),array('+','/'),$string);
    $mod4 = strlen($data) % 4;
    if ($mod4) {
        $data .= substr('====', $mod4);
    }
    return base64_decode($data);
}

url参数验证 'http:'.env('WEB_DOMAIN').'/'.$id.'?invite='.$joinId.'_'.md5($joinId.'gkD7brVL')
$inviteBase = $this->getParam('invite', '');//邀请人
        $invite = 0;
        if ($inviteBase) {
            $inviteArr = explode('_', $inviteBase);
            if (count($inviteArr) == 2 && md5($inviteArr[0].'gkD7brVL') === $inviteArr[1]) {
                $invite = intval($inviteArr[0]);
            }
        }
//设置cookie 防止微信跳转丢失参数
if ($invite) {
            Request::setCookie(cookie('saas_webinar_view_invite', $invite.'|'.$webinar->id));
        }
if (!$invite) {
                $inviteCookie = Request::getCookie('saas_webinar_view_invite');
                $inviteCookieArr = explode("|",$inviteCookie);
                $inviteWebinarId = empty($inviteCookieArr['1'])?0:$inviteCookieArr['1'];
                if ($inviteWebinarId == $webinar->id) {
                    $invite = empty($inviteCookieArr['0'])?0:$inviteCookieArr['0'];
                }
            }
http://www.cnblogs.com/cnteam/articles/4240896.html
@mingyun
Copy link
Owner Author

mingyun commented Oct 26, 2017

 Mysql 获取成绩排序后的名次
select id,maxScore,(@rowNum:=@rowNum+1) as rowNo  
from t_user,  
(select (@rowNum :=0) ) b  
order by t_user.maxScore desc   
查出某个用户在所有用户成绩中的排名
select u.rowNo from (  
select id,(@rowNum:=@rowNum+1) as rowNo  
from t_user,  
(select (@rowNum :=0) ) b  
order by t_user.maxScore desc ) u where u.id="2015091810371700001";

http://www.qttc.net/201302281.html http://www.cnblogs.com/zengguowang/p/5541431.html
SELECT obj.user_id,obj.score,@rownum := @rownum + 1 AS rownum FROM (SELECT user_id,score FROM `sql_rank` ORDER BY score DESC) AS obj,(SELECT @rownum := 0) r ; 
SELECT @counter:=@counter+1 AS rownum,user_id,score FROM sql_rank,(SELECT @counter:=0) AS t ORDER BY score desc;

SELECT obj.user_id,obj.rownum from (select user_id,(@rownum := @rownum + 1) AS rownum from sql_rank,(select (@rowNum :=0)) b ORDER BY sql_rank.score DESC) obj where obj.user_id=103;
SELECT * FROM (
SELECT (@rownum:=@rownum+1) AS rownum, a.* FROM `test` a, (SELECT @rownum:= 0 ) r  ORDER BY a.`socre` DESC ,`createTime`
) AS b  WHERE id = 1

select invite_id,rownum from (select id,(@rownum := @rownum + 1) AS rownum,invite_id,count(if(be_invited_id=0,null,1)) as s  from webinar_invites,(select (@rowNum :=0)) b where webinar_id=463992667 group by invite_id having s>0 order by s desc ,id asc) as i where i.invite_id=1027156;

获取单个用户的成绩在所有用户成绩中的排名
SET @counter=0;
SELECT @counter:=@counter+1 AS Rank,LastName,Roll_no as Roll FROM Students ORDER BY Roll_no ASC;
SELECT @counter:=@counter+1 AS Rank,LastName,Roll_no as Roll FROM Students,(SELECT @counter:=0) AS t ORDER BY Roll_no ASC;
不用临时变量, 用表的自关联
SELECT   
  
(SELECT COUNT(ROLL_NO) FROM Students WHERE s.ROLL_NO <= ROLL_NO) AS Rank,   
  
s.LastName, s.Roll_no AS Roll   
  
FROM Students s   
  
ORDER BY s.Roll_no ASC 

添加公钥

生成密钥对,添加公钥到受信任的密钥中,并保留私钥。

$ ssh-keygen
(一路回车,将在 ~/.ssh/ 下生成 id_rsa  id_rsa.pub 两个文件,分别为私钥和公钥。)

$ cat .ssh/id_rsa.pub >> .ssh/authorized_keys
复制私钥并妥善保存到自己的电脑上。

禁止通过密码登录

$ vi /etc/ssh/sshd_config
# 查找并修改下面两项
PasswordAuthentication no // 禁止使用基于口令认证的方式登陆
PubkeyAuthentication yes // 允许使用基于密钥认证的方式登陆
在执行下面命令之前,确保私钥已经保存到自己的电脑(自己的 Windows / Linux / MacOS 电脑, 而不是 VPS),否则重启 sshd 服务后,将无法通过密码再连接到 VPS,而只能用私钥连接。

$ systemctl restart sshd
# 重启 sshd 服务
假如保存下来的私钥文件名为 id_rsa.vultr,那后面要通过 SSH 连接 VPS 时,执行下面命令 即可。

$ ssh -i id_rsa.vultr root@112.113.114.115
 112.113.114.115 替换为你的 VPS 公网 IP
Vultr的vps搭建shadowsocks翻墙http://mpc2008cn.github.io/2015/10/22/vps/

@mingyun
Copy link
Owner Author

mingyun commented Oct 26, 2017

在 Vultr 上搭建 Shadowsocks 服务端https://tenach.github.io/post/ss-server-on-vultr/

$vipMinId = UserVip::min('id');
        $vipMaxId = UserVip::max('id');
        $index = 0;
        $take = 5000;
        for ($i=$vipMinId;$i<=$vipMaxId;$i+=$take) {
            $userIsPayed = [];
            $skip = $take * $index;
            $userVipList = UserVip::skip($skip)->take($take)->lists('user_id','id');
            if (empty($userVipList)) continue;
            if (!empty($userIsPayed)) {
                UserVip::whereIn('id', $userIsPayed)->update(['is_payed' => 1]);
            }

            $index += 1;
        }

 $userVipCount = UserVipModel::where('is_payed', 0)->count();
        $take = 10000;
        $s = floor($userVipCount/$take);
        for ($i=0; $i<=$s; $i++) {
            $skip = $take*$i;//每页10000
            $userVips = UserVipModel::where('is_payed', 0)->skip($skip)->take($take)->orderBy('id')->get();
//调用脚本
$beginTime = strtotime('2017-09-01');
        $endTime = strtotime('2017-10-24');

        do {
            $this->call('UserInitiatorStatistics', [
                'timeline' => date('Y-m-d', $beginTime)
            ]);

            $this->info(date('Y-m-d', $beginTime));

            $beginTime += 86400;
        } while ($beginTime <= $endTime);

$timeline = $this->argument('timeline');
	$str='请输入你的php代码';
echo (strlen($str) + mb_strlen($str, 'utf8')) / 2;//中英文混合中文按2个字符计算
mb_strimwidth($l['subject'], 0, 30, '...', 'utf-8');
mb_strwidth(trim($subject), 'utf-8')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant