The content of this article is about the analysis of AES encrypted files in PHP (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
AES Introduction
Advanced Encryption Standard (AES, Advanced Encryption Standard) is the most common symmetric encryption algorithm (WeChat applet encrypted transmission uses this encryption algorithm) . Symmetric encryption algorithms use the same key for encryption and decryption.
Symmetric encryption
The keys used for encryption and decryption are the same. This encryption method is very fast and suitable for occasions where data is frequently sent. The disadvantage is that the transmission of the key is more troublesome. Secret keys are easily leaked.
Asymmetric encryption
The keys used for encryption and decryption are different. This encryption method is constructed using difficult mathematical problems. Usually the speed of encryption and decryption is relatively small. Slow, suitable for occasionally sending data. The advantage is that key transmission is convenient. Common asymmetric encryption algorithms are RSA, ECC and EIGamal.
Note:
PHP7.2 has deleted the Mcrypt extension, and the OpenSSL extension is used here.
<?php /* * AES 算法 */class Aes { private $hex_iv = '00000000000000000000000000000000'; private $key = '397e2eb61307109f6e68006ebcb62f98'; function __construct($key) { $this->key = $key; $this->key = hash('sha256', $this->key, true); } /* * 字符串加密 不写入文件 */ public function encrypt($input) { $data = openssl_encrypt($input, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->hexToStr($this->hex_iv)); $data = base64_encode($data); return $data; } /* * aes 给PHP文件加密 * 写入设置文件 */ public function filecrypt($filename) { $type=strtolower(substr(strrchr($filename,'.'),1)); if ('php' == $type && is_file($filename) && is_writable($filename)) { $contents = file_get_contents($filename); // echo $contents;exit; $contents = php_strip_whitespace($filename); // echo $contents;exit; // $headerPos = strpos($contents,'<?php'); // echo $headerPos;exit; // $contents = substr($contents, $headerPos + 5, $footerPos - $headerPos); // echo $contents; exit; $data = openssl_encrypt($contents, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->hexToStr($this->hex_iv)); // echo $data;exit; $data = base64_encode($data); // echo $data;exit; return file_put_contents($filename, $data); } return false; } /* * 字符串解密 */ public function decrypt($input) { $decrypted = openssl_decrypt(base64_decode($input), 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->hexToStr($this->hex_iv)); return $decrypted; } /* For PKCS7 padding */ private function addpadding($string, $blocksize = 16) { $len = strlen($string); $pad = $blocksize - ($len % $blocksize); $string .= str_repeat(chr($pad), $pad); return $string; } private function strippadding($string) { $slast = ord(substr($string, -1)); $slastc = chr($slast); $pcheck = substr($string, -$slast); if (preg_match("/$slastc{" . $slast . "}/", $string)) { $string = substr($string, 0, strlen($string) - $slast); return $string; } else { return false; } } function hexToStr($hex) { $string=''; for ($i=0; $i < strlen($hex)-1; $i+=2) { $string .= chr(hexdec($hex[$i].$hex[$i+1])); } return $string; } } $key = '397e2eb61307109f6e68006ebcb62f98'; $aes = new Aes($key); $filename = __DIR__.'\exchange.php'; // $filename = 'Y6RCuF6ETPC5J57hfhxovg=='; // 加密 $string = $aes->filecrypt($filename); // echo $string; echo "OK,加密完成!" ;
2. Simple function to encrypt PHP files
<?php function encode_file_contents($filename) { $type=strtolower(substr(strrchr($filename,'.'),1)); if ('php' == $type && is_file($filename) && is_writable($filename)) { // 如果是PHP文件 并且可写 则进行压缩编码 $contents = file_get_contents($filename); // 判断文件是否已经被编码处理 $contents = php_strip_whitespace($filename); // 去除PHP头部和尾部标识 $headerPos = strpos($contents,'<?php'); $footerPos = strrpos($contents,'?>'); $contents = substr($contents, $headerPos + 5, $footerPos - $headerPos); $encode = base64_encode(gzdeflate($contents)); // 开始编码 $encode = '<?php'."\n eval(gzinflate(base64_decode("."'".$encode."'".")));\n\n?>"; return file_put_contents($filename, $encode); } return false; } //调用函数 // echo __DIR__.'\server.php'; $filename = __DIR__.'\server.php'; encode_file_contents($filename); echo "OK,加密完成!" ;
Recommended related articles:
Request codes for post mode and get mode in curl of php
How to convert json object to array in thinkphp5 (code)
The above is the detailed content of Parsing of AES encrypted files in PHP (with code). For more information, please follow other related articles on the PHP Chinese website!