php加密与解密函数(不支持中文)

发布时间:2020-03-23编辑:脚本学堂
php加密与解密函数(不支持中文),有用到的朋友可以作个参考。

php加密与解密函数(不支持中文),有用到的朋友可以作个参考。
 

复制代码 代码如下:
<?php 
    /*
    @名称:PHP加密/解密 
    @link:www.jb200.com
    @date:2013/2/28
    */ 
    function phpencode($code) { 
        $code = str_replace(array('<?php','?>','<?PHP'),array('','',''),$code); 
        $encode = base64_encode(gzdeflate($code)); // 开始编码 
        $encode = '<?php'."neval(gzinflate(base64_decode("."'".$encode."'".")));n?>"; 
        return $encode; 
    } 
     
    function phpdecode($code) { 
        $code = str_replace(array('<?php','<?PHP',"eval(gzinflate(base64_decode('","')));",'?>'),array('','','','','',''),$code); 
        $decode = base64_decode($code); 
        $decode = @gzinflate($decode); 
        return $decode; 
    } 
    ?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>PHP加密/解密</title> 
    <style type="text/css" media="all"> 
        html, body { 
            margin: 0;padding: 0;  
        } 
         
        body { 
            color: #333; 
            font: 12px Tahoma,Lucida Grande, sans-serif; 
            margin: 9%; 
        } 
         
        a { 
            color: #0055CC;  
        } 
         
        img { 
            border: 0px solid #CCC; 
        } 
         
        h1 { 
            margin: 0; 
        } 
         
        h3 { 
            color: #555; 
            font-size: 1.6em; 
            font-weight: normal; 
            margin: 0;  
        } 
         
        pre { 
            color: #0055CC; 
            font-size: 1.1em; 
            line-height: 1.2; 
            margin: 0.25em 0;  
        } 
         
        p { 
            margin: 0.65em 0; 
        } 
         
        #ads { 
            border-left: 1px solid #eee; 
            float: right; 
            margin: 0 0 2em 2.5em; 
            padding-left: 3px; 
            width: 160px; 
        } 
         
        #source { 
            margin-bottom: 2.5em;  
        } 
         
        pre { 
            overflow: auto; 
            padding:1em 0;  
        } 
         
        h2 { 
            position: relative; 
            top: 0.5em; 
        } 
    </style> 
    </head>     
    <body>     
        <h3>PHP加密/解密</h3> 
        <form method="post"> 
            <textarea name="source" cols="55" rows="8"> 
            <?php 
            if(!emptyempty($_POST['source'])) { 
                if($_POST['button']=='加密') { 
                    echo htmlspecialchars(phpencode(stripcslashes($_POST['source']))); 
                } 
                if($_POST['button']=='解密') { 
                    echo htmlspecialchars(phpdecode(stripcslashes($_POST['source']))); 
                } 
            } 
            ?> 
            </textarea>    
            <?php 
            if(!emptyempty($_POST['source'])){ 
                if($_POST['button']=='加密') { 
                    echo '<br /><br />加密成功.'; 
                } 
                if($_POST['button']=='解密') { 
                    echo '<br /><br />解密成功.'; 
                } 
            }else{ 
                echo '<br /><br />利用 base64+gzinflate 对您的PHP代码进行压缩,可以一定程度上保护您的代码版权和减小代码的体积。'; 
            } 
            ?>     
            <br /><br /> 
            <input type="submit" name="button" value="加密"> 
            <input type="submit" name="button" value="解密"> 
        </form>
    </body> 
    </html>