So we can use this function to do a lot of purposes, such as: Single Sign-on token encryption transmission, temporary password, etc.
Copy CodeThe code is as follows:
/**
* @param string $string original text or ciphertext
* @param string $operation operation (ENCODE | DECODE), default is DECODE
* @param string $key key
* @param int $expiry ciphertext validity, encryption time valid, unit seconds, 0 for permanent effective
* @return The original text after string processing or the ciphertext after base64_encode processing
*
* @example
*
* $a = Authcode (' abc ', ' ENCODE ', ' key ');
* $b = Authcode ($a, ' DECODE ', ' key '); $b (ABC)
*
* $a = Authcode (' abc ', ' ENCODE ', ' key ', 3600);
* $b = Authcode (' abc ', ' DECODE ', ' key '); Within one hours, $b (ABC), otherwise $b is empty
*/
function Authcode ($string, $operation = ' DECODE ', $key = ", $expiry = 3600) {
$ckey _length = 4;
Random key length value 0-32;
Join with the secret key, you can make ciphertext no rules, even if the original and key exactly the same, the encryption results will be different each time, increase the difficulty of cracking.
The greater the value, the greater the ciphertext variation, and the ciphertext change = 16 $ckey _length
When this value is 0 o'clock, the random key is not generated
$key = MD5 ($key? $key: Eabax::getappinf (' 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, (+). $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 < $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, ten) = = 0 | | substr ($result, 0,)-time () > 0) && substr ($result, ten, +) = = SUBSTR (MD 5 (substr ($result, +). $keyb), 0, 16)) {
Return substr ($result, 26);
} else {
Return ';
}
} else {
Return $KEYC. Str_replace (' = ', ' ', Base64_encode ($result));
}
}
http://www.bkjia.com/PHPjc/321662.html www.bkjia.com true http://www.bkjia.com/PHPjc/321662.html techarticle so we can take this function to do a lot of purposes, such as: Token encryption transmission of single sign-on, temporary password, etc. copy code code as follows:/** * @param string $string original ...