PHP對接java的AES/ECB/PKCS5Padding加密方式的範例程式碼

來源:互聯網
上載者:User
因項目需要,要和一家保險公司對接調用API,我公司是PHP後台,保險公司是java後台,中間的資料轉送就避免不了要加密、解密了,目前通行的加密AES比較推薦。

對接的過程中,就難免要翻山越水的了,

下面是我對接公司的加密說明:

一定要屢清楚自己的加密方式,否則一個加密模式ECB、CBC的差別,結果就千差萬別的。

附上最終能使用的代碼:

<?phpclass Security {    public static function encrypt($input, $key) {    $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, 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 "加密::".$value.'<br/>';// echo Security::decrypt($value, $key );

公用函數中調用:

/**     * request body加密     * @param array $content 投保人的資訊     * @return string     */    function hetai_encrypt($content) {                 // 方案七        print_r("\r\n");        vendor('encrypt.Security') or die("方案7引入失敗");        $sec = new \Security();        $string = $content;        $sec_res = $sec->encrypt($string, base64_decode("eeSvvVtUDLi5lTBHDjCeFw=="));        $sec_res = strToHex($sec_res);// 結果轉16進位並轉成大寫        // 這裡做了好幾次的轉換        // 只是為了迎合出來我需要的結果而已        // 根據自己的加密要求來定        $encrypt_upper = strToHex(base64_decode(hexToStr($sec_res)));        var_dump("\r\n方案7加密的結果\r\n" . $encrypt_upper);        // 解密        $sec_res_lower = strtolower($sec_res);// 轉小寫        $sec_res_lower_tostr = hexToStr($sec_res);// 16進位轉成string        $sec_dec = $sec->decrypt($sec_res_lower_tostr, base64_decode("eeSvvVtUDLi5lTBHDjCeFw=="));        var_dump("\r\n方案7解密的結果\r\n" . $sec_dec);        return $encrypt_upper;             }

二進位字串轉16進位、16進位字串轉二進位:

/**     * 字串轉十六進位     * @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;    }         /**     * 十六進位轉字串     * 16進位的轉為2進位字串         * @param 十六進位 $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;    }

附上折磨我三天的加密功能塊,僅供自己作提醒之用,不喜勿噴~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.