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

Parsing of AES encrypted files in PHP (with code)

Aug 04, 2018 pm 01:56 PM
encryption

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Enable 256-bit Bitlocker encryption on Windows 11 for increased security Enable 256-bit Bitlocker encryption on Windows 11 for increased security Nov 26, 2023 am 11:21 AM

Bitlocker is the default encryption technology for Windows operating systems. It is widely used on Windows, but some users prefer third-party solutions such as VeraCrypt. What many users of Bitlocker don't know is that it defaults to 128-bit encryption, even though 256-bit is available. Without going into too much detail about the differences; the core difference between AES 128-bit and 256-bit encryption is the length of the security key. Longer keys make brute force attacks more difficult. While the default is 128-bit, even Microsoft recommends 256-bit for better security. The problem is, most users probably don't know about the weaker defaults or how to change them. First, you might want to know W

Win11 encrypted dns detailed tutorial Win11 encrypted dns detailed tutorial Dec 25, 2023 am 10:13 AM

Microsoft previously provided dns encryption services for win11, but many users do not know how to use win11 encrypted dns. In fact, we only need to open the dns settings under network settings. Detailed tutorial on win11 encrypted dns: 1. First enter the disk and find the folder you want to encrypt. 2. Then open "Ethernet" on the right 3. Then find the DNS server allocation below and click "Edit" 4. After changing "Auto (DHCP)" to "Manual", open "IPv4" below 5. After turning it on, enter "8.8.8.8" in the preferred DNS 6. Then change the preferred DNS encryption to "Encryption only (DNS over HTTPS)" 7. After the changes are completed, click "Save" and you will find

Does Win10 Home Edition support folder encryption? Does Win10 Home Edition support folder encryption? Jan 09, 2024 am 08:58 AM

File encryption aims to implement professional-level encryption of data to more effectively ensure data security! Only by mastering the correct encryption key can the decryption operation be performed, ensuring the security of information assets. However, the file encryption function of Win10 Home Edition does not yet have this feature. Can Win10 Home Edition encrypt folders? Answer: Win10 Home Edition cannot encrypt folders. Tutorial on encrypting files in Windows system 1. Right-click on the file or folder you want to encrypt (or press and hold for a while), and then select the "Properties" function. 2. In the new expanded interface, look for the "Advanced" option. After clicking to enter, remember to check the "Encrypt content to protect data" option located below. 3. After the setting is completed, click "OK" to

How to set up encryption of photo album on Apple mobile phone How to set up encryption of photo album on Apple mobile phone Mar 02, 2024 pm 05:31 PM

In Apple mobile phones, users can encrypt photo albums according to their own needs. Some users don't know how to set it up. You can add the pictures that need to be encrypted to the memo, and then lock the memo. Next, the editor will introduce the method of setting up the encryption of mobile photo albums for users. Interested users, come and take a look! Apple mobile phone tutorial How to set up iPhone photo album encryption A: After adding the pictures that need to be encrypted to the memo, go to lock the memo for detailed introduction: 1. Enter the photo album, select the picture that needs to be encrypted, and then click [Add to] below. 2. Select [Add to Notes]. 3. Enter the memo, find the memo you just created, enter it, and click the [Send] icon in the upper right corner. 4. Click [Lock Device] below

Common network communication and security problems and solutions in C# Common network communication and security problems and solutions in C# Oct 09, 2023 pm 09:21 PM

Common network communication and security problems and solutions in C# In today's Internet era, network communication has become an indispensable part of software development. In C#, we usually encounter some network communication problems, such as data transmission security, network connection stability, etc. This article will discuss in detail common network communication and security issues in C# and provide corresponding solutions and code examples. 1. Network communication problems Network connection interruption: During the network communication process, the network connection may be interrupted, which may cause

How to set a password for folder encryption without compression How to set a password for folder encryption without compression Feb 20, 2024 pm 03:27 PM

Folder encryption is a common data protection method that encrypts the contents of a folder so that only those who have the decryption password can access the files. When encrypting a folder, there are some common ways to set a password without compressing the file. First, we can use the encryption function that comes with the operating system to set a folder password. For Windows users, you can set it up by following the following steps: Select the folder to be encrypted, right-click the folder, and select "Properties"

Complete guide to win11 file encryption Complete guide to win11 file encryption Jan 09, 2024 pm 02:50 PM

Some friends want to protect their files, but don’t know how to encrypt win11 documents. In fact, we can directly use folder encryption or use third-party software to encrypt files. Detailed tutorial on win11 document encryption: 1. First find the file you want to encrypt, right-click to select it, and open "Properties" 2. Then click "Advanced" in the properties column 3. Select "Encrypt content to protect data" in Advanced and click " OK" 4. Then click "OK" to save. 5. Finally, select the desired encryption mode and "OK" to save the document to encrypt the document.

How to encrypt the compressed package in winrar-winrar encrypted compressed package method How to encrypt the compressed package in winrar-winrar encrypted compressed package method Mar 23, 2024 pm 12:10 PM

The editor will introduce to you three methods of encryption and compression: Method 1: Encryption The simplest encryption method is to enter the password you want to set when encrypting the file, and the encryption and compression are completed. Method 2: Automatic encryption Ordinary encryption method requires us to enter a password when encrypting each file. If you want to encrypt a large number of compressed packages and the passwords are the same, then we can set automatic encryption in WinRAR, and then just When compressing files normally, WinRAR will add a password to each compressed package. The method is as follows: Open WinRAR, click Options-Settings in the setting interface, switch to [Compression], click Create Default Configuration-Set Password Enter the password we want to set here, click OK to complete the setting, we only need to correct

See all articles