


Summary of examples of AES encryption and decryption in php_PHP tutorial
aesDemo.php:
Example,
require_once('./AES.php');
//$aes = new AES();
$aes = new AES(true);//Storage the encrypted string in hexadecimal format
//$aes = new AES (true,true);//With debugging information and the encrypted string is stored in hexadecimal
$key = "this is a 32 byte key";//Key
$keys = $aes- >makeKey($key);
$encode = "123456";// Encrypted string
$ct = $aes->encryptString($encode, $keys);
echo " encode = ".$ct."
";
$cpt = $aes->decryptString($ct, $keys);
echo "decode = ".$cpt;
? >
Example, AES encryption class
//php aes encryption class
class AESMcrypt {
public $iv = null;
public $key = null;
public $bit = 128;
private $cipher;
public function __construct($bit, $key, $iv, $mode) {
if(empty($bit) || empty($key) || empty($iv) || empty($mode ))
return NULL;
$this->bit = $bit;
$this->key = $key;
$this->iv = $iv;
$this->mode = $ mode;
switch($this->bit) {
case 192:$this->cipher = MCRYPT_RIJNDAEL_192; break;
case 256:$this->cipher = MCRYPT_RIJNDAEL_256; break;
default: $this->cipher = MCRYPT_RIJNDAEL_128;
}
switch($this->mode) {
case 'ecb':$this->mode = MCRYPT_MODE_ECB; break;
case 'cfb':$this->mode = MCRYPT_MODE_CFB; break ;
case 'ofb':$this->mode = MCRYPT_MODE_OFB; break;
case 'nofb':$this->mode = MCRYPT_MODE_NOFB; break;
default: $this->mode = MCRYPT_MODE_CBC;
}
}
public function encrypt($data) {
$data = base64_encode(mcrypt_encrypt( $this->cipher, $this->key, $data, $this->mode, $this-> iv));
return $data;
}
public function decrypt($data) {
$data = mcrypt_decrypt( $this->cipher, $this->key, base64_decode($data), $this->mode, $this-> ;iv);
$data = rtrim(rtrim($data), ".. ");
return $data;
}
}
//How to use
$aes = new AESMcrypt($bit = 128, $key = 'abcdef1234567890', $iv = '0987654321fedcba', $mode = 'cbc');
$c = $aes->encrypt('haowei.me');
var_dump($aes->decrypt($c));
Example, attached with an encryptable and decryptable class
/**
* AES encryption and decryption classes
* @author hushangming
*
* Usage:
*
<br> * // Instantiation class <br> * // Parameters $_bit: format, supports 256, 192, 128, the default is 128 bytes <br> * // Parameter $_type: encryption/decryption method, supports cfb, cbc, nofb, ofb, stream, ecb, the default is ecb<br> * // Parameter $_key: key, default is abcdefghijuklmno<br> * $tcaes = new TCAES(); <br> * $string = 'laohu';<br> * // Encryption<br> * $ encodeString = $tcaes->encode($string);<br> * // Decrypt<br> * $decodeString = $tcaes->decode($encodeString);<br> *
*/
class TCAES{
private $_bit = MCRYPT_RIJNDAEL_256;
private $_type = MCRYPT_MODE_CBC;
//private $_key = 'abcdefghijuklmno0123456789012345';
private $_key = 'abcdefghijuklmno'; // 密钥
private $_use_base64 = true;
private $_iv_size = null;
private $_iv = null;
/**
* @param string $_key key
* @param int $_bit uses 128 bytes by default
* @param string $_type encryption and decryption method
* @param boolean $_use_base64 uses base64 by default Encryption
*/
public function __construct($_key = '', $_bit = 128, $_type = 'ecb', $_use_base64 = true){
// 加密字节
if(192 === $_bit){
$this->_bit = MCRYPT_RIJNDAEL_192;
}elseif(128 === $_bit){
$this->_bit = MCRYPT_RIJNDAEL_128;
}else{
$this->_bit = MCRYPT_RIJNDAEL_256;
}
// 加密方法
if('cfb' === $_type){
$this->_type = MCRYPT_MODE_CFB;
}elseif('cbc' === $_type){
$this->_type = MCRYPT_MODE_CBC;
}elseif('nofb' === $_type){
$this->_type = MCRYPT_MODE_NOFB;
}elseif('ofb' === $_type){
$this->_type = MCRYPT_MODE_OFB;
}elseif('stream' === $_type){
$this->_type = MCRYPT_MODE_STREAM;
}else{
$this->_type = MCRYPT_MODE_ECB;
}
// 密钥
if(!empty($_key)){
$this->_key = $_key;
}
// 是否使用base64
$this->_use_base64 = $_use_base64;
$this->_iv_size = mcrypt_get_iv_size($this->_bit, $this->_type);
$this->_iv = mcrypt_create_iv($this->_iv_size, MCRYPT_RAND);
}
/**
* Encryption
* @param string $string String to be encrypted
* @return string
*/
public function encode($string){
if(MCRYPT_MODE_ECB === $this->_type){
$encodeString = mcrypt_encrypt($this->_bit, $this->_key, $string, $this->_type);
}else{
$encodeString = mcrypt_encrypt($this->_bit, $this->_key, $string, $this->_type, $this->_iv);
}
if($this->_use_base64)
$encodeString = base64_encode($encodeString);
return $encodeString;
}
/**
* Decryption
* @param string $string String to be decrypted
* @return string
*/
public function decode($string){
if($this->_use_base64)
$string = base64_decode($string);
$string = $this->toHexString($string);
if(MCRYPT_MODE_ECB === $this->_type){
$decodeString = mcrypt_decrypt($this->_bit, $this->_key, $string, $this->_type);
}else{
$decodeString = mcrypt_decrypt($this->_bit, $this->_key, $string, $this->_type, $this->_iv);
}
return $decodeString;
}
/**
* Convert $string to hexadecimal
* @param string $string
* @return stream
*/
private function toHexString ($string){
$buf = "";
for ($i = 0; $i $val = dechex(ord($string{$i}));
if(strlen($val) $val = "0".$val;
$buf .= $val;
}
return $buf;
}
/**
* Convert hexadecimal stream $string to string
* @param stream $string
* @return string
*/
private function fromHexString($string){
$buf = "";
for($i = 0; $i $val = chr(hexdec(substr($string, $i, 2)));
$buf .= $val;
}
return $buf;
}
}

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
