Home > php教程 > php手册 > body text

PHP加密解密内部算法

WBOY
Release: 2016-05-25 16:40:47
Original
1519 people have browsed it

<?php
function passport_encrypt($txt, $key) {
    srand((double)microtime() * 1000000);
    $encrypt_key = md5(rand(0, 32000));
    $ctr = 0;
    $tmp = &#39;&#39;;
    for ($i = 0; $i < strlen($txt); $i++) {
        $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
        $tmp.= $encrypt_key[$ctr] . ($txt[$i] ^ $encrypt_key[$ctr++]);
    }
    return base64_encode(passport_key($tmp, $key));
}
function passport_decrypt($txt, $key) {
    $txt = passport_key(base64_decode($txt) , $key);
    $tmp = &#39;&#39;;
    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 = &#39;&#39;;
    for ($i = 0; $i < strlen($txt); $i++) {
        $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
        $tmp.= $txt[$i] ^ $encrypt_key[$ctr++];
    }
    return $tmp;
}
?>
Copy after login

以下是一些示例…加深对这三个加密解密函数的理解…

//string.php 
<?php
include "fun.php";
$txt = "This is a test";
$key = "testkey";
$encrypt = passport_encrypt($txt, $key);
$decrypt = passport_decrypt($encrypt, $key);
echo $txt . "<br><hr>";
echo $encrypt . "<br><hr>";
echo $decrypt . "<br><hr>";
?> 
//array.php 
<?php
include "fun.php";
$array = array(
    "a" => "1",
    "b" => "2",
    "c" => "3",
    "d" => "4"
);
//serialize产生一个可存储的值,返回一个字符串,unserialize还原
$txt = serialize($array);
$key = "testkey";
$encrypt = passport_encrypt($txt, $key);
$decrypt = passport_decrypt($encrypt, $key);
$decryptArray = unserialize($decrypt);
//开源代码phprm.com
echo $txt . "<br><hr>";
echo $encrypt . "<br><hr>";
echo $decrypt . "<br><hr>";
echo $decryptArray . "<br><hr>";
?>
Copy after login

关键的地方来了…当你要跳转到另外一个网址,但又要保证你的session无误的时候,你需要对session作一个处理.貌似一个公司有一个网站又有一个论坛,两个地方都有注册和登录,但又不想让用户在主页登录后跳转到论坛的时候session失效,即是登录一次跑完整间公司…

那要怎样来处理用户的session呢…

网页都是无状态的,如果要在新的网页中继续使用session,则需要把session从一个地方移到另一个地方,可能有些人已经想到了,我可以通过url传址的方式来调用它….而PHP有个处理session的变量,叫$_SESSION.于是….

将需要注册的session转换成一个数组吧.那么,你可以这样写:

//login.php 
<?php
session_start();
include "fun.php";
… . $_SESSION["userid"];
$_SESSION["username"];
$_SESSION["userpwd"];
…header("Location: http://$domain/process.php?s=" . urlencode(passport_encrypt(serialize($_SESSION) , "sessionkey")));
?>
Copy after login

上例中先用serialize将$_SESSION变成可存储的数据,然后通过passport_encrypt将这个数据加密,加urlencode的原因是因为$_SESSION加密时,有可能会产生像料想不到的编码,所以以防万一…(事实证明非常有效)处理下先

//process.php 
<?php
session_start();
include "fun.php";
$_SESSION = unserialize(passport_decrypt($_GET["s"], "sessionkey"));
header("Location: http://$domain/index.php");
?>
Copy after login

先用$_GET["s"]获取URL的参数,然后用passport_decrypt将其解密,再用unserialize将其数据还原成原始数据,到了这步处理,你的网页就可能通过header自由跳转啦….

这种方法还涉及到安全性的问题,如果你的url地址在传址的过程中被人家获取的话,那就真的是不好意思了…人家虽然可能破解不了url里边的内容,但人家也可以直接用这个url地址来登录你的一些个人账户啊,邮箱帐户啊甚至银行帐户(当然很少人会这样写,我例外,哈哈)…听起来好怕….但其实你可以在跳转页面作取消session处理….

以下是加强版的process.php

<?php
session_start();
include_once "fun.php";
$_SESSION = unserialize(passport_decrypt($_GET["s"], "sessionkey"));
if ((time() - $_SESSION["TIME"]) > 30) {
    header("Location: http://$domain/ login.php");
    unset($_SESSION["USERNAME"]);
    unset($_SESSION["PASSWORD"]);
} else header("Location: http://$domain/ index.php");
?>
Copy after login

写这个文件之前,你还要在登录那边设置:

$_SESSION["TIME"] = time();
Copy after login

设置这个的原因主要是获取两边的时间,如果跳转的时候超过30秒的时候,你就可以让它跳转到login.php登录页面,网速慢的客户就不好意思啦…但这也预防了如果此url被人获取,而这个人又没有在30秒内登录的话,那就不好意思啊,超时重新登录.

$_SESSION["USERNAME"]和$_SESSION["PASSWORD"] 这两个东东就是用户登录时需要输入的用户名和密码了….取消这两个session的原因就是因为如果你的url被人获取了,那个人虽然在超过30秒内跳转到loign.php的页面,但那些传过来的session依然有效,只要将url后缀login.php改为index.php….那他一样登录成功…


本文地址:

转载随意,但请附上文章地址:-)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template