Example of using AES encryption algorithm to encrypt data in PHP

Source: Internet
Author: User
Tags mcrypt
AES encryption is a very advanced encryption. many people say it is very difficult to crack AES encryption. the following section describes an example of how to use the AES encryption algorithm to encrypt data. when studying Discuz, we found that Discuz has a set of perfect... AES encryption is a very advanced encryption. many people say it is very difficult to crack AES encryption. the following section describes an example of how to use the AES encryption algorithm to encrypt data.

When studying Discuz, we found that Discuz has a set of perfect encryption algorithms (relatively speaking, this algorithm can encrypt and store data. when needed, use the encrypted key to restore it.

In addition to this, the AES algorithm can encrypt data well and is not prone to cracking during transmission.

In PHP, we must first install the mcrypt module and add the extension of the corresponding version to php. For more information, see install the Mcrypt extension without recompiling PHP.

The AES encryption mode and filling method are as follows, but not all.

Algorithm/mode/fill 16-byte encryption after data length less than 16 bytes encryption after length AES/CBC/NoPadding 16 does not support AES/CBC/PKCS5Padding 32 16 AES/CBC/ISO10126Padding 32 16 AES /CFB/NoPadding 16 original data length AES/CFB/PKCS5Padding 32 16 AES/CFB/ISO10126Padding 32 16 AES/ECB/NoPadding 16 not support AES/ECB/PKCS5Padding 32 16 AES/ECB /ISO10126Padding 32 16 AES/OFB/NoPadding 16 original data length AES/OFB/PKCS5Padding 32 16 AES/OFB/ISO10126Padding 32 16 AES/PCBC/NoPadding 16 not support AES/PCBC/PKCS5Padding 32 16 AES/PCBC/ISO10126Padding 32 16

The following is the use of AES in PHP data encryption, AES-CBC encryption scheme, the code is as follows:

 '; // Decrypt $ encryptedData = base64_decode ("2fbwW9 + 8vPId2/foafZq6Q ="); $ decrypted = Encrypt (encrypt, $ privateKey, $ encryptedData, encrypt, $ iv ); echo ($ decrypted );

AES-ECB encryption scheme, the code is as follows:

 '; If (mcrypt_generic_init ($ cipher, pad2Length ($ key, 16), $ iv )! =-1) {// PHP pads with NULL bytes if $ content is not a multiple of the block size .. $ cipherText = mcrypt_generic ($ cipher, pad2Length ($ content, 16); mcrypt_generic_deinit ($ cipher); mcrypt_module_close ($ cipher); // Display the result in hex. printf ("128-bit encrypted result: n % snn", bin2hex ($ cipherText); print ("
") ;}// Decrypt $ mw = bin2hex ($ cipherText); $ td = mcrypt_module_open (encrypt,'', MCRYPT_MODE_ECB, ''); if (mcrypt_generic_init ($ td, $ padkey, $ iv )! =-1) {$ p_t = mdecrypt_generic ($ td, hexToStr ($ mw); mcrypt_generic_deinit ($ td); mcrypt_module_close ($ td ); $ p_t = trimEnd ($ p_t); echo 'decryption: '; print ($ p_t); print ("
"); Print (bin2hex ($ p_t); echo'

';} // Add $ text to the length function pad2Length ($ text, $ padlen) in multiples of $ padlen {$ len = strlen ($ text) % $ padlen; $ res = $ text; $ span = $ padlen-$ len; for ($ I = 0; $ I <$ span; $ I ++) {$ res. = chr ($ span) ;}return $ res ;}// remove the extra length after decryption (because the length of block_size is added during encryption) function trimEnd ($ text) {$ len = strlen ($ text); $ c = $ text [$ len-1]; if (ord ($ c) <$ len) {for ($ I = $ len-ord ($ c); $ I <$ len; $ I ++) {if ($ text [$ I]! = $ C) {return $ text;} return substr ($ text, 0, $ len-ord ($ c);} return $ text ;} // Convert the hexadecimal string to the binary string function hexToStr ($ hex) {$ bin = ""; for ($ I = 0; $ I

AES-ECB encryption scheme, the code is as follows:

   '; $ Jiemi = mcrypt_decrypt (MCRYPT_RIJNDAEL_128, $ key, hexToStr ($ AESed), MCRYPT_MODE_ECB, $ iv); # decrypt echo 'decryption:'; echo trimEnd ($ jiemi );

The above are just the three simple encryption methods I listed. In fact, there are still many methods. we need to keep learning, and there is still a long way to go for cryptography.


Link to this article:

Add to favorites ^ please keep the tutorial address.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.