Go online to find it.
Php commonly used encryption functions are
MD5 encryption (irreversible), Crypt encryption (irreversible), Sha1 encryption (irreversible), Urlencode encryption (reversible)
base64 encoding encryption (reversible)
I still have too little knowledge, among which I have only used MD5 and base64
The most commonly used is MD5, although MD5 encryption is irreversible, but some simple double MD5 encrypted strings can be decrypted with a little money.
In order to prevent decryption, MD5 encryption of the string is generally performed, a fixed string is added to the encrypted string, and then MD5 encryption is performed. (It feels so troublesome...)
Share a private encryption and decryption function
/**
* 加密
* @param string $string 要加密或解密的字符串
* @param string $operation 加密 '' 解密 DECODE
* @param string $key 密钥,加密解密时保持一致
* @param int $expiry 有效时长,单位:秒
* @return string
*/
function encrypt_code($string, $expiry = 0, $key = '1234567890') {
$ckey_length = 1;
$key = md5($key ? $key : UC_KEY); //加密解密时这个是不变的
$keya = md5(substr($key, 0, 16)); //加密解密时这个是不变的
$keyb = md5(substr($key, 16, 16)); //加密解密时这个是不变的
$keyc = $ckey_length ? substr(md5(microtime()), -$ckey_length) : '';
$cryptkey = $keya . md5($keya . $keyc); //64
$key_length = strlen($cryptkey); //64
$string =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++) {
//字母表 64位后重复 数列 范围为48~122
$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++) {
$result .= chr(ord($string[$i]) ^ ($box[$i]));
}
$str = $keyc . str_replace('=', '', base64_encode($result));
// $str =htmlentities($str, ENT_QUOTES, "UTF-8"); // curl 访问出错
return $str ;
}
/**
* 解密
* @param string $string 要加密或解密的字符串
* @param string $operation 加密 '' 解密 DECODE
* @param string $key 密钥,加密解密时保持一致
* @param int $expiry 有效时长,单位:秒
* @return string
*/
function encrypt_decode($string, $expiry = 0,$key = '1234567890') {
$ckey_length = 1;
$key = md5($key ? $key : UC_KEY); //加密解密时这个是不变的
$keya = md5(substr($key, 0, 16)); //加密解密时这个是不变的
$keyb = md5(substr($key, 16, 16)); //加密解密时这个是不变的
$keyc = $ckey_length ? substr($string, 0, $ckey_length) : '';
$cryptkey = $keya . md5($keya . $keyc); //64
$key_length = strlen($cryptkey); //64
$string = base64_decode(substr($string, $ckey_length)) ;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
for ($i = 0; $i <= 255; $i++) {
//字母表 64位后重复 数列 范围为48~122
$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++) {
$result .= chr(ord($string[$i]) ^ ($box[$i]));
}
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 false;
}
}
Note: The $key in the encryption function and the decryption function must be exactly the same.
Have a good suggestion, please enter your comment below.
Welcome to personal blog
https://guanchao.site
Welcome to the Mini Program: