php-libmcrypt-2.4.x Encryption-aes Encryption

Source: Internet
Author: User

mcrypt_get_block_size - get the packet size of the encryption algorithm

This function will be deprecated after PHP7.1.0.


int mcrypt_get_block_size (int $cipher)
int Mcrypt_get_block_size (string $cipher, String $mode)

The first prototype is for Libmcrypt 2.2.x, and the second prototype is for Libmcrypt 2.4.x or 2.5.x.


PHP AES Plus, decryption class:

<?phpclass security {public static function encrypt ($input,  $key)  {//  define  (' mcrypt_rijndael_128 ',  "rijndael-128");// 16 bytes, 128-bit// define  (' Mcrypt_ Mode_ecb ',  "ECB");//  commonly used encryption mode $size = mcrypt_get_block_size (Mcrypt_rijndael_128, mcrypt_ MODE_ECB);//  fill vacancy; Pkcs5_pad&pkcs7_pad difference is not very $input = security::p kcs5_pad ($input,  $size) ; $td  = mcrypt_module_open (mcrypt_rijndael_128,  ", mcrypt_mode_ecb, "); $iv  =  mcrypt_create_iv  (Mcrypt_enc_get_iv_size ($TD),  mcrypt_rand) mcrypt_generic_init ($td,  $key ,  $iv), $data  = mcrypt_generic ($td,  $input); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ( $TD); $data  = base64_encode ($data);return  $data;}                  /**          *  $text &NBSP; encrypted string          *  $blocksize   chunk size, 16 bytes; Value 16          */private static function pkcs5_pad  ($text ,  $blocksize)  {$pad  =  $blocksize  -  (strlen ($text)  %  $blocksize);//   Use $pad ASCII values to populate empty return  $text  . str_repeat (Chr ($pad),  $pad);}  public static function decrypt ($sStr,  $sKey)  {$decrypted = mcrypt_decrypt ( mcrypt_rijndael_128, $sKey, Base64_decode ($SSTR), MCRYPT_MODE_ECB), $dec _s = strlen ($decrypted); $padding  = ord ($decrypted [$dec _s-1]); $decrypted  = substr ($decrypted,  0, -$padding); return  $decrypted;}}    //  $key  =  "1234567891234567";//  $data  =  ' {' user ': ' 1234556789 ', "pwd": "4343434343", "tel": "18988888888", "Realname": "Zhangsan"} '; //  $value  = security:: Encrypt ($data  ,  $key  );// echo  "Encrypt::" $value. ' <br/> ';// echo security::d ecrypt ($value,  $key  );


Used in code:

   /**     *  encryption, decryption      */     function zm_ecrypt ()     {           Encryption, decryption          $string  =  ' {' user ': ' 1234556789 ', ' pwd ': ' 4343434343 "," tel ":" 18988888888 "," Realname ":" Zhangsan "} ';                 vendor (' Encrypt. Security ')  or die ("Scenario 7 introduced failure");         $sec  = new  \security ();        //  The second parameter key is Base64_encode encrypted           $sec _res1 =  $sec->encrypt ($string,  " Em1hcnrlaziwmtc5odc2ntqzmjewmtiz ");        //  result turn 16           $sec _res = tohexstring (Base64_decode ($sec _res1));                 //   Decryption         //  first turn hex into a string and then base64_encode          $demo  = base64_encode (hextostr ($sec _res));          $sec _dec =  $sec->decrypt ($demo,  " Em1hcnrlaziwmtc5odc2ntqzmjewmtiz ");//         var_dump (" \r\ N Scenario 7 encryption results \ r \  .  $sec _res);//         var_dump ("\r\ N Scenario 7 decryption results \ r \  .  $sec _dec);//         return  $as _ res_encrypt;    }

When using, be aware of Base64_encode and base64_decode, whether the key and string are encoded.


The conversion between the hexadecimal and the string used:

   /**     *  Hex to String      *  16 Binary to 2 binary string          *  @param   hex   $hex       *  @return  string     */     Function hextostr ($hex)     {         $string = " ";         for ($i =0; $i <strlen ($hex)-1; $i +=2)               $string. =CHR (Hexdec ($hex [$i]. $hex [$i +1]);         return   $string;    }         /**     *  convert $string to hexadecimal      *   @param  string  $string      *  @return  stream      */    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;     }


This article from "for the Future" blog, declined reprint!

php-libmcrypt-2.4.x Encryption-aes Encryption

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.