有个PHP的动态加解密函数代码,求大神帮忙写一个对接这个PHP动态加解密的加解密的源码谢谢、
<?php//动态加密解密Codeby201724$key="123456";functionencrypt($str,$key){$thetime=(string)ti...
<?php
//动态加密解密 Code by 201724
$key="123456";
function encrypt($str,$key) {
$thetime = (string)time();
$timestrlen = strlen($thetime);
$keylen = strlen($key);
$estrlen = strlen($str);
$dumpstr = '';
$tmp='';
for($i=0;$i<$estrlen;$i++)
{
$tmp = ord($str[$i]);
for($j=0;$j<$keylen;$j++)
{
$tmp = $tmp ^ ord($key[$j]);
}
for($t=0;$t<$timestrlen;$t++)
{
$tmp = $tmp ^ ord($thetime[$t]);
}
$dumpstr .= chr($tmp);
}
return base64_encode($thetime . '|' .$dumpstr);
}
function decrypt($destr,$key) {
$str = base64_decode($destr);
$thetime = substr($str,0,10);
//第11个是|
$str = substr($str,11);
$timestrlen = strlen($thetime);
$keylen = strlen($key);
$estrlen = strlen($str);
$dumpstr = '';
$tmp='';
for($i=0;$i<$estrlen;$i++)
{
$tmp = ord($str[$i]);
for($j=0;$j<$keylen;$j++)
{
$tmp = $tmp ^ ord($key[$j]);
}
for($t=0;$t<$timestrlen;$t++)
{
$tmp = $tmp ^ ord($thetime[$t]);
}
$dumpstr .= chr($tmp);
}
return $dumpstr;
}
代码如上。求大神帮忙做做谢谢。。 展开
//动态加密解密 Code by 201724
$key="123456";
function encrypt($str,$key) {
$thetime = (string)time();
$timestrlen = strlen($thetime);
$keylen = strlen($key);
$estrlen = strlen($str);
$dumpstr = '';
$tmp='';
for($i=0;$i<$estrlen;$i++)
{
$tmp = ord($str[$i]);
for($j=0;$j<$keylen;$j++)
{
$tmp = $tmp ^ ord($key[$j]);
}
for($t=0;$t<$timestrlen;$t++)
{
$tmp = $tmp ^ ord($thetime[$t]);
}
$dumpstr .= chr($tmp);
}
return base64_encode($thetime . '|' .$dumpstr);
}
function decrypt($destr,$key) {
$str = base64_decode($destr);
$thetime = substr($str,0,10);
//第11个是|
$str = substr($str,11);
$timestrlen = strlen($thetime);
$keylen = strlen($key);
$estrlen = strlen($str);
$dumpstr = '';
$tmp='';
for($i=0;$i<$estrlen;$i++)
{
$tmp = ord($str[$i]);
for($j=0;$j<$keylen;$j++)
{
$tmp = $tmp ^ ord($key[$j]);
}
for($t=0;$t<$timestrlen;$t++)
{
$tmp = $tmp ^ ord($thetime[$t]);
}
$dumpstr .= chr($tmp);
}
return $dumpstr;
}
代码如上。求大神帮忙做做谢谢。。 展开
1个回答
展开全部
<?php
/**
* 加密函数
* @param string $string 明文 或 密文
* @param string $operation DECODE表示解密,其它表示加密
* @param string $key 密匙
* @param integer $expiry 密文有效期
*/
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$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 < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for($a = $j = $i = 0; $i < $string_length; $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 {
return $keyc.str_replace('=', '', base64_encode($result));
}
}
?>
我们公司常用的加密 解密函数
望采纳Thx
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询