php的字符串的加密和编码,PHP字符串编码解码函数,字符串安全加密解密方法!(可用于用户自动登录安全实现加密方法)...

PHP字符串编码解码函数,字符串安全加密解密方法:

方法1:/**

* 字符串编码解码函数

* @param: string $string 字符串

* @param: string $operation 分ENCODE和DECODE两种方式

* @param: string $key 算法密钥

* @param: int $expiry 有效期(单位秒)

* return: string

*/

function string_code($string, $operation = 'DECODE', $key = '', $expiry = 0) {

if($operation == 'DECODE'){

$string1 = substr($string, 0, floor(strlen($string)/3));

$string2 = str_replace($string1, "", $string);

$string = $string2.$string1;

$string = str_replace(["-", "_"], ["/", "+"], $string);

}

$ckey_length = 4;

$key = md5($key);

$keya = md5(substr($key, 0, 16));

$keyb = md5(substr($key, 16, 16));

$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length):

substr(md5(microtime()), -$ckey_length)) : '';

$cryptkey = $keya.md5($keya.$keyc);

$key_length = strlen($cryptkey);

$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) :

sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;

$string_length = strlen($string);

$result = '';

$box = range(0, 255);

$rndkey = array();

for($i = 0; $i <= 255; $i++) {

$rndkey[$i] = ord($cryptkey[$i % $key_length]);

}

for($j = $i = 0; $i 

$j = ($j + $box[$i] + $rndkey[$i]) % 256;

$tmp = $box[$i];

$box[$i] = $box[$j];

$box[$j] = $tmp;

}

for($a = $j = $i = 0; $i 

$a = ($a + 1) % 256;

$j = ($j + $box[$a]) % 256;

$tmp = $box[$a];

$box[$a] = $box[$j];

$box[$j] = $tmp;

$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));

}

if($operation == 'DECODE') {

if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0)

&& substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {

return substr($result, 26);

} else {

return '';

}

} else {

$result = $keyc.str_replace('=', '', base64_encode($result));

$result = str_replace(["/", "+"], ["-", "_"], $result);

$result1 = substr($result, floor(strlen($result)/3)*(-1));

$result2 = str_replace($result1, "", $result);

$result = $result1.$result2;

return $result;

}

}

// 可以用来自动登录用户等等

//1 、加密数据,然后存入cookie

$code = string_code('5|小川编程', 'ENCODE', 'CtLgVd7f6r1xs', 2592000);

cookie(md5(config('cookie_key')), $code, 2592000);

//2、取出cookie值,然后解密。

$cookie = cookie(md5(config('cookie_key')));

$value = string_code($cookie, 'DECODE', 'CtLgVd7f6r1xs');

$value = explode('|', $value, 2);

if(count($value) != 2) $this->error('自动登录失败');

list($uid, $lastTime) = $value;

if(!empty($uid)){

//有了用户ID,辅助值,我们就可以判断用户存在与否等等、

}else{

$this->error('自动登录失败');

}

方法2:/**

* edauth高效可逆随机加密函数

* @param string $string    明文 或 密文

* @param $operation:true表示加密,false表示解密

* @param $key: 密匙

* @param $outtime:密文有效期, 单位为秒

* @param $entype:加密方式 有md5和sha1两种 加密解密需要统一使用同一种方式才能正确还原明文

* @return string

*/

function edauth($string, $operation = true, $outtime = 0, $entype = 'md5') {

$key = md5($key ? $key : C('AUTH_KEY'));

$key_length = 4;

if ($entype == 'md5') { //使用md5方式

$long_len = 32;

$half_len = 16;

$entype == 'md5';

} else { //使用sha1方式

$long_len = 40;

$half_len = 20;

$entype == 'sha1';

}

$key      = $key != '' ? $key : substr(md5($_SERVER['DOCUMENT_ROOT']

. C('AUTH_KEY') . $_SERVER['REMOTE_ADDR']), 0, 30);

$fixedKey = hash($entype, $key);

$egiskeys = md5(substr($fixedKey, $half_len, $half_len));

$runtoKey = $key_length ? ($operation ?

substr(hash($entype, microtime(true)), -$key_length) :

substr($string, 0, $key_length)) : '';

$keys     = hash($entype, substr($runtoKey, 0, $half_len) . substr($fixedKey, 0, $half_len)

. substr($runtoKey, $half_len) . substr($fixedKey, $half_len));

$string   = $operation ? sprintf('%010d', $outtime ? $outtime + time() : 0)

. substr(md5($string . $egiskeys), 0, $half_len)

. $string : base64_decode(substr($string, $key_length));

$i = 0;

$result   = '';

$string_length = strlen($string);

for ($i = 0; $i 

$result .= chr(ord($string{$i}) ^ ord($keys{$i % $long_len}));

}

if ($operation) {

return $runtoKey . str_replace('=', '', base64_encode($result));

} else {

if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) &&

substr($result, 10, $half_len) == substr(md5(substr($result, $half_len + 10)

. $egiskeys), 0, $half_len)) {

return substr($result, $half_len + 10);

} else {

return '';

}

}

}

浏览器启用弹出窗口过滤功能,将无法跳转到下载页。在浏览器地址栏右边符号提示处点击允许就可以了!

郑重声明:

1、本站源码仅供个人学习研究和交流使用,请于下载后二十四小时内删除

2、本站大多资源来源于互联网、用户分享,仅供学习交流使用,本站不提供任何技术支持

3、本站联系方式Email:admin@youhutong.com ,收到邮件会第一时间处理。

4、如侵犯到任何版权问题,请立即告知本站(立即在线告知),本站将及时删除并致以最深的歉意

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值