PHP Docking Java aes/ecb/pkcs5padding encryption method

Source: Internet
Author: User
Tags decrypt hex to string ord

Because of the project needs, and an insurance company docking call API, my company is the PHP background, the insurance company is Java background, the middle of the data transmission will not be able to encrypt, decrypt, the current encrypted AES comparison recommended.

In the process of docking, it is inevitable to turn over the mountains, the more water,

Here are the encryption instructions for my docking company:

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/8F/14/wKiom1jTKWKR7b5pAAD_NXwMzS0759.jpg "title=" Docking company encryption description. jpg "alt=" wkiom1jtkwkr7b5paad_nxwmzs0759.jpg "/>

Be sure to know their own encryption method, or a cryptographic mode of the ECB, CBC differences, the results are very different.


Attach the final code that can be used:

<?phpclass security {public static function encrypt ($input,  $key)  {$size  = mcrypt_get_block_size (MCRYPT_RIJNDAEL_128,&NBSP;MCRYPT_MODE_ECB); $input  = security:: Pkcs5_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;}  private static function pkcs5_pad  ($text,  $blocksize)  {$pad  = $ blocksize -  (strlen ($text)  %  $blocksize);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  =  "Example";  //   $value  = security::encrypt ($data  ,  $key  );// echo  "Encrypt::". $value. ' <br/> ';// echo security::d ecrypt ($value,  $key  );

In public functions called:

/**     * request body Encryption      *  @param   array  $content   Policyholders ' information      *  @return  string      */    function hetai_encrypt ($content)  {                 //  Program VII          print_r ("\ r \ n");         vendor (' Encrypt. Security ')  or die ("Scenario 7 introduced failure");         $sec  = new  \security ();         $string  =  $content;          $sec _res =  $sec->encrypt ($string,  base64_decode (" eesvvvtudli5ltbhdjcefw== "));         $sec _res = strtohex ($sec _res);//  results turn 16 into uppercase         //  has done a couple of conversions here         //  Just to cater to the results I need.         //  according to its own encryption requirements to set           $encrypt _upper = strtohex (Base64_decode (Hextostr ($sec _res));         var_dump ("\ r \ n Scheme 7 encryption results \ r \ n"  .  $encrypt _upper);         //  decryption          $sec _res_lower =  strtolower ($sec _res);//  turn lowercase          $sec _res_lower_tostr  = hextostr ($sec _res),// 16 into string         $sec _dec  =  $sec->decrypt ($sec _res_lower_tostr, base64_decode ("eesvvvtudli5ltbhdjcefw==");         var_dump ("\ r \ n Solution 7 decryption results \ r \ n"  .  $sec _dec);         return  $encrypt _upper;            } 

Binary string to 16 binary, 16 binary string to binary:

/**     *  string to hex      *  @param  string $ string     *  @return  string     */     function strtohex ($string)     {          $hex = "";         for ($i =0; $i <strlen ($string); $i + +)               $hex. =dechex (Ord ($string [$i]);          $hex =strtoupper ($hex);         return   $hex;    }        /**      *  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;     }



Attached to torture my three days of cryptographic function block, only for their own reminders, do not like to spray ~

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/8F/14/wKiom1jTLSDTgTLIAANr6tgJTpc316.jpg "title=" AES encryption. jpg "alt=" wkiom1jtlsdtgtliaanr6tgjtpc316.jpg "/>

of the same problem


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

PHP Docking Java aes/ecb/pkcs5padding encryption method

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.