PHP Cryptographic String Functions (Discuz built-in)

Source: Internet
Author: User
Tags ord

Contact Discuz For some time, has been very fond of this forum program, is really a very good program, Gray is often worth learning, here is a cryptographic function (including decryption)
This cryptographic function is characterized in that, more than the normal cryptographic function of a random key, can make ciphertext without any rules, even if the original and the key is exactly the same, the encryption results will be different each time, increase the crack difficulty. There is also a time limit function, you can make the encrypted string can only be valid for a specified time, over time, even if the key is known, you cannot restore the original string.

$string strings that need to be encrypted
$operation perform actions, that is, encryption or decryption, encryption is encode, decryption is decode
$key key, use the default key when empty, once set key, must remember, decryption must be consistent, otherwise you know ...
$expiry ciphertext effective time, default is 0, that is, permanently valid, customizable, unit is seconds

function _authcode ($string, $operation = ' DECODE ', $key = ", $expiry = 0) {
$ckey _length = 4; With the length of the secret key, the 0~32 bit, the value is 0 o'clock, does not produce the random key, the greater the value, the greater the ciphertext changes

$key = MD5 ($key? $key: ' Yutent_key '); Default key, you can define it yourself
$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)%;
$j = ($j + $box [$a])%;
$tmp = $box [$a];
$box [$a] = $box [$j];
$box [$j] = $tmp;
$result. = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j])]));

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));
}

}

Here's a simple demo

echo $a = Authcode (' hello,world! ', ' ENCODE ', ' 123 ');
echo $b = Authcode ($a, ' DECODE ', ' 123 ');
The results of the output run it yourself and look at it.

PHP Cryptographic String Functions (Discuz built-in)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.