PHP Cryptographic decryption string function, PHP encryption decryption string _php tutorial

Source: Internet
Author: User

PHP Cryptographic decryption string function, PHP encrypted decryption string


PHP Cryptographic decryption string function frequently used in the program

The code is as follows:

/********************************************************************* function Name: Encrypt function: Encrypt and decrypt string usage: Encrypt    : Encrypt (' str ', ' E ', ' nowamagic ');    Decryption: Encrypt (' encrypted string ', ' D ', ' nowamagic ');    Parameter description: $string: A string that requires encryption and decryption $operation: Determine whether encryption or decryption: E: Encryption D: Decryption $key: Encrypted key (key); http://www.cnblogs.com/roucheng/*********************************************************************/function        Encrypt ($string, $operation, $key = ") {$key =md5 ($key);        $key _length=strlen ($key);        $string = $operation = = ' D '? Base64_decode ($string): substr (MD5 ($string. $key), 0,8). $string;        $string _length=strlen ($string);        $rndkey = $box =array ();        $result = ";            for ($i =0; $i <=255; $i + +) {$rndkey [$i]=ord ($key [$i% $key _length]);        $box [$i]= $i;            } 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 = = ' D ') {if (substr ($result, 0,8) ==substr (MD5 (substr ($result, 8). $key), 0,8))            {return substr ($result, 8);            } else {return ';        }} else {return str_replace (' = ', ' ', Base64_encode ($result)); }    }

How to use:

$id =;    $token = Encrypt ($id, ' E ', ' a ');    echo ' encryption: '. Encrypt ($id, ' E ', ' a '); Echo '
'; Echo ' decryption: '. Encrypt ($token, ' D ', ' a ');

http://www.bkjia.com/PHPjc/1081558.html www.bkjia.com true http://www.bkjia.com/PHPjc/1081558.html techarticle PHP cryptographic decryption string function, PHP encryption decryption string program is often used in PHP cryptographic decryption string function code as follows:/*********************************************** ...

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