• php AES-128-CBC 加密 通信java


    <?php
    header("Content-type: text/html; charset=utf-8");
    require('MyAES.php');
    class Xfb{
    /*************************************生成sign start*/
    //生成sign
    function sign($arrStr)
    {
    $str='';
    sort($arrStr);
    foreach($arrStr as $v)
    {
    $str .= $v.'&';
    }
    //$str .= 'key='.md5(strtoupper(MISHI));
    $str .= 'key='.MISHI;
    $handle = fopen(ROOT_PATH.'sign.txt','a+');
    fwrite($handle,$str);
    return strtoupper(md5($str));
    }
    /*************************************生成sign end*/
    /*************************************提交xml start*/
    function requestData($data)
    {
    $MyAES = new MyAES();
    $jiaRes = $MyAES->desEncryptStr($data,"1102130405061708");
    $header[] = "Content-type: text/xml;charset=UTF-8";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,REURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jiaRes);
    $aa = curl_exec($ch);
    // grab URL, and print
    if(curl_errno($ch)){
    print curl_error($ch);
    }
    curl_close($ch);
    //$resultDa = $this->jiemi128CBC($aa);
    $resultDa = $MyAES->DesDecryptStr($aa,"1102130405061708"); return $resultDa; } /*************************************提交xml end*/ /*************************************最后一部反会成功 start*/ function requestDataSuccess($data) { $header[] = "Content-type: text/xml;charset=UTF-8"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,REURL); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt($ch, CURLOPT_HTTPHEADER,$header); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $aa = curl_exec($ch); // grab URL, and print if(curl_errno($ch)){ print curl_error($ch); } curl_close($ch); } /*************************************最后一部反会成功 end*/ /*************************************AES 128 CBC start*/ function aes128CBC($data) { $privateKey = "1102130405061708"; $iv = "1102130405061708"; //加密 $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv); $encrypted = base64_encode($encrypted); $handle = fopen(ROOT_PATH.'b.txt','a+'); fwrite($handle,$encrypted); return $encrypted; } /*************************************AES 128 CBC end*/ /*************************************AES解密 128 CBC start*/ function jiemi128CBC($encrypted) { $privateKey = "1102130405061708"; $iv = "1102130405061708"; //解密 $decrypted = base64_decode($encrypted); $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, $decrypted, MCRYPT_MODE_CBC, $iv); $handle = fopen(ROOT_PATH.'b.txt','a+'); fwrite($handle,$decrypted); } /*************************************AES 128 CBC end*/ /*************************************php解析xml start*/ //$xmlstring = "<xml><body>锤子手机</body><orderid>154879797</orderid></xml>"; //第一种方法 //$dom = new DOMDocument(); //$dom->loadXML($reponse); //print_r(getArray($dom->documentElement)); function getArray($node) { $array = false; if ($node->hasAttributes()) { foreach ($node->attributes as $attr) { $array[$attr->nodeName] = $attr->nodeValue; } } if ($node->hasChildNodes()) { if ($node->childNodes->length == 1) { $array[$node->firstChild->nodeName] = $this->getArray($node->firstChild); } else { foreach ($node->childNodes as $childNode) { if ($childNode->nodeType != XML_TEXT_NODE) { $array[$childNode->nodeName][] = $this->getArray($childNode); } } } } else { return $node->nodeValue; } return $array; } //第二种方法 /*$p = xml_parser_create(); xml_parse_into_struct($p, $xmlstring, $vals, $index); xml_parser_free($p); echo "Index array "; print_r($index); echo " Vals array "; print_r($vals);*/ /*************************************php解析xml end*/ }?>





    <?php
    class MyAES {
    public function encryptString($input, $key) {
    $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $input = MyAES::pkcs5_pad($input, $size);
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    //$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    $iv="1102130405061708";
    mcrypt_generic_init($td, $key, $iv);
    $data = mcrypt_generic($td, $input);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    $data = base64_encode($data);
    $handle = fopen(ROOT_PATH.'a.txt','a+');
    fwrite($handle,$data);
    return $data;
    }

    private static function pkcs5_pad ($text, $blocksize) {
    $pad = $blocksize - (strlen($text) % $blocksize);
    return $text . str_repeat(chr($pad), $pad);
    }

    public function decryptString($sStr, $sKey) {
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    //$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    $iv = "1102130405061708";
    $decrypted= @mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $sKey, base64_decode($sStr), MCRYPT_MODE_CBC, $iv);
    $dec_s = strlen($decrypted);
    $padding = ord($decrypted[$dec_s-1]); $decrypted = substr($decrypted, 0, -$padding); return $decrypted; } //���ܷ��� function desEncryptStr($xml,$keyString){ //$aes = new myAES(); $ct = $this->encryptString($xml, $keyString); return $ct; } //���ܷ��� function DesDecryptStr($xml,$keyString){ //$aes = new myAES(); $cpt = $this->decryptString($xml, $keyString); return $cpt; } }?>



  • 相关阅读:
    PHP+MySQL实现海量数据导入导出的总结:is_numbric函数的坑
    【PHP开发规范】继承与扩展:PSR-2 编码风格规范
    【PHP开发规范】老生常谈的编码开发规范你懂多少?
    【PHP面试题】通俗易懂的两个面试必问的排序算法讲解:冒泡排序和快速排序
    php数组函数array_column:不用循环就能提取多维数组内容
    php使用urlencode对中文编码而引出的问题:urlencode和rawurlencode的区别
    table-tree vs stock vs whiteboard
    PDF解析
    山灵up4
    Devops之CI/CD
  • 原文地址:https://www.cnblogs.com/gzyx1988/p/5625816.html
Copyright © 2020-2023  润新知