The encryption algorithm is as follows:
Copy Code code as follows:
function Encrypt ($data, $key)
{
$key = MD5 ($key);
$x = 0;
$len = strlen ($data);
$l = strlen ($key);
for ($i = 0; $i < $len; $i + +)
{
if ($x = = $l)
{
$x = 0;
}
$char. = $key {$x};
$x + +;
}
for ($i = 0; $i < $len; $i + +)
{
$str. = Chr (Ord ($data {$i}) + (Ord ($char {$i}))% 256);
}
Return Base64_encode ($STR);
}
The decryption algorithm is as follows:
Copy Code code as follows:
function Decrypt ($data, $key)
{
$key = MD5 ($key);
$x = 0;
$data = Base64_decode ($data);
$len = strlen ($data);
$l = strlen ($key);
for ($i = 0; $i < $len; $i + +)
{
if ($x = = $l)
{
$x = 0;
}
$char. = substr ($key, $x, 1);
$x + +;
}
for ($i = 0; $i < $len; $i + +)
{
if (Ord (substr ($data, $i, 1)) < Ord (substr ($char, $i, 1))
{
$str. = Chr ((Ord (substr ($data, $i, 1)) + 256)-Ord (substr ($char, $i, 1));
}
Else
{
$str. = Chr (Ord (substr ($data, $i, 1))-Ord (substr ($char, $i, 1));
}
}
return $str;
}
A cryptographic key (that is, parameter $key) is required for the encryption and decryption process described above.
Copy Code code as follows:
$data = ' PHP encryption and decryption algorithm '; Encrypted information
$key = ' 123 '; Secret key
$encrypt = Encrypt ($data, $key);
$decrypt = Decrypt ($encrypt, $key);
echo $encrypt, "\ n", $decrypt;
The output is similar to the following results:
Copy Code code as follows:
Gnicsozzg+hns9zcfea7sefnghxf
PHP Encryption and decryption algorithm
As can be seen from the above results, this is a set of reversible encryption and decryption algorithm, can be used for partial restoration of data encryption.