您现在的位置是:网站首页>后端技术>PHP教程PHP教程

PHP 字段加密解密通常用于key

神夜2020-07-16 22:16:08PHP教程1382人已围观文章来源:神夜个人博客

简介

PHP 字段加密解密通常用于key

<?php 
//改进后的算法 
//加密函数 
function lock_url($txt,$key='str'){ 
    $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 = ''; 
    $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='str'){ 
    $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 = ''; 
    $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); 
} 
 
?>

站点信息

  • 建站时间:2017-10-24
  • 网站程序:Hsycms 3.0
  • 文章统计:288条
  • 微信公众号:扫描二维码,关注我们
分享按钮