php加解密怎麼做

王林
發布: 2023-02-25 06:14:01
原創
2038 人瀏覽過

php加解密怎麼做

基於這幾個函數可逆轉的加密為:base64_encode()urlencode()相對應的解密函數:base64_decode()urldecode() ,範例如下:

1、第一種加密解密演算法

<?php  
function encryptDecrypt($key, $string, $decrypt){   
    if($decrypt){   
        $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12");   
        return $decrypted;   
    }else{   
        $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));   
        return $encrypted;   
    }   
}   

//加密:"z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk="  
echo encryptDecrypt(&#39;password&#39;, &#39;Helloweba欢迎您&#39;,0);   
//解密:"Helloweba欢迎您"  
echo encryptDecrypt(&#39;password&#39;, &#39;z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=&#39;,1);  
?>
登入後複製

2、第二種解密解密演算法

<?php  
//加密函数  
function lock_url($txt,$key=&#39;www.xxxx.com&#39;){  
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";  
    $nh = rand(0,64);  
    $ch = $chars[$nh];  
    $mdKey = md5($key.$ch);  
    $mdKey = substr($mdKey,$nh%8, $nh%8+7);  
    $txt = base64_encode($txt);  
    $tmp = &#39;&#39;;  
    $i=0;$j=0;$k = 0;  
    for ($i=0; $i<strlen($txt); $i++) {  
        $k = $k == strlen($mdKey) ? 0 : $k;  
        $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;  
        $tmp .= $chars[$j];  
    }  
    return urlencode($ch.$tmp);  
}  
//解密函数  
function unlock_url($txt,$key=&#39;www.xxxx.com&#39;){  
    $txt = urldecode($txt);  
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";  
    $ch = $txt[0];  
    $nh = strpos($chars,$ch);  
    $mdKey = md5($key.$ch);  
    $mdKey = substr($mdKey,$nh%8, $nh%8+7);  
    $txt = substr($txt,1);  
    $tmp = &#39;&#39;;  
    $i=0;$j=0; $k = 0;  
    for ($i=0; $i<strlen($txt); $i++) {  
        $k = $k == strlen($mdKey) ? 0 : $k;  
        $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);  
        while ($j<0) $j+=64;  
        $tmp .= $chars[$j];  
    }  
    return base64_decode($tmp);  
}  
?>
登入後複製

3、第三種加密解密演算法

<?php  

//改进后的算法  
//加密函数  
function lock_url($txt,$key=&#39;test&#39;){  
    $txt = $txt.$key;  
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";  
    $nh = rand(0,64);  
    $ch = $chars[$nh];  
    $mdKey = md5($key.$ch);  
    $mdKey = substr($mdKey,$nh%8, $nh%8+7);  
    $txt = base64_encode($txt);  
    $tmp = &#39;&#39;;  
    $i=0;$j=0;$k = 0;  
    for ($i=0; $i<strlen($txt); $i++) {  
        $k = $k == strlen($mdKey) ? 0 : $k;  
        $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;  
        $tmp .= $chars[$j];  
    }  
    return urlencode(base64_encode($ch.$tmp));  
}  
//解密函数  
function unlock_url($txt,$key=&#39;test&#39;){  
    $txt = base64_decode(urldecode($txt));  
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";  
    $ch = $txt[0];  
    $nh = strpos($chars,$ch);  
    $mdKey = md5($key.$ch);  
    $mdKey = substr($mdKey,$nh%8, $nh%8+7);  
    $txt = substr($txt,1);  
    $tmp = &#39;&#39;;  
    $i=0;$j=0; $k = 0;  
    for ($i=0; $i<strlen($txt); $i++) {  
        $k = $k == strlen($mdKey) ? 0 : $k;  
        $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);  
        while ($j<0) $j+=64;  
        $tmp .= $chars[$j];  
    }  
    return trim(base64_decode($tmp),$key);  
}  

?>
登入後複製

對於不同需要,可以使用不同加密解密演算法。

推薦教學:PHP影片教學

#

以上是php加解密怎麼做的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板