The method and principle of reversible encryption in PHP

Source: Internet
Author: User
This article mainly introduced the PHP reversible encryption method and the principle, the interest friend's reference, hoped to be helpful to everybody.

The PHP code is as follows:

<?phpclass Encryptcalss{var $key =12;function encode ($txt) {for ($i =0; $i <strlen ($txt); $i + +) {$txt [$i]=chr (Ord ($ txt[$i]) + $this->key);} Return $txt =urlencode (Base64_encode (UrlEncode ($txt)));} function decode ($txt) {$txt =urldecode (Base64_decode ($txt)), for ($i =0; $i <strlen ($txt); $i + +) {$txt [$i]=chr (Ord ($ txt[$i])-$this->key);} return $txt;}}? >

Discuz Encryption and decryption:

<?php/** * * @param string $string original text or ciphertext * @param string $operation operation (ENCODE |  DECODE), default is DECODE * @param string $key key * @param int $expiry ciphertext expiration, encryption time valid, unit seconds, 0 is permanent valid * @return The original text after string processing or after Base64_encode processed ciphertext * @example * $a = authcode (' abc ', ' Encode ', ' key '); * $b = Authcode ($a, ' DECODE ', ' key '); $b (ABC) * * $a = authcode (' abc ', ' ENCODE ', ' key ', 3600); * $b = Authcode (' abc ', ' DECODE ', ' key ');  Within one hours, $b (ABC), otherwise $b is empty */function authcode ($string, $operation = ' DECODE ', $key = ' ', $expiry =0) {$ckey _length=4;  $key =md5 ($key? $key: "kalvin.cn");  $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,16). $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,10) ==0| |      substr ($result, 0,10)-time () >0) &&substr ($result, 10,16) ==substr (MD5 (substr ($result, up). $keyb), 0,16)) {    Returnsubstr ($result, 26);    }else{return ';  }}else{return $KEYC. Str_replace (' = ', ' ', Base64_encode ($result)); }}?>

Summary: the above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

A few ways to implement the static page of PHP

PHP implementation and C # consistent method of DES encryption and decryption

How to filter, verify, Escape and password in PHP practice tutorial

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.