Example code for PHP docking Java's aes/ecb/pkcs5padding encryption method

Source: Internet
Author: User
Tags decrypt 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:

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_rijndae    l_128, MCRYPT_MODE_ECB);    $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;        } private static function Pkcs5_pad ($text, $blocksize) {$pad = $blocksize-(strlen ($text)% $blocksize); Return $text.    Str_repeat (Chr ($pad), $pad);        The 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 "Encryption::". $value. ' <br/> ';//echo Security::d ecrypt ($value, $key);

In public functions called:

/** * Request Body Encryption * @param array $content Policyholder's information * @return string        */function Hetai_encrypt ($content) {//Scheme VII Print_r ("\ r \ n"); Vendor (' Encrypt.        Security ') or Die ("Scenario 7 introduction failed");        $sec = new \security ();        $string = $content;        $sec _res = $sec->encrypt ($string, Base64_decode ("eesvvvtudli5ltbhdjcefw=="));        $sec _res = Strtohex ($sec _res);//Results Turn 16 and turn to uppercase//here do a few conversions//just to cater to the results I need//according to their 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);         Decrypt $sec _res_lower = Strtolower ($sec _res);//Turn lowercase $sec _res_lower_tostr = hextostr ($sec _res);//16 binary to 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;    }         /**     * Hexadecimal 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 ~

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.