在PHP中使用AES加密算法加密数据
在研究Discuz 的时候,发现Discuz有一套相当完美的加密算法(相对而言)。这个算法可以将数据加密后,储存起来,到需要用的时候,用之前加密的秘钥将之还原。 除了这个之外,还有AES这个算法能够将数据很好的加密起来,在传输过程中不容易被破解。 在PHP中,我
在研究Discuz 的时候,发现Discuz有一套相当完美的加密算法(相对而言)。这个算法可以将数据加密后,储存起来,到需要用的时候,用之前加密的秘钥将之还原。
除了这个之外,还有AES这个算法能够将数据很好的加密起来,在传输过程中不容易被破解。
在PHP中,我们必须先安装好mcrypt这个模块,并且添加相应版本的扩展到php中,详情可以看 不重新编译PHP安装Mcrypt扩展
AES加密模式和填充方式有以下之中,但不是全部
算法/模式/填充 16字节加密后数据长度 不满16字节加密后长度 AES/CBC/NoPadding 16 不支持 AES/CBC/PKCS5Padding 32 16 AES/CBC/ISO10126Padding 32 16 AES/CFB/NoPadding 16 原始数据长度 AES/CFB/PKCS5Padding 32 16 AES/CFB/ISO10126Padding 32 16 AES/ECB/NoPadding 16 不支持 AES/ECB/PKCS5Padding 32 16 AES/ECB/ISO10126Padding 32 16 AES/OFB/NoPadding 16 原始数据长度 AES/OFB/PKCS5Padding 32 16 AES/OFB/ISO10126Padding 32 16 AES/PCBC/NoPadding 16 不支持 AES/PCBC/PKCS5Padding 32 16 AES/PCBC/ISO10126Padding 32 16
下面就是在PHP中使用AES对数据加密
AES-CBC 加密方案
<code><span><span><?php <span>$privateKey <span>= <span>"1234567812345678"<span>; <span>$iv <span>= <span>"1234567812345678"<span>; <span>$data <span>= <span>"Test String"<span>; <span> //加密 <span>$encrypted <span>= <span>mcrypt_encrypt<span>(<span>MCRYPT_RIJNDAEL_128<span>, <span>$privateKey<span>, <span>$data<span>, <span>MCRYPT_MODE_CBC<span>, <span>$iv<span>)<span>; <span>echo<span>(<span>base64_encode<span>(<span>$encrypted<span>)<span>)<span>; <span>echo '<span><span><span><span><br>/>'<span>; <span> //解密 <span>$encryptedData <span>= <span>base64_decode<span>(<span>"2fbwW9+8vPId2/foafZq6Q=="<span>)<span>; <span>$decrypted <span>= <span>mcrypt_decrypt<span>(<span>MCRYPT_RIJNDAEL_128<span>, <span>$privateKey<span>, <span>$encryptedData<span>, <span>MCRYPT_MODE_CBC<span>, <span>$iv<span>)<span>; <span>echo<span>(<span>$decrypted<span>)<span>; <span>?></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
AES-ECB加密方案
<code><span><?php <span> //加密 <span>$key <span>= <span>'1234567890123456'<span>; <span>$content <span>= <span>'hello'<span>; <span>$padkey <span>= <span>pad2Length<span>(<span>$key<span>,<span>16<span>)<span>; <span>$cipher <span>= <span>mcrypt_module_open<span>(<span>MCRYPT_RIJNDAEL_128<span>, <span>''<span>, <span>MCRYPT_MODE_ECB<span>, <span>''<span>)<span>; <span>$iv_size <span>= <span>mcrypt_enc_get_iv_size<span>(<span>$cipher<span>)<span>; <span>$iv <span>= <span>mcrypt_create_iv<span>(<span>$iv_size<span>, <span>MCRYPT_RAND<span>)<span>;<span> #IV自动生成? <span>echo <span>'自动生成iv的长度:'<span>.<span>strlen<span>(<span>$iv<span>)<span>.<span>'位:'<span>.<span>bin2hex<span>(<span>$iv<span>)<span>.'<span><span><span><span><br>>'<span>; <span>if <span>(<span>mcrypt_generic_init<span>(<span>$cipher<span>, <span>pad2Length<span>(<span>$key<span>,<span>16<span>)<span>, <span>$iv<span>) <span>!<span>= <span>-<span>1<span>) <span>{ <span> // PHP pads with NULL bytes if $content is not a multiple of the block size.. <span>$cipherText <span>= <span>mcrypt_generic<span>(<span>$cipher<span>,<span>pad2Length<span>(<span>$content<span>,<span>16<span>) <span>)<span>; <span>mcrypt_generic_deinit<span>(<span>$cipher<span>)<span>; <span>mcrypt_module_close<span>(<span>$cipher<span>)<span>; <span> // Display the result in hex. <span>printf<span>(<span>"128-bit encrypted result:n%snn"<span>,<span>bin2hex<span>(<span>$cipherText<span>)<span>)<span>; <span>print<span>("<span><span><span><span><br>/>"<span>)<span>; <span>} <span> //解密 <span>$mw <span>= <span>bin2hex<span>(<span>$cipherText<span>)<span>; <span>$td <span>= <span>mcrypt_module_open<span>(<span>MCRYPT_RIJNDAEL_128<span>, <span>''<span>, <span>MCRYPT_MODE_ECB<span>, <span>''<span>)<span>; <span>if <span>(<span>mcrypt_generic_init<span>(<span>$td<span>, <span>$padkey<span>, <span>$iv<span>) <span>!<span>= <span>-<span>1<span>) <span>{ <span>$p_t <span>= <span>mdecrypt_generic<span>(<span>$td<span>, <span>hexToStr<span>(<span>$mw<span>)<span>)<span>; <span>mcrypt_generic_deinit<span>(<span>$td<span>)<span>; <span>mcrypt_module_close<span>(<span>$td<span>)<span>; <span>$p_t <span>= <span>trimEnd<span>(<span>$p_t<span>)<span>; <span>echo <span>'解密:'<span>; <span>print<span>(<span>$p_t<span>)<span>; <span>print<span>("<span><span><span><span><br>/>"<span>)<span>; <span>print<span>(<span>bin2hex<span>(<span>$p_t<span>)<span>)<span>; <span>echo '<span><span><span><span><br>><span><span><span><span><br>>'<span>; <span>} <span> //将$text补足$padlen倍数的长度 <span>function <span>pad2Length<span>(<span>$text<span>, <span>$padlen<span>)<span>{ <span>$len <span>= <span>strlen<span>(<span>$text<span>)<span>%<span>$padlen<span>; </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.
