Home > Backend Development > PHP Tutorial > 2 more classic PHP encryption and decryption functions

2 more classic PHP encryption and decryption functions

不言
Release: 2023-03-23 06:22:01
Original
1889 people have browsed it


Reprint: https://www.cnblogs.com/wicub/p/6395349.html



This article mainly introduces the sharing of two more classic PHP encryption and decryption functions, one is Discuz!'s authcode encryption function (with detailed decomposition), and the other is the encrypt() function, both of which are relatively classic, friends in need You can refer to the following

Sometimes in the project we need to use PHP to encrypt specific information, that is, an encrypted string is generated through the encryption algorithm. This encrypted string can be decrypted through the decryption algorithm Decrypt to facilitate the program to process the decrypted information.
The most common applications are in user login and some API data exchange scenarios.

The author has collected some classic PHP encryption and decryption function codes to share with you. The principle of encryption and decryption is generally to use a certain encryption and decryption algorithm, add the key to the algorithm, and finally obtain the encryption and decryption results.
1. A very powerful authcode encryption function, Discuz! Classic code (with detailed explanation):


2 more classic PHP encryption and decryption functions

function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {   
    // 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙   
    $ckey_length = 4;   
       
    // 密匙   
    $key = md5($key ? $key : $GLOBALS['discuz_auth_key']);   
       
    // 密匙a会参与加解密   
    $keya = md5(substr($key, 0, 16));   
    // 密匙b会用来做数据完整性验证   
    $keyb = md5(substr($key, 16, 16));   
    // 密匙c用于变化生成的密文   
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): 
substr(md5(microtime()), -$ckey_length)) : '';   
    // 参与运算的密匙   
    $cryptkey = $keya.md5($keya.$keyc);   
    $key_length = strlen($cryptkey);   
    // 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b), 
//解密时会通过这个密匙验证数据完整性   
    // 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确   
    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) :  
sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;   
    $string_length = strlen($string);   
    $result = '';   
    $box = range(0, 255);   
    $rndkey = array();   
    // 产生密匙簿   
    for($i = 0; $i  0) &&  
substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {   
            return substr($result, 26);   
        } else {   
            return '';   
        }   
    } else {   
        // 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因   
        // 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码   
        return $keyc.str_replace('=', '', base64_encode($result));   
    }   
}
Copy after login

2 more classic PHP encryption and decryption functions

$string in function authcode($string, $operation, $key, $expiry): character String, plain text or cipher text; $operation: DECODE means decryption, others means encryption; $key: encryption key; $expiry: ciphertext validity period.

Usage:

$str = 'abcdef'; 
$key = 'www.helloweba.com'; 
echo authcode($str,'ENCODE',$key,0); //加密 $str = '56f4yER1DI2WTzWMqsfPpS9hwyoJnFP2MpC8SOhRrxO7BOk'; 
echo authcode($str,'DECODE',$key,0); //解密
Copy after login

2. Encryption and decryption function encrypt():


2 more classic PHP encryption and decryption functions

function encrypt($string,$operation,$key=''){ 
    $key=md5($key); 
    $key_length=strlen($key); 
      $string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string; 
    $string_length=strlen($string); 
    $rndkey=$box=array(); 
    $result=''; 
    for($i=0;$i<p class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><img src="https://img.php.cn/upload/article/000/153/291/9e43d35958ca6748e93ca3656ec369b0-0.gif" alt="2 more classic PHP encryption and decryption functions"></span></p>##Function encrypt($string,$operation,$key) Medium $string: the string that needs to be encrypted and decrypted; $operation: determine whether to encrypt or decrypt, E means encryption, D means decryption; $key: key. <p   style="max-width:90%"Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;text-align:left;background-color:rgb(245,245,245);">usage:<br></p><p class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);text-align:left;font-family:'Courier New';font-size:12px;"></p><pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';">$str = 'abc'; 
$key = 'www.helloweba.com'; 
$token = encrypt($str, 'E', $key); 
echo '加密:'.encrypt($str, 'E', $key); 
echo '解密:'.encrypt($str, 'D', $key);
Copy after login

相关推荐:

分享一个刚写的PHP加密解密函数 

PHP中加密解密函数与DES加密解密实例


The above is the detailed content of 2 more classic PHP encryption and decryption functions. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template