php simple encryption and decryption

? < PHP
 namespace App \ Service; 

/ * 
* @link http://kodcloud.com/ 
* @author warlee | E-mail: [email protected] 
* @copyright warlee 2014. (on Shanghai) Co., Ltd 
* @ License http://kodcloud.com/tools/licenses/license.txt 
* ------ 
* decryption string class; 
* one-time pad; decryption and timing of effective 
* & dynamically encryption key may be used to generate 
* demo: 
* encryption: echo Mcrypt :: encode ( 'ABC', '123'); 
* decryption: echo Mcrypt :: decode ( '9f843I0crjv5y0dWE_-uwzL_mZRyRb1ynjGK4I_IACQ', '123'); 
* / 

class Mcrypt {
     Private  static $ default_key = ' A ! TAKA: d1989lmc1988ldEv, E ' ; 

    / * *
     * Character encryption, one-time pad, the timing can be effectively decrypt 
     * 
     * @param description String String $ 
     * @param string $ key Key 
     * @param int $ expiry valid ciphertext, the unit s, 0 is the permanent 
     * @return string encryption the contents 
     * / 
    public  static function encode ($ String , Key $ = '' , expiry $ = 0 ) { 
        $ ckeyLength = . 4 ; 
        $ Key = MD5 (Key $ $ Key:? :: $ Self default_key); // decryption key 
        $ KEYA = MD5 (substr ($ Key, 0 , 16 ));          // for data integrity verification 
        $ KEYB = MD5 (substr ($ Key, 16 ,16 ));          // for variations generated ciphertext (initialization vector IV) 
        $ KEYc = substr (MD5 (microtime, and ()), - $ ckeyLength); 
        $ cryptkey = .. KEYA MD5 $ ($ $ KEYA KEYc); 
        keylength $ = strlen ($ cryptkey); 
        $ String = sprintf ( ' % 010d ' ?, $ $ expiry expiry + Time (): 0 ) .substr (MD5 ($ String $ KEYB),. 0 , 16 ) $. String ; 
        $ StringLength = strlen ($ String ); 

        $ rndkey = Array ();
         for($ I = 0 ; $ I <= 255 ; $ I ++ ) { 
            $ rndkey [$ I] = the ord ($ cryptkey [$ I% $ keylength]); 
        } 

        $ Box = Range ( 0 , 255 );
         // play chaos book key, increase randomness 
        for ($ I $ J = = 0 ; $ I < 256 ; I ++ $ ) { 
            $ J = (J + $ $ Box [$ I] + $ rndkey [$ I])% 256 ; 
            $ tmp = $ Box [$ I]; 
            $ Box [$ I] = $ Box [$ J]; 
            $ Box [$ J] = $ tmp; 
        } 
        //Encryption and decryption key derived from the key XOR book, then into the character 
        $ Result = '' ;
         for ($ = $ A $ I = J = 0 ; $ I <$ StringLength; $ I ++ ) { 
            $ A = ($ A + . 1 )% 256 ; 
            $ J = ($ J + $ Box [$ A])% 256 ; 
            $ tmp = $ Box [$ A]; 
            $ Box [$ A] = $ Box [$ J] ; 
            $ Box [$ J] = $ tmp; 
            . $ Result = CHR (the ord ($ String [$ I]) ^ ($ Box [($ Box [$ A] + $ Box [$ J])% 256 ]) ); 
        } 
        $ Result. = $ KEYc str_replace ( ' = ' , '' , the base64_encode ($ Result)); 
        $ Result = str_replace (Array ( ' + ' , ' / ' , ' = ' ), Array ( ' - ' , ' _ ' , ' . ' ), $ Result);
         return $ Result; 
    } 

    / * * 
     * decrypted character, one-time pad,Timing can effectively decrypt 
     * 
     * @param String String $ ciphertext 
     * @param string $ key decryption key 
     content after decryption @return string *
     */
    public static function decode($string,$key = '')
    {
        $string = str_replace(array('-', '_', '.'),array('+', '/', '='), $string);
        $ckeyLength = 4;
        $key = md5($key ? $key : self::$default_key); //解密密匙
        KEYA MD5 = $ (substr ($ Key, 0 , 16 ));          // for data integrity verification 
        $ KEYB = MD5 (substr ($ Key, 16 , 16 ));          // for changes in the ciphertext generated (initialized vector IV) 
        $ KEYc = substr ($ String , 0 , $ ckeyLength); 
        $ cryptkey = $ KEYA MD5 ($ KEYA $ KEYc);.. 
        $ keylength = strlen ($ cryptkey); 
        $ String = base64_decode (substr ($ String , $ ckeyLength)); 
        $ StringLength = strlen ($ String ); 

        $ rndkey= array();
        for($i = 0; $i <= 255; $i++) {
            $rndkey[$i] = ord($cryptkey[$i % $keyLength]);
        }

        $box = range(0, 255);
        // 打乱密匙簿,增加随机性
        for($j = $i = 0; $i < 256; $i++) {
            $j = ($j + $box[$i] + $rndkey[$i]) % 256;
            $tmp = $box[$i];
            $box[$i] = $box[$j];
            $box[$j] = $ Tmp; 
        } 
        // encryption and decryption key derived from the key XOR book, then into the character 
        $ Result = '' ;
         for ($ = $ A $ I = J = 0 ; $ I <$ StringLength ; $ I ++ ) { 
            $ A = ($ A + . 1 )% 256 ; 
            $ J = ($ J + $ Box [$ A])% 256 ; 
            $ tmp = $ Box [$ A]; 
            $ Box [$ A] = $ Box [$ J]; 
            $ Box [$ J] = $ tmp; 
            . $ Result = CHR (the ord ($ String [$ I]) ^ ($ Box [($ Box [$ A] + $ Box [$ J])% 256]));
        }
        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0)
            && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)
        ) {
            return substr($result, 26);
        } else {
            return '';
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/sunlong88/p/11230822.html