php hex加密解密工具方法

没有评论

2014 年 05 月 14 日 at 上午 9:35分类:PHP

//Hex(Hex(Md5((原文+密钥).getBytes(“utf-8”))).getBytes(“utf-8”))
function String2Hex($string){
    $hex='';
    for ($i=0; $i < strlen($string); $i++){
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}

function Hex2String($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

// example:

$hex = String2Hex("test sentence...");

$data = array(
    'appId' => 'SHAN-GUO-CHI-BI', 
    'data' =>'{"appId":"SHAN-GUO-CHI-BI","cardNo":"6006012000000001"}', 
    'sig'=>'4331443139334331313645464642444544364334343742374236364335454243',
);

$key = '111111111111111111111111';

$data['sig'] = String2Hex(strtoupper(md5($data['data'] . $key)));