This example describes the PHP source code Analysis DZX1.5 encryption and decryption function Authcode usage. Share to everyone for your reference. The specific analysis is as follows:
<?php $authkey = '; /** * @param string $string: PlainText (or ciphertext) that you enter needs to be encrypted (or decrypted) * @param string $operation: ' DECODE ' or others where decryption is the default, and other representations are encrypted * @param St Ring $key: Add decryption key * @param int $expiry: validity/function Authcode ($string, $operation = ' DECODE ', $key = ', $expiry = 0)
{Global $authkey;
$ckey _length = 4;
$key = MD5 ($key!= "" $key: $authkey);
$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 < 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 = = ' DECODE ') {if (substr ($result, 0,) = 0 | | substr ($result, 0)-time () > 0) &&
substr ($result) = = substr (MD5 (substr ($result,). $keyb) (0)) {return substr ($result, 26);
else {return ';
} else {return $KEYC. Str_replace (' = ', ', Base64_encode ($result));
}} $str = ' 1234 ';
$key = ' 1234 ';
echo "PlainText:". $str;
echo "<br/>";
echo "key:". $key;
$encode = Authcode ($str, ' one ', $key);
echo "<br/>";
echo "Encrypted ciphertext:". $encode;
echo "<br/>";
$decode = Authcode ($encode, ' decode ', $key); echo "After decryptionThe clear text: ". $decode;
/*end of php*/
The results of the operation are as follows:
Clear text: 1234
Key: 1234
Encrypted ciphertext: A52F67EXZGVY0HTQBO4VOREHQ3WXNL6E2ZLX75JGFOJW
Decrypted plaintext: 1234
I hope this article will help you with your PHP program design.