1. 程式人生 > >php實現的三個常用加密解密功能函數示例

php實現的三個常用加密解密功能函數示例

htm 常用 zab rand unlock ret == UNC test

目錄

  • 算法一:
  • 算法二:
  • 算法三(改進第一個加密之後的算法)

本文實例講述了php實現的三個常用加密解密功能函數。分享給大家供大家參考,具體如下:

算法一:

//加密函數
function lock_url($txt,$key=‘www.jb51.net‘)
{
  $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($ch.$tmp);
}
//解密函數
function unlock_url($txt,$key=‘www.jb51.net‘)
{
  $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 = ‘‘;
  $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);
}

用法:

$str="腳本之家";
$pwd = lock_url($str);
echo "加密之後:".$pwd."<br/>";
echo "解密還原:".unlock_url($pwd);

運行結果:

加密之後:7MI21MNFgbrlr
解密之後:腳本之家

算法二:

<?php
function passport_encrypt($txt, $key = ‘www.jb51.net‘) 
{ 
  srand((double)microtime() * 1000000); 
  $encrypt_key = md5(rand(0, 32000)); 
  $ctr = 0; 
  $tmp = ‘‘; 
  for($i = 0;$i < strlen($txt); $i++) { 
  $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr; 
  $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]); 
  } 
  return urlencode(base64_encode(passport_key($tmp, $key))); 
} 
function passport_decrypt($txt, $key = ‘www.jb51.net‘) 
{ 
  $txt = passport_key(base64_decode(urldecode($txt)), $key); 
  $tmp = ‘‘; 
  for($i = 0;$i < strlen($txt); $i++) { 
  $md5 = $txt[$i]; 
  $tmp .= $txt[++$i] ^ $md5; 
  } 
  return $tmp; 
} 
function passport_key($txt, $encrypt_key) 
{ 
  $encrypt_key = md5($encrypt_key); 
  $ctr = 0; 
  $tmp = ‘‘; 
  for($i = 0; $i < strlen($txt); $i++) { 
  $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr; 
  $tmp .= $txt[$i] ^ $encrypt_key[$ctr++]; 
  } 
  return $tmp; 
} 
?>

用法:

<?php
$txt = "1";
$key = "testkey";
$encrypt = passport_encrypt($txt,$key);
$decrypt = passport_decrypt($encrypt,$key);
echo $encrypt."<br>";
echo $decrypt."<br>";
?>

運行結果:

ADE%3D
1

算法三(改進第一個加密之後的算法)

//加密函數
function lock_url($txt,$key=‘www.jb51.net‘)
{
  $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=‘www.jb51.net‘)
{
  $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);
}

用法

$str="腳本之家";
$pwd = lock_url($str);
echo "加密之後:".$pwd."<br/>";
echo "解密還原:".unlock_url($pwd);

運行結果:

加密之後:clNBeVRPSS04VHhzYj0otMHNadjJWUTR5ME4%3D
解密之後:腳本之家

php實現的三個常用加密解密功能函數示例