原生PHP 数字加密方法,随机生成散列不规则数字对
Posted 哈尔滨洛弘科技有限公司
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原生PHP 数字加密方法,随机生成散列不规则数字对相关的知识,希望对你有一定的参考价值。
/**
* @param int $num
* @return $this
* 数字加密
*/
function encode_num($num = 0)
$num_len = strlen($num);
if (!is_numeric($num) || $num_len > 15)
$this->data = false;
$this->message = '请输入数字';
return $this;
list($start_rand, $end_rand, $nums, $pies) = [rand(100, 999), rand(10, 99), [], self::unique_rand(1, 9, 9)];
for ($i = 0; $i < ($num_len / 2); $i++)
$nums[$pies[$i]] = ['num' => substr($num, $i * 2, 2), 'rand' => rand(1000, 7000), 'pie' => $pies[$i], 'i' => $i + 1];
ksort($nums);
$str = $start_rand;
foreach ($nums as $maps)
list($rand, $d_rand) = [rand(10, 70), rand(1, 9)];
$i = $rand + $maps['i'] + $d_rand;
$len=strlen($maps['num']);
$vs = ($maps['num'] * $maps['pie']) + $maps['rand'] + $start_rand - ($end_rand * $maps['i']) + $i;
$str .= $maps['rand'] . $maps['pie'] . $rand . $i . $d_rand .$len .$vs;
return $str . $end_rand;
/**
* @param int $num
* @return $this
* 数字解密
*/
function decode_num($num = 0)
if (!is_numeric($num))
return false;
if (empty($num)) $num = $this->data;
list($start_rand, $end_rand, $num_len, $encode, $nums) = [ceil(substr($num, 0, 3)), ceil(substr($num, -2)), ceil(strlen($num) - 5), substr(substr($num, 3), 0, -2), []];
for ($i = 0; $i < ceil($num_len / 15); $i++)
$nums[] = substr($encode, $i * 15, 15);
$n = [];
foreach ($nums as $maps)
list($maps_rand, $pie, $rand, $i, $d_rand, $vs,$len) = [ceil(substr($maps, 0, 4)), ceil(substr($maps, 4, 1)), substr($maps, 5, 2), intval(substr($maps, 7, 2)), substr($maps, 9, 1), ceil(substr($maps, 11)),ceil(substr($maps,10,1))];
$rule_i = intval($i - $rand - $d_rand);
$v = ceil(($vs - $i + ($end_rand * $rule_i) - $start_rand - $maps_rand) / $pie);
if($len==2 && strlen($v)==1) $v='0'.$v;
$n[$rule_i] = $v;
ksort($n);
return implode($n, '');
欢迎大家来测试体验,一起分享经验
以上是关于原生PHP 数字加密方法,随机生成散列不规则数字对的主要内容,如果未能解决你的问题,请参考以下文章
数字签名数字证书对称加密算法非对称加密算法单向加密(散列算法)
数字签名数字证书对称加密算法非对称加密算法单向加密(散列算法)