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!

Redis作为一款开源的内存缓存数据库,在应用开发中极度广泛。其强大、高效的性能优势,使得其成为了最常用的缓存数据库之一。然而,在某些特殊场景下,由于数据量过大或安全性需要,我们需要对Redis数据进行压缩和加密处理。本文将从Redis的数据压缩和加密两方面入手,探讨Redis作为缓存数据库在实际应用中的数据压缩与加密方案。一、Redis数据压缩方案Re

如何使用PHPZipArchive实现对压缩包的文件内容加密和解密?在进行文件传输或存储时,保护数据安全是非常重要的。使用密码对压缩包的文件内容进行加密和解密可以有效地避免数据泄漏的风险。PHP提供了一个名为ZipArchive的类,它可以用来创建和操作ZIP格式的压缩包。本文将介绍如何使用PHPZipArchive类实现对压缩包的文件内容加密和解密。创

在Windows11上加密文件和文件夹与WindowsBitLocker一样,EFS加密可用于加密您PC上最重要的文件。使用内置加密非常简单,而且触手可及。此外,由于EFS与您的用户帐户相关联,我们将向您展示如何将加密密钥备份到安全位置,这样您就永远不会失去对文件和文件夹的访问权限。注意:要使用EFS,您的PC必须运行Windows11专业版、企业版或教育版。EFS加密在Windows11家庭版上不可用。要加密充满文件的文件夹或单个文件,请使用以下步骤:

windows10家庭版如何加密文件夹呢,加密文件夹这个功能一般客户没有使用,但是如果想要设定的话也是可行的,首先在想要加密的文件夹中右键属性进到高级,然后选择缩小加密属性,加密内容维护数据,下面就是具体的windows10家庭版如何加密文件夹方式介绍,大家如果想要学会的话就接着往下看。windows10家庭版如何加密文件夹1.最先,先找到想要加密的文件夹,然后用鼠标右键文件夹,在弹出的菜单中选择底部的“属性”选项,点击查看;2.随后,将打开文件的属性窗口,点击窗口里的“高级”按键进到;3.接着

SHA(SecureHashAlgorithm)加密技术是一种常用的安全加密算法。在PHP开发中,SHA加密技术通常用于加密账户密码以及保护敏感数据。本文将介绍如何在PHP中实现SHA加密技术。SHA算法简介SHA算法是一种信息摘要算法,通常用于数据的完整性保护和身份验证。SHA算法的主要作用是将任意长度的消息转换为一个固定长度的消息摘要(即哈希值),通

PHP和XML:如何实现数据的加密和解密引言:在现代的互联网时代,数据的安全性越来越受到重视。其中,对于敏感数据的加密和解密成为了保护数据安全的重要手段之一。本文将通过使用PHP和XML来实现数据的加密和解密,并提供相关的代码示例。加密数据的实现使用PHP的加密函数,可以轻松实现对数据的加密。下面是一个使用AES加密算法对数据进行加密的示例代码://待加密

如何通过PHPZipArchive实现对压缩包的加密和解密操作?概述:PHPZipArchive是一种用于创建、打开和操作ZIP压缩文件的功能强大的类。尽管ZipArchive类本身并不直接提供加密和解密ZIP压缩文件的功能,但我们可以利用一些PHP扩展来实现对压缩包的加密和解密操作,如openssl扩展。在本文中,我们将介绍如何使用PHPZipArc

在当前信息化时代,网络上存在着大量的软件、程序和代码文件,其中有不少代码是需要被保护的,以避免被盗版或恶意利用,同时也有些代码需要进行授权以获得经济收益。那么,问题就来了:如何进行代码授权和加密保护呢?一、代码授权代码授权是指在一定的条件下,授予使用或修改、发布软件或程序源代码的权利。此时,程序开发者作为版权人,需要明确在何种情况下允许其他人使用代码、以何


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment
