Programmer-who can write a PHP cryptographic decryption function, can customize the encryption decryption key, thank you

Source: Internet
Author: User
Who can write a PHP cryptographic decryption function, can be customized encryption decryption key, thank you

Reply content:

Who can write a PHP cryptographic decryption function, can be customized encryption decryption key, thank you

I have just learned VB in junior high School wrote a cryptographic function, the approximate principle (flow) is this:

明文Xor 密钥1 gets密文1
密文1Convert characters to hexadecimal by character密文2
密文2Xor 密钥2 get 密文3 //FINAL redaction

Encryption is a lot of ways (if you can not be reversible with MD5 () and other functions to achieve, even you can write a, haha), you can use a variety of artifice, you can find out on-line encryption function, but I still build the topic of the Lord himself to think of an algorithm bar, after all, engage in Web Generally not all try not to let people know their own encryption algorithm? Write your own proprietary encryption algorithm AH.

Still need to write? Mcrpty

You can use the encryption function in disuse, the code is as follows, very useful

//Start copying
/**
* $string plaintext or redaction
* $operation encrypt encode or decrypt decode
* $key key
* $expiry key Validity period
*/
Func tion Authcode ($string, $operation = ' DECODE ', $key = ', $expiry = 0) {
//dynamic key length, the same plaintext will generate different ciphertext is dependent on the dynamic key
//Join the random key, you can In order to make ciphertext without any rules, even if the original and key exactly the same, the encryption results will be different each time, increase the difficulty of cracking. The greater the
///value, the greater the ciphertext variation, the ciphertext change = 16 $ckey _length max.
//When this value is 0 o'clock, the random key
$ckey _length = + is not generated;

Key//$GLOBALS [' Discuz_auth_key '] Here you can modify $key = MD5 ($key? $key: $GLOBALS [' Discuz_auth_key ']) as required; Key A will participate in the decryption $keya = MD5 (substr ($key, 0, 16)),//key B will be used for data integrity verification $KEYB = MD5 (substr ($key, 16, 16));//key C is used to change the generated ciphertext $KEYC = $ck Ey_length? ($operation = = ' DECODE '? substr ($string, 0, $ckey _length): substr (MD5 (Microtime ()),-$ckey _length)): ';//key to participate in the Operation $cryp TKey = $keya. MD5 ($keya. $KEYC); $key _length = strlen ($cryptkey);//Clear text, the first 10 bits are used to hold the timestamp, verify the data validity when decrypting, 10 to 26 bits to save $keyb (key B), This key will be decrypted to verify the integrity of the data//if it is decoded, it will start from the $ckey_length bit, because the $ckey_length bit before the ciphertext to save the dynamic key to ensure that the decryption is correct $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 ();//Generate Key book for ($i = 0; $i <= 255; $i + +) {$rndkey [$i] = Ord ($cryptkey [$i% $key _length]);} Using a fixed algorithm to disrupt the key book, to increase randomness, as if complex, does not actually increase the strength of the ciphertext for ($j = $i = 0; $i < $i) {$j = ($j + $box [$i] + $rndkey [$i])% 256;    $tmp = $box [$i];    $box [$i] = $box [$j]; $box [$j] = $tmp;}    Core Plus decrypt section 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; The key is derived from the key book and then converted into a character $result. = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j])% 256]));} if ($operation = = ' DECODE ') {//substr ($result, 0, 10) = = 0 Verify data validity//substr ($result, 0)-time () > 0 Verify data valid Sex//substr ($result, +) = = substr (MD5 ($result, $keyb), 0, 16) Verifying data integrity//verifying data validity, see the format of unencrypted plaintext if (s Ubstr ($result, 0, 10) = = 0 | | substr ($result, 0, Ten)-time () > 0) && substr ($result, ten, 0) = = substr (MD5 (substr ($result),, 16    ) {return substr ($result, 26);    } else {return '; }} else {//To keep the dynamic key in ciphertext, which is why the same plaintext, the production of different ciphertext can be decrypted after the reason//because the encrypted ciphertext may be some special characters, the copy process may be lost, so the Base64 code return $KEYC. Str_re Place (' = ', ', Base64_encode ($result));} 

}
Run as follows
$a = "Apple";
$b = Authcode ($a, "ENCODE", "abc123");
echo $b. "
";
Echo Authcode ($b, "DECODE", "abc123");

To the end of this copy

If the above code you do not understand can go to http://my.oschina.net/wzwitblog/blog/160597 view

The next phpcms, the main function library, is said to be discuz in global.

  • 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.