Example of AES encryption and decryption in php

Source: Internet
Author: User
Put the code first. There are two files: AES. php (aes algorithm file) and aesDemo. php (application instance File) aesDemo. php:

Example,

 MakeKey ($ key); $ encode = "123456"; // The encrypted string $ ct = $ aes-> encryptString ($ encode, $ keys ); echo "encode = ". $ ct."
"; $ Cpt = $ aes-> decryptString ($ ct, $ keys); echo" decode = ". $ cpt;?>

Example, AES encryption class

 Bit = $ bit; $ this-> key = $ key; $ this-> iv = $ iv; $ this-> mode = $ mode; switch ($ this-> bit) {case 192: $ this-> cipher = encrypt; break; case 256: $ this-> cipher = MCRYPT_RIJNDAEL_256; break; default: $ this-> cipher = MCRYPT_RIJNDAEL_128 ;} switch ($ this-> mode) {case 'ECB ': $ this-> mode = MCRYPT_MODE_ECB; break; case 'cfb': $ this-> mode = MCRYPT_MODE_CFB; break; case 'ofb': $ this-> mode = MCRYPT_MODE_OFB; break; case 'nofb ': $ this-> mode = MCRYPT_MODE_NOFB; break; default: $ this-> mode = MCRYPT_MODE_CBC;} public function encrypt ($ data) {$ data = base64_encode (mcrypt_encrypt ($ this-> cipher, $ this-> key, $ data, $ this-> mode, $ this-> iv); return $ data;} public function decrypt ($ data) {$ data = mcrypt_decrypt ($ this-> cipher, $ this-> key, base64_decode ($ data), $ this-> mode, $ this-> iv); $ data = rtrim ($ data ),".. "); return $ data ;}// usage method $ aes = new AESMcrypt ($ bit = 128, $ key = 'abcdef1234567890', $ iv = '0987654321cba ba ', $ mode = 'cbc'); $ c = $ aes-> encrypt ('haowei. me '); var_dump ($ aes-> decrypt ($ c ));

Example: an encryption and decryption class

 * // Instantiation class * // parameter $ _ bit: Format. it can be 256, 192, or 128 bytes. the default value is 128 bytes * // parameter $ _ type: encryption/decryption mode, supports cfb, cbc, nofb, ofb, stream, and ecb. the default value is ecb * // parameter $ _ key: key. the default value is abcdefghijuklmno * $ tcaes = new TCAES (); * $ string = 'laohu'; * // encryption * $ encodeString = $ tcaes-> encode ($ string ); * // decrypt * $ decodeString = $ tcaes-> decode ($ encodeString );*
*/Class TCAES {private $ _ bit = MCRYPT_RIJNDAEL_256; private $ _ type = MCRYPT_MODE_CBC; // private $ _ key = 'authorization'; private $ _ key = 'abcdefjuklmno '; // private key $ _ use_base64 = true; private $ _ iv_size = null; private $ _ iv = null; /*** @ param string $ _ key * @ param int $ _ bit uses 128 byte by default * @ param string $ _ type encryption/decryption mode * @ param boolean $ _ use_base64 default use base64 secondary encryption */public Function _ construct ($ _ key = '', $ _ bit = 128, $ _ type = 'ECB ', $ _ use_base64 = true) {// encrypted byte if (192 ==== _ bit) {$ this-> _ bit = MCRYPT_RIJNDAEL_192;} elseif (128 ==== _ bit) {$ this-> _ bit = MCRYPT_RIJNDAEL_128;} else {$ this-> _ bit = MCRYPT_RIJNDAEL_256;} // encryption method if ('cfb '==== _ type) {$ this-> _ type = MCRYPT_MODE_CFB;} elseif ('cbc' ===$ _ type) {$ this-> _ type = MCRYPT_MODE_CBC ;} elseif ('nofb '===$ _ type) {$ t His-> _ type = MCRYPT_MODE_NOFB;} elseif ('ofb' ===$ _ type) {$ this-> _ type = MCRYPT_MODE_OFB ;} elseif ('stream' ===$ _ type) {$ this-> _ type = MCRYPT_MODE_STREAM;} else {$ this-> _ type = MCRYPT_MODE_ECB ;} // key if (! Empty ($ _ key) {$ this-> _ key = $ _ key;} // whether to use base64 $ this-> _ use_base64 = $ _ use_base64; $ this-> _ iv_size = mcrypt_get_iv_size ($ this-> _ bit, $ this-> _ type); $ this-> _ iv = mcrypt_create_iv ($ this-> _ iv_size, MCRYPT_RAND);}/*** encrypted * @ param string $ string to be encrypted * @ return string */public function encode ($ string) {if (MCRYPT_MODE_ECB ===$ this-> _ type) {$ encodeString = mcrypt_encrypt ($ this-> _ bit, $ this-> _ key, $ string, $ this-> _ type);} else {$ encodeString = mcrypt_encrypt ($ this-> _ bit, $ this-> _ key, $ string, $ this-> _ type, $ this-> _ iv);} if ($ this-> _ use_base64) $ encodeString = base64_encode ($ encodeString); return $ encodeString ;} /*** decrypt * @ param string $ string to be decrypted * @ return string */public function decode ($ string) {if ($ this-> _ use_base64) $ string = base64_decode ($ string); $ string = $ this-> toHexString ($ string); if (MCRYPT_MODE_ECB ===$ this-> _ type) {$ decodeString = mcrypt_decrypt ($ this-> _ bit, $ this-> _ key, $ string, $ this-> _ type );} else {$ decodeString = mcrypt_decrypt ($ this-> _ bit, $ this-> _ key, $ string, $ this-> _ type, $ this-> _ iv );} return $ decodeString;}/*** convert $ string to hexadecimal form * @ param string $ string * @ return stream */private function toHexString ($ string) {$ buf = ""; for ($ I = 0; $ I <strlen ($ string); $ I ++) {$ val = dechex (ord ($ string {$ I}); if (strlen ($ val) <2) $ val = "0 ". $ val; $ buf. = $ val;} return $ buf ;} /*** convert the hexadecimal stream $ string to a string * @ param stream $ string * @ return string */private function fromHexString ($ string) {$ buf = ""; for ($ I = 0; $ I <strlen ($ string); $ I + = 2) {$ val = chr (hexdec (substr ($ string, $ I, 2); $ buf. = $ val;} return $ buf ;}}

For more information about examples of AES encryption and decryption in php, refer to the PHP Chinese website!

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.