Home php教程 php手册 迅速学会PHP加密解密技巧

迅速学会PHP加密解密技巧

Jun 13, 2016 am 11:11 AM
php for use encrypt and decode reason learn us Skill of detailed fast

我们在这里为大家详细介绍了有关

使用这些PHP加密解密的原因是因为有时自己的URL地址被人获取以后想破解你里面传值的内容就必须知道你的key,没有key,他应该要破了一阵子才能知道你URL里面的内容吧...

闲话少说,先将它们打包成一个文件就叫fun.php吧

  1.  ?php  
  2. function passport_encrypt($txt, $key) {  
  3. srand((double)microtime() * 1000000);  
  4. $encrypt_key = md5(rand(0, 32000));  
  5. $ctr = 0;  
  6. $tmp = '';  
  7. for($i = 0;$i  strlen($txt); $i++) {  
  8. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;  
  9. $tmp .= $encrypt_key[$ctr].($txt[$i]
     ^ $encrypt_key[$ctr++]);  
  10. }  
  11. return base64_encode(passport_key($tmp, $key));  
  12. }  
  13. function passport_decrypt($txt, $key) {  
  14. $txt = passport_key(base64_decode($txt), $key);  
  15. $tmp = '';  
  16. for($i = 0;$i  strlen($txt); $i++) {  
  17. $md5 = $txt[$i];  
  18. $tmp .= $txt[++$i] ^ $md5;  
  19. }  
  20. return $tmp;  
  21. }  
  22. function passport_key($txt, $encrypt_key) {  
  23. $encrypt_key = md5($encrypt_key);  
  24. $ctr = 0;  
  25. $tmp = '';  
  26. for($i = 0; $i  strlen($txt); $i++) {  
  27. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;  
  28. $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];  
  29. }  
  30. return $tmp;  
  31. }  
  32. ?> 

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

<ol class="dp-xml">
<li class="alt"><span><span>//string.php  </span></span></li>
<li><span class="tag"><span> ?php  </span></span></li>
<li class="alt"><span>include “fun.php”;  </span></li>
<li>
<span>$</span><span class="attribute">txt</span><span> = “This is a test”;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">key</span><span> = “testkey”;  </span>
</li>
<li>
<span>$</span><span class="attribute">encrypt</span><span> = </span><span class="attribute-value">passport_encrypt</span><span>($txt,$key);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">decrypt</span><span> = </span><span class="attribute-value">passport_decrypt</span><span>($encrypt,$key);  </span>
</li>
<li>
<span>echo $txt.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li class="alt">
<span>echo $encrypt.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li>
<span>echo $decrypt.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
<li><span>//array.php  </span></li>
<li class="alt"><span class="tag"><span> ?php  </span></span></li>
<li><span>include “fun.php”;  </span></li>
<li class="alt">
<span>$</span><span class="attribute">array</span><span class="attribute-value">array</span><span> = array(  </span>
</li>
<li>
<span>"a" =</span><span class="tag">></span><span> "1",  </span>
</li>
<li class="alt">
<span>"b" =</span><span class="tag">></span><span> "2",  </span>
</li>
<li>
<span>"c" =</span><span class="tag">></span><span> "3",  </span>
</li>
<li class="alt">
<span>"d" =</span><span class="tag">></span><span> "4"  </span>
</li>
<li><span>);  </span></li>
<li class="alt"><span>//serialize产生一个可存储的值,<br>返回一个字符串,unserialize还原  </span></li>
<li>
<span>$</span><span class="attribute">txt</span><span> = </span><span class="attribute-value">serialize</span><span>($array);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">key</span><span> = “testkey”;  </span>
</li>
<li>
<span>$</span><span class="attribute">encrypt</span><span> = </span><span class="attribute-value">passport_encrypt</span><span>($txt,$key);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">decrypt</span><span> = </span><span class="attribute-value">passport_decrypt</span><span>($encrypt,$key);  </span>
</li>
<li>
<span>$</span><span class="attribute">decryptArray</span><span> = </span><span class="attribute-value">unserialize</span><span>($decrypt);  </span>
</li>
<li class="alt">
<span>echo $txt.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li>
<span>echo $encrypt.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li class="alt">
<span>echo $decrypt.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li>
<span>echo $decryptArray.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

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

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

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

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

<ol class="dp-xml">
<li class="alt"><span><span>//login.php  </span></span></li>
<li><span class="tag"><span> ?php  </span></span></li>
<li class="alt"><span>session_start();  </span></li>
<li><span>include “fun.php”;  </span></li>
<li class="alt"><span>….  </span></li>
<li><span>$_SESSION[“userid”];  </span></li>
<li class="alt"><span>$_SESSION[“username”];  </span></li>
<li><span>$_SESSION[“userpwd”];  </span></li>
<li class="alt"><span>…  </span></li>
<li>
<span>header("Location: http:<br>//$domain/process.php?</span><span class="attribute">s</span><span>="<br>.urlencode(passport_encrypt<br>(serialize($_SESSION),"</span><span class="attribute-value">sessionkey</span><span>")));  </span>
</li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

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

<ol class="dp-xml">
<li class="alt"><span><span>//process.php  </span></span></li>
<li><span class="tag"><span> ?php  </span></span></li>
<li class="alt"><span>session_start();  </span></li>
<li><span>include “fun.php”;  </span></li>
<li class="alt">
<span>$</span><span class="attribute">_SESSION</span><span>=</span><span class="attribute-value">unserialize</span><span>(passport<br>_decrypt($_GET["s"],"sessionkey"));  </span>
</li>
<li><span>header("Location: http://$domain/index.php");  </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

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

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

以下是加强版的process.php

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li><span>session_start();  </span></li>
<li class="alt"><span>include_once "fun.php";  </span></li>
<li>
<span>$</span><span class="attribute">_SESSION</span><span>=</span><span class="attribute-value">unserialize</span><span>(passport_<br>decrypt($_GET["s"],"sessionkey"));  </span>
</li>
<li class="alt">
<span>if((time()-$_SESSION["TIME"])</span><span class="tag">></span><span>30){  </span>
</li>
<li><span>header("Location: http://<br>$domain/ login.php");  </span></li>
<li class="alt"><span>unset($_SESSION["USERNAME"]);  </span></li>
<li><span>unset($_SESSION["PASSWORD"]);  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>else  </span></li>
<li class="alt"><span>header("Location: http://<br>$domain/ index.php");  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

写这个PHP加密解密文件之前,你还要在登录那边设置

$_SESSION["TIME"] = time();

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

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


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles