php 可逆加密方法
作者:jao 发布于:2012-12-17 22:31 分类:PHP教程
01 |
<? |
02 |
03 |
class encryptCalss |
04 |
{ |
05 |
var $key =12; |
06 |
function encode( $txt ){ |
07 |
for ( $i =0; $i < strlen ( $txt ); $i ++){ |
08 |
$txt [ $i ]= chr (ord( $txt [ $i ])+ $this ->key); |
09 |
} |
10 |
return $txt =urlencode( base64_encode (urlencode( $txt ))); |
11 |
} |
12 |
function decode( $txt ){ |
13 |
$txt =urldecode( base64_decode ( $txt )); |
14 |
for ( $i =0; $i < strlen ( $txt ); $i ++){ |
15 |
$txt [ $i ]= chr (ord( $txt [ $i ])- $this ->key); |
16 |
} |
17 |
return $txt ; |
18 |
} |
19 |
} |
20 |
21 |
?> |
[代码] discuz加密解密
01 |
<?php |
02 |
/** |
03 |
* |
04 |
* @param string $string 原文或者密文 |
05 |
* @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE |
06 |
* @param string $key 密钥 |
07 |
* @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效 |
08 |
* @return string 处理后的 原文或者 经过 base64_encode 处理后的密文 |
09 |
* @example |
10 |
* $a = authcode('abc', 'ENCODE', 'key'); |
11 |
* $b = authcode($a, 'DECODE', 'key'); // $b(abc) |
12 |
* |
13 |
* $a = authcode('abc', 'ENCODE', 'key', 3600); |
14 |
* $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为空 |
15 |
*/ |
16 |
function authcode( $string , $operation = 'DECODE' , $key = '' , $expiry =0){ |
17 |
18 |
$ckey_length =4; |
19 |
20 |
$key =md5( $key ? $key :\"kalvin.cn\"); |
21 |
$keya =md5( substr ( $key ,0,16)); |
22 |
$keyb =md5( substr ( $key ,16,16)); |
23 |
$keyc = $ckey_length ? ( $operation == 'DECODE' ? substr ( $string ,0, $ckey_length ): substr (md5(microtime()),- $ckey_length )): '' ; |
24 |
25 |
$cryptkey = $keya .md5( $keya . $keyc ); |
26 |
$key_length = strlen ( $cryptkey ); |
27 |
28 |
$string = $operation == 'DECODE' ? base64_decode ( substr ( $string , $ckey_length )):sprintf( '%010d' , $expiry ? $expiry +time():0). substr (md5( $string . $keyb ),0,16). $string ; |
29 |
$string_length = strlen ( $string ); |
30 |
31 |
$result = '' ; |
32 |
$box =range(0,255); |
33 |
34 |
$rndkey = array (); |
35 |
for ( $i =0; $i <=255; $i ++){ |
36 |
$rndkey [ $i ]=ord( $cryptkey [ $i % $key_length ]); |
37 |
} |
38 |
39 |
for ( $j = $i =0; $i <256; $i ++){ |
40 |
$j =( $j + $box [ $i ]+ $rndkey [ $i ])%256; |
41 |
$tmp = $box [ $i ]; |
42 |
$box [ $i ]= $box [ $j ]; |
43 |
$box [ $j ]= $tmp ; |
44 |
} |
45 |
46 |
for ( $a = $j = $i =0; $i < $string_length ; $i ++){ |
47 |
$a =( $a +1)%256; |
48 |
$j =( $j + $box [ $a ])%256; |
49 |
$tmp = $box [ $a ]; |
50 |
$box [ $a ]= $box [ $j ]; |
51 |
$box [ $j ]= $tmp ; |
52 |
$result .= chr (ord( $string [ $i ]) ^ ( $box [( $box [ $a ]+ $box [ $j ])%256])); |
53 |
} |
54 |
55 |
if ( $operation == 'DECODE' ){ |
56 |
if (( substr ( $result ,0,10)==0|| substr ( $result ,0,10)-time()>0)&& substr ( $result ,10,16)== substr (md5( substr ( $result ,26). $keyb ),0,16)){ |
57 |
returnsubstr( $result ,26); |
58 |
} else { |
59 |
return '' ; |
60 |
} |
61 |
} else { |
62 |
return $keyc . str_replace ( '=' , '' , base64_encode ( $result )); |
63 |
} |
64 |
65 |
} |
66 |
?> |
我的主页统计
- 建站日期:2012-11-26