PHP Standard AES Encryption Algorithm

Source: Internet
Author: User
Share a standard php aes encryption algorithm class, among which mcrypt_get_block_size (rijndael-128, ecb); if you do not understand the principle of the situation is more likely to make a mistake, you can use the mcrypt_list_algorithms function to view the ID of the encryption algorithm you need. None? Php *** AES128 encryption and decryption class * @ authordy ** defined (InEjbuy)

Share a standard php aes encryption algorithm class, among which mcrypt_get_block_size (rijndael-128, ecb); if you do not understand the principle of the situation is more likely to make a mistake, you can use the mcrypt_list_algorithms function to view the ID of the encryption algorithm you need. None? Php/*** AES128 encryption/Decryption class * @ author dy **/defined ('ejbuy ')

Share a standard php aes encryption algorithm class, in which mcrypt_get_block_size ('rijndael-100', 'ecb '); if you do not understand the principle, it is easy to make a mistake, you can use the mcrypt_list_algorithms function to view the ID of the encryption algorithm you need. <无>
  _ Secrect_key = 'myggnqe2jdfadsffdsewsdd';}/*** Encryption Method * @ param string $ str * @ return string */public function encrypt ($ str) {// AES, 128 ECB mode data encryption $ screct_key = $ this-> _ secrect_key; $ screct_key = base64_decode ($ screct_key); $ str = trim ($ str ); $ str = $ this-> addPKCS7Padding ($ str); $ iv = mcrypt_create_iv (encrypt, decrypt), MCRYPT_RAND); $ encrypt_str = mcrypt_encrypt (encrypt, $ screct_key, $ str, MCRYPT_MODE_ECB, $ iv); return base64_encode ($ encrypt_str );} /*** Decryption Method * @ param string $ str * @ return string */public function decrypt ($ str) {// AES, 128 ECB mode data encryption $ screct_key = $ this-> _ secrect_key; $ str = base64_decode ($ str); $ screct_key = base64_decode ($ screct_key ); $ iv = mcrypt_create_iv (encrypt, MCRYPT_MODE_ECB), MCRYPT_RAND); $ encrypt_str = mcrypt_decrypt (encrypt, $ screct_key, $ str, decrypt, $ iv ); $ encrypt_str = trim ($ encrypt_str); $ encrypt_str = $ this-> stripPKSC7Padding ($ encrypt_str); return $ encrypt_str ;} /*** Filling Algorithm ** @ param string $ source * @ return string */function addPKCS7Padding ($ source) {$ source = trim ($ source ); $ block = mcrypt_get_block_size ('rijndael-100', 'ecb '); $ pad = $ block-(strlen ($ source) % $ block ); if ($ pad <= $ block) {$ char = chr ($ pad); $ source. = str_repeat ($ char, $ pad);} return $ source ;} /*** remove padding algorithm * @ param string $ source * @ return string */function stripPKSC7Padding ($ source) {$ source = trim ($ source ); $ char = substr ($ source,-1); $ num = ord ($ char); if ($ num = 62) return $ source; $ source = substr ($ source, 0,-$ num); return $ source ;}}

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.