gpt4 book ai didi

php实现的三个常用加密解密功能函数示例

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章php实现的三个常用加密解密功能函数示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了php实现的三个常用加密解密功能函数。分享给大家供大家参考,具体如下:

算法一:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//加密函数
function lock_url( $txt , $key = 'www.zzvips.com' )
{
   $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.zzvips.com' )
{
   $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 );
}

用法:

?
1
2
3
4
$str = "我" ;
$pwd = lock_url( $str );
echo "加密之后:" . $pwd . "<br/>" ;
echo "解密还原:" .unlock_url( $pwd );

运行结果:

php实现的三个常用加密解密功能函数示例

算法二:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
function passport_encrypt( $txt , $key = 'www.zzvips.com' )
{
   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.zzvips.com' )
{
   $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 ;
}
?>

用法:

?
1
2
3
4
5
6
7
8
<?php
$txt = "1" ;
$key = "testkey" ;
$encrypt = passport_encrypt( $txt , $key );
$decrypt = passport_decrypt( $encrypt , $key );
echo $encrypt . "<br>" ;
echo $decrypt . "<br>" ;
?>

运行结果:

php实现的三个常用加密解密功能函数示例

算法三(改进第一个加密之后的算法) 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//加密函数
function lock_url( $txt , $key = 'www.zzvips.com' )
{
   $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.zzvips.com' )
{
   $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 );
}

用法:

?
1
2
3
4
$str = "我" ;
$pwd = lock_url( $str );
echo "加密之后:" . $pwd . "<br/>" ;
echo "解密还原:" .unlock_url( $pwd );

运行结果:

php实现的三个常用加密解密功能函数示例

ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:

文字在线加密解密工具(包含aes、des、rc4等):https://tool.zzvips.com/t/txtencode/ 。

md5在线加密工具:https://tool.zzvips.com/t/md5/ 。

在线sha1/sha224/sha256/sha384/sha512加密工具:https://tool.zzvips.com/t/sha/ 。

希望本文所述对大家php程序设计有所帮助.

原文链接:http://www.cnblogs.com/zqifa/p/php-8.html 。

最后此篇关于php实现的三个常用加密解密功能函数示例的文章就讲到这里了,如果你想了解更多关于php实现的三个常用加密解密功能函数示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com