Class crypt{
This article source: Dragon elder brother Blog
protected $key = ""; Public
Private Function KeyED ($txt, $encrypt _key)
{
$encrypt _key = MD5 ($encrypt _key);
$ctr = 0;
$tmp = "";
for ($i =0; $i
{
if ($ctr ==strlen ($encrypt _key)) {
$ctr = 0;
}
$tmp. = substr ($txt, $i, 1) ^ substr ($encrypt _key, $ctr, 1);
$ctr + +;
}
return $tmp;
}
Public function Encrypt ($txt, $key = "")
{
if (empty ($key)) {
$key = $this->key;
}
Srand (Double) microtime () *1000000);
$encrypt _key = MD5 (rand (0,32000));
$ctr = 0;
$tmp = "";
for ($i =0; $i
{
if ($ctr ==strlen ($encrypt _key)) {
$ctr = 0;
}
$tmp. = substr ($encrypt _key, $ctr, 1).
(Substr ($txt, $i, 1) ^ substr ($encrypt _key, $ctr, 1));
$ctr + +;
}
return $this->keyed ($tmp, $key);
}
Public function Decrypt ($txt, $key = "")
{
if (empty ($key)) {
$key = $this->key;
}
$txt = $this->keyed ($txt, $key);
$tmp = "";
for ($i =0; $i
{
$MD 5 = substr ($txt, $i, 1);
$i + +;
$tmp. = (substr ($txt, $i, 1) ^ $md 5);
}
return $tmp;
}
Public Function Setkey ($key)
{
if (empty ($key)) {
return null;
}
$this->key= $key;
}
Public Function GetKey ()
{
return $this->key;
}
Downloads by http://www.bkjia.com
}
$string = "http://www.52blogger.com";
$crypt = new Crypt ();
$crypt->setkey ("http://www.52blogger.com");
$enc _text = $crypt->encrypt ($string, $crypt->getkey ());
$dec _text = $crypt->decrypt ($enc _text, $crypt->getkey ());
echo "Before encryption: $string
\ n ";
echo "After encryption: $enc _text
\ n ";
echo "After decryption: $dec _text \ n";
?>
Execution Result:
Before encryption: http://www.52blogger.com
After encryption: Hszq ' z* kp~y (Wpvs=6q3gzbr5qkhtz (f=zm
After decryption: http://www.52blogger.com
Note that the string after each encryption is different, but also requires the public key to decrypt, if the public key is not correct, the decryption result and clear text is inconsistent. Unfortunately, this method does not seem to support Chinese ~
http://www.bkjia.com/PHPjc/363850.html www.bkjia.com true http://www.bkjia.com/PHPjc/363850.html techarticle PHP class crypt{//This article source: Dragon Brother blog protected $key =;//public key Private Function KeyED ($txt, $encrypt _key) {$encrypt _key = MD5 ( $encrypt _key); $ctr = 0; $tmp =; for ($i = ...