Sample code for PHP to connect to java AES/ECB/PKCS5Padding encryption mode (Figure)

Source: Internet
Author: User
For project requirements, we need to connect to an insurance company to call APIs. our company is the PHP backend and the insurance company is the java backend. data transmission in the middle cannot be prevented from being encrypted or decrypted, currently, we recommend that you use encrypted AES. For project requirements, we need to connect to an insurance company to call APIs. our company is the PHP backend and the insurance company is the java backend. data transmission in the middle cannot be prevented from being encrypted or decrypted, currently, we recommend that you use encrypted AES.

During the connection process, it is inevitable that the mountains will be migrated to the water,

The following describes the encryption instructions for my company:

Be sure to know your own encryption methods repeatedly. Otherwise, the results of a different encryption mode, ECB and CBC, will be very different.

The final code that can be used is attached:

 '; // Echo Security: decrypt ($ value, $ key );

Call in common functions:

/*** Request body encryption ** @ param array $ information of the content applicant * @ return string */function hetai_encrypt ($ content) {// solution 7 print_r ("\ r \ n"); vendor ('encrypt. security ') or die ("solution 7 failed to be introduced"); $ sec = new \ Security (); $ string = $ content; $ sec_res = $ sec-> encrypt ($ string, base64_decode ("eeSvvVtUDLi5lTBHDjCeFw ="); $ sec_res = strToHex ($ sec_res ); // The result is converted to hexadecimal and converted to uppercase. // The conversion is performed several times here. // It is only to cater to the results I need. // Set it according to your own encryption requirements. $ encrypt_upper = strToHex (base64_decode (hexToStr ($ sec_res ))); var_dump ("\ r \ n solution 7 encryption result \ r \ n ". $ encrypt_upper); // decrypt $ sec_res_lower = strtolower ($ sec_res); // Convert it to lower case $ sec_res_lower_tostr = hexToStr ($ sec_res ); // Convert the hexadecimal string $ sec_dec = $ sec-> decrypt ($ sec_res_lower_tostr, base64_decode ("eeSvvVtUDLi5lTBHDjCeFw = ")); var_dump ("\ r \ n solution 7 decryption result \ r \ n ". $ sec_dec); return $ encrypt_upper ;}

Convert a binary string to a hexadecimal string or a hexadecimal string to a binary string:

/*** Convert string to hexadecimal format * @ param string $ string * @ return string */function strToHex ($ string) {$ hex = ""; for ($ I = 0; $ I
 
  

I have attached a three-day encrypted feature block that will only be used as a reminder ~

The above is the sample code (figure) for PHP to connect to the java AES/ECB/PKCS5Padding encryption method. For more information, see other related articles in the first PHP community!

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.