• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

phpAES-128-CBC加密通信java

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<?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\n"; print_r($index); echo "\nVals array\n"; 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; } }?>




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php输出、写入csv发布时间:2022-07-10
下一篇:
小项目:聊天室(jQuery,PHP,MySQL)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap