Heim  >  Artikel  >  Backend-Entwicklung  >  PHP的加密解密字符串函数_PHP教程

PHP的加密解密字符串函数_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:42:25912Durchsuche

程序中经常使用的PHP加密解密字符串函数

代码如下:

<span /*</span><span ********************************************************************
    函数名称:encrypt
    函数作用:加密解密字符串
    使用方法:
    加密     :encrypt('str','E','nowamagic');
    解密     :encrypt('被加密过的字符串','D','nowamagic');
    参数说明:
    $string   :需要加密解密的字符串
    $operation:判断是加密还是解密:E:加密   D:解密
    $key      :加密的钥匙(密匙);
    
http://www.cnblogs.com/roucheng/
********************************************************************</span><span */</span>
    <span function</span> encrypt(<span $string</span>,<span $operation</span>,<span $key</span>=''<span )
    {
        </span><span $key</span>=<span md5</span>(<span $key</span><span );
        </span><span $key_length</span>=<span strlen</span>(<span $key</span><span );
        </span><span $string</span>=<span $operation</span>=='D'?<span base64_decode</span>(<span $string</span>):<span substr</span>(<span md5</span>(<span $string</span>.<span $key</span>),0,8).<span $string</span><span ;
        </span><span $string_length</span>=<span strlen</span>(<span $string</span><span );
        </span><span $rndkey</span>=<span $box</span>=<span array</span><span ();
        </span><span $result</span>=''<span ;
        </span><span for</span>(<span $i</span>=0;<span $i</span><=255;<span $i</span>++<span )
        {
            </span><span $rndkey</span>[<span $i</span>]=<span ord</span>(<span $key</span>[<span $i</span>%<span $key_length</span><span ]);
            </span><span $box</span>[<span $i</span>]=<span $i</span><span ;
        }
        </span><span for</span>(<span $j</span>=<span $i</span>=0;<span $i</span><256;<span $i</span>++<span )
        {
            </span><span $j</span>=(<span $j</span>+<span $box</span>[<span $i</span>]+<span $rndkey</span>[<span $i</span>])%256<span ;
            </span><span $tmp</span>=<span $box</span>[<span $i</span><span ];
            </span><span $box</span>[<span $i</span>]=<span $box</span>[<span $j</span><span ];
            </span><span $box</span>[<span $j</span>]=<span $tmp</span><span ;
        }
        </span><span for</span>(<span $a</span>=<span $j</span>=<span $i</span>=0;<span $i</span><<span $string_length</span>;<span $i</span>++<span )
        {
            </span><span $a</span>=(<span $a</span>+1)%256<span ;
            </span><span $j</span>=(<span $j</span>+<span $box</span>[<span $a</span>])%256<span ;
            </span><span $tmp</span>=<span $box</span>[<span $a</span><span ];
            </span><span $box</span>[<span $a</span>]=<span $box</span>[<span $j</span><span ];
            </span><span $box</span>[<span $j</span>]=<span $tmp</span><span ;
            </span><span $result</span>.=<span chr</span>(<span ord</span>(<span $string</span>[<span $i</span>])^(<span $box</span>[(<span $box</span>[<span $a</span>]+<span $box</span>[<span $j</span>])%256<span ]));
        }
        </span><span if</span>(<span $operation</span>=='D'<span )
        {
            </span><span if</span>(<span substr</span>(<span $result</span>,0,8)==<span substr</span>(<span md5</span>(<span substr</span>(<span $result</span>,8).<span $key</span>),0,8<span ))
            {
                </span><span return</span> <span substr</span>(<span $result</span>,8<span );
            }
            </span><span else</span><span 
            {
                </span><span return</span>''<span ;
            }
        }
        </span><span else</span><span 
        {
            </span><span return</span> <span str_replace</span>('=','',<span base64_encode</span>(<span $result</span><span ));
        }
    }</span>

 

使用方法:

<span $id</span> = 132<span ;
    
</span><span $token</span> = encrypt(<span $id</span>, 'E', 'a'<span );
    
</span><span echo</span> '加密:'.encrypt(<span $id</span>, 'E', 'a'<span );
</span><span echo</span> '<br />'<span ;
    
</span><span echo</span> '解密:'.encrypt(<span $token</span>, 'D', 'a');

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/647261.htmlTechArticle程序中经常使用的PHP加密解密字符串函数 代码如下: encrypt( , , ='' = ( = ( = =='D'? ( ): ( ( . ),0,8). = ( = = ='' ( =0; =255; ++ [ ]= ( [ % [ ]= ( = =0; 256;...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn