php 简单的加密和解密

臭大佬 2020-11-02 15:56:23 165
php 
简介 php 简单的加密和解密

代码

/**
* Description:加密
* User: Vijay <1937832819@qq.com>
* Date: 2020/11/2
* Time: 15:52
* @param string $string 解析字符串
* @param string $key key
* @return string|string[]
*/
function encryption($string = "", $key = "drp")
{
//str_split把字符串分割到数组中
$strArr = str_split(base64_encode($string));
$strcount = count($strArr);
foreach (str_split($key) as $key => $value) {
$key < $strcount && $strArr[$key] .= $value;
return str_replace(array('=', '+', '1'), array('O0O0O', 'o000o', 'oo00o'), join($strArr));
}
}

/**
* Description:解密
* User: Vijay <1937832819@qq.com>
* Date: 2020/11/2
* Time: 15:52
* @param string $string 解析字符串
* @param string $skey
* @return false|string
*/
function deciphering($string = '', $skey = 'drp')
{
$strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2);
$strCount = count($strArr);
foreach (str_split($skey) as $key => $value)
$key <= $strCount && @$strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
return base64_decode(join($strArr));
}

调用及结果

$a = encryption('32');
var_dump($a);

$a = 'MczIO0O0O';

$b = deciphering($a);
var_dump($b);

string(9) “MczIO0O0O”
string(2) “32”