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

WBOY
풀어 주다: 2016-07-13 10:42:25
원래의
903명이 탐색했습니다.

程序中经常使用的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;...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿