• 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; } }?>



  • 相关阅读:
    Win7 安装 IIS 7.5 及部署 .NET 程序到 IIS 的内容杂记
    乐嘉,你快回来
    AMD将要嫁他人?
    高精度运算(运算符重载)
    魔数卡牌
    C语言"#","#@"和"##"在宏定义中的使用技巧
    CentOS7下安装FTP服务
    Linux下FTP的安装和登陆
    怎么让FOXMAIL关了以后在右下角自动收取邮件
    在CentOS7上安装ftp服务器用于保存服务端上传的图片。
  • 原文地址:https://www.cnblogs.com/gzyx1988/p/5625816.html
Copyright © 2020-2023  润新知