Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example code explanation of PHP encryption and decryption function

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the "example code explanation of PHP encryption and decryption function". In daily operation, I believe that many people have doubts about the example code explanation of PHP encryption and decryption function. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "example code explanation of PHP encryption and decryption function". Next, please follow the editor to study!

In the project, sometimes we need to use PHP to encrypt specific information, that is, through the encryption algorithm to generate an encrypted string, the encrypted string can be decrypted by the decryption algorithm to facilitate the program to deal with the decrypted information.

The most common applications are in user login and some API data exchange scenarios.

The author included some classic PHP encryption and decryption function code to share with you. The principle of encryption and decryption is generally through a certain encryption and decryption algorithm, the key is added to the algorithm, and finally get the encryption and decryption results.

1. Very powerful authcode encryption function, Discuz! Classic code (with detailed explanation):

The copy code is as follows:

Function authcode ($string, $operation = 'DECODE', $key ='', $expiry = 0) {

/ / dynamic key length. The same plaintext will generate different ciphertexts depending on the dynamic key.

$ckey_length = 4

/ / key

$key = md5 ($key? $key: $GLOBALS ['discuz_auth_key'])

/ / key a will participate in encryption and decryption

$keya = md5 (substr ($key, 0,16))

/ / key b will be used for data integrity verification

$keyb = md5 (substr ($key, 16,16))

/ / key c is used to change the generated ciphertext

$keyc = $ckey_length? ($operation = = 'DECODE'? Substr ($string, 0, $ckey_length):

Substr (md5 (microtime ()),-$ckey_length):''

/ / the key participating in the operation

$cryptkey = $keya.md5 ($keya.$keyc)

$key_length = strlen ($cryptkey)

/ / plaintext, the first 10 bits are used to save the timestamp, verify the validity of the data during decryption, and 10 to 26 bits are used to save $keyb (key b)

/ / this key will be used to verify data integrity during decryption.

/ / if it is decoding, it starts with the $ckey_length bit, because the first $ckey_length bit of the ciphertext holds the dynamic key to ensure the correct decryption.

$string = $operation = = 'DECODE'? Base64_decode (substr ($string, $ckey_length)):

Sprintf ('0dink, $expiry? $expiry + time (): 0) .substr (md5 ($string.$keyb), 0,16). $string

$string_length = strlen ($string)

$result =''

$box = range (0,255)

$rndkey = array ()

/ / generate a keybook

For ($I = 0; $I 0) & &

Substr ($result, 10,16) = = substr (md5 (substr ($result, 26). $keyb), 0,16)) {

Return substr ($result, 26)

} else {

Return''

}

} else {

/ / keep the dynamic key in the ciphertext, which is why the same plaintext can be decrypted after producing different ciphertexts.

/ / because the encrypted ciphertext may be some special characters, the copying process may be lost, so base64 encoding is used.

Return $keyc.str_replace ('=','', base64_encode ($result))

}

}

$string in the function authcode ($string, $operation, $key, $expiry): string, plaintext or ciphertext; $operation:DECODE for decryption, others for encryption; $key: key; $expiry: ciphertext validity.

Usage:

The copy code is as follows:

$str = 'abcdef'

$key = 'www.helloweba.com'

Echo authcode ($str,'ENCODE',$key,0); / / encryption

$str = '56f4yER1DI2WTzWMqsfPpS9hwyoJnFP2MpC8SOhRrxO7BOk'

Echo authcode ($str,'DECODE',$key,0); / / decrypt

2. Encryption and decryption function encrypt ():

The copy code is as follows:

Function encrypt ($string,$operation,$key='') {

$key=md5 ($key)

$key_length=strlen ($key)

$string=$operation=='D'?base64_decode ($string): substr (md5 ($string.$key), 0pr 8). $string

$string_length=strlen ($string)

$rndkey=$box=array ()

$result=''

For ($iSuppli

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report