<?php
$id = "Http://www.xxxx.net";
$token = Encrypt ($id, ' E ', ' xxxx ');
echo ' encryption: '. Encrypt ($id, ' E ', ' xxxx ');
echo ' <br/> ';
Echo ' Decrypt: '. Encrypt ($token, ' D ', ' xxxx ');/*********************************************************************
Function Name: Encrypt
Function: Encrypt and decrypt strings
How to use:
Encryption: Encrypt (' str ', ' E ', ' Qingdou ');
Decryption: Encrypt (' encrypted string ', ' D ', ' Qingdou ');
Parameter description:
$string: Need to encrypt the decrypted string
$operation: To determine whether to encrypt or decrypt: E: Encrypt D: Decrypt
$key: Encrypted key (secret key);
*********************************************************************/
function Encrypt ($string, $operation, $key = ')
{
$SRC = Array ("/", "+", "=");
$dist = Array ("_a", "_b", "_c");
if ($operation = = = ' D ') {$string = Str_replace ($dist, $SRC, $string);}
$key =md5 ($key);
$key _length=strlen ($key);
$string = $operation = = ' D ' Base64_decode ($string): substr (MD5 ($string. $key), 0,8). $string;
$string _length=strlen ($string);
$rndkey = $box =array ();
$result = ';
For ($i =0 $i <=255; $i + +)
{
$rndkey [$i]=ord ($key [$i $key _length]);
$box [$i]= $i;
}
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 = = ' D ')
{
if (substr ($result, 0,8) ==substr (MD5 (substr ($result, 8). $key), 0,8))
{
Return substr ($result, 8);
}
Else
{
Return ";
}
}
Else
{
$rdate = str_replace (' = ', ', ', Base64_encode ($result));
$rdate = Str_replace ($src, $dist, $rdate);
return $rdate;
}
}
?>