Home > Backend Development > PHP Tutorial > Parsing of AES encrypted files in PHP (with code)

Parsing of AES encrypted files in PHP (with code)

不言
Release: 2023-04-03 15:42:02
Original
4867 people have browsed it

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 = &#39;00000000000000000000000000000000&#39;; 

    private $key = &#39;397e2eb61307109f6e68006ebcb62f98&#39;;    
    function __construct($key) {
        $this->key = $key;        
        $this->key = hash(&#39;sha256&#39;, $this->key, true);
    }    /*
    * 字符串加密 不写入文件 
    */
    public function encrypt($input)
    {
        $data = openssl_encrypt($input, &#39;AES-256-CBC&#39;, $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,&#39;.&#39;),1));            
        if (&#39;php&#39; == $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,&#39;<?php&#39;);
                 // echo $headerPos;exit;


                 // $contents = substr($contents, $headerPos + 5, $footerPos - $headerPos);
                 // echo $contents;
                 exit;
                 $data = openssl_encrypt($contents, &#39;AES-256-CBC&#39;, $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), &#39;AES-256-CBC&#39;, $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=&#39;&#39;;        
        for ($i=0; $i < strlen($hex)-1; $i+=2)

        {            
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));

        }        
        return $string;
    }

}
$key = &#39;397e2eb61307109f6e68006ebcb62f98&#39;;
$aes = new Aes($key);
$filename = __DIR__.&#39;\exchange.php&#39;;
// $filename = &#39;Y6RCuF6ETPC5J57hfhxovg==&#39;;
// 加密
$string = $aes->filecrypt($filename);
// echo $string;
echo "OK,加密完成!" ;
Copy after login

2. Simple function to encrypt PHP files

<?php  

 function encode_file_contents($filename) {  
     $type=strtolower(substr(strrchr($filename,&#39;.&#39;),1));  
     if (&#39;php&#39; == $type && is_file($filename) && is_writable($filename)) { // 如果是PHP文件 并且可写 则进行压缩编码  
         $contents = file_get_contents($filename); // 判断文件是否已经被编码处理  
         $contents = php_strip_whitespace($filename);   

         // 去除PHP头部和尾部标识  
         $headerPos = strpos($contents,&#39;<?php&#39;);  
         $footerPos = strrpos($contents,&#39;?>&#39;);  
         $contents = substr($contents, $headerPos + 5, $footerPos - $headerPos);  
         $encode = base64_encode(gzdeflate($contents)); // 开始编码  
         $encode = &#39;<?php&#39;."\n eval(gzinflate(base64_decode("."&#39;".$encode."&#39;".")));\n\n?>";   

         return file_put_contents($filename, $encode);  
     }  
     return false;  
 }   

 //调用函数
 // echo __DIR__.&#39;\server.php&#39;;   
 $filename = __DIR__.&#39;\server.php&#39;;  
 encode_file_contents($filename);  
 echo "OK,加密完成!" ;
Copy after login

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!

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