public void testPay() throws Exception {
    StringBuilder sfb = new StringBuilder();
    String merId = "4110023900000628";
    String termId = "8454637507";
    String amount = "1";
    String token = "231433940394039403493"; //支付条码
    String requestId = String.valueOf(System.nanoTime());
    String payType = "0";
    String reservedOne = "1";
    String reservedTwo = "1";
    Map<String, Object> map = new TreeMap<>();
    map.put("merId", merId);
    map.put("termId", termId);
    map.put("amount", amount);
    map.put("token", token);
    map.put("requestId", requestId);
    map.put("payType", payType);
    map.put("reservedOne", reservedOne);
    map.put("reservedTwo", reservedTwo);
    map.forEach((k, v) -> sfb.append(v));
    String  key = "MIICdQIBAstU/KqZouo9NCkjs/cMxLqzQPx4+";
    String sign = RSAUtil.sign(sfb.toString().getBytes(),key);
    boolean flag = RSAUtil.verify(sfb.toString().getBytes(),publicKey,sign);
    map.put("sign",sign);
    String responseText = HttpUtil.post("http://URL ",map);
    System.out.println(responseText);
}

解决方案 »

  1.   

    看你java代码,    boolean flag = RSAUtil.verify(sfb.toString().getBytes(),publicKey,sign); 这一行貌似没作用,简单的写了了一下,貌似是某支付的API,应该有PHP版本的吧另外php不支持纳秒 这里粗略的写了一下。 RAS
    <?php
    $data = ['merId' => '4110023900000628',
        'termId' => 8454637507,
        'amount' => '',
        'token' => '231433940394039403493',
        'requestId' => time(),
        'payType' => 0,
        'reservedOne' => 1,
        'reservedTwo' => 1
    ];$string = '';
    foreach ($data as $k => $v) {
        $string .= sprintf("%s%s", $k, $v);
    }
    $data['sign'] = RSAUtil::sign($string, 'privatekey.pem');
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, "http://URL ");
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      // post数据
      curl_setopt($ch, CURLOPT_POST, 1);
      // post的变量
      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));  $output = curl_exec($ch);
      curl_close($ch);  //打印获得的数据
      print_r($output);
     /**
     * Created by PhpStorm.
     * User: dengpeng.zdp
     * Date: 2015/9/28
     * Time: 19:11
     */
    class RSAUtil{
        /**
         * 加签
         * @param $data 要加签的数据
         * @param $privateKeyFilePath 私钥文件路径
         * @return string 签名
         */
        public static function sign($data, $privateKeyFilePath) {
            $priKey = file_get_contents($privateKeyFilePath);
            $res = openssl_get_privatekey($priKey);
            openssl_sign($data, $sign, $res);
            openssl_free_key($res);
            $sign = base64_encode($sign);
            return $sign;
        }
        /**
         * 验签
         * @param $data 用来加签的数据
         * @param $sign 加签后的结果
         * @param $rsaPublicKeyFilePath 公钥文件路径
         * @return bool 验签是否成功
         */
        public static function verify($data, $sign, $rsaPublicKeyFilePath) {
            //读取公钥文件
            $pubKey = file_get_contents($rsaPublicKeyFilePath);
            //转换为openssl格式密钥
            $res = openssl_get_publickey($pubKey);
            //调用openssl内置方法验签,返回bool值
            $result = (bool)openssl_verify($data, base64_decode($sign), $res);
            //释放资源
            openssl_free_key($res);
            return $result;
        }
        /**
         * rsa加密
         * @param $data 要加密的数据
         * @param $pubKeyFilePath 公钥文件路径
         * @return string 加密后的密文
         */
        public static function rsaEncrypt($data, $pubKeyFilePath){
            //读取公钥文件
            $pubKey = file_get_contents($pubKeyFilePath);
            //转换为openssl格式密钥
            $res = openssl_get_publickey($pubKey);
            $maxlength = RSAUtil::getMaxEncryptBlockSize($res);
            $output='';
            while($data){
                $input= substr($data,0,$maxlength);
                $data=substr($data,$maxlength);
                openssl_public_encrypt($input,$encrypted,$pubKey);
                $output.= $encrypted;
            }
            $encryptedData =  base64_encode($output);
            return $encryptedData;
        }
        /**
         * 解密
         * @param $data 要解密的数据
         * @param $privateKeyFilePath 私钥文件路径
         * @return string 解密后的明文
         */
        public static function rsaDecrypt($data, $privateKeyFilePath){
            //读取私钥文件
            $priKey = file_get_contents($privateKeyFilePath);
            //转换为openssl格式密钥
            $res = openssl_get_privatekey($priKey);
            $data = base64_decode($data);
            $maxlength = RSAUtil::getMaxDecryptBlockSize($res);
            $output='';
            while($data){
                $input = substr($data,0,$maxlength);
                $data = substr($data,$maxlength);
                openssl_private_decrypt($input,$out,$res);
                $output.=$out;
            }
            return $output;
        }
        /**
         *根据key的内容获取最大加密lock的大小,兼容各种长度的rsa keysize(比如1024,2048)
         * 对于1024长度的RSA Key,返回值为117
         * @param $keyRes
         * @return float
         */
        public static function getMaxEncryptBlockSize($keyRes){
            $keyDetail = openssl_pkey_get_details($keyRes);
            $modulusSize = $keyDetail['bits'];
            return $modulusSize/8 - 11;
        }
        /**
         * 根据key的内容获取最大解密block的大小,兼容各种长度的rsa keysize(比如1024,2048)
         * 对于1024长度的RSA Key,返回值为128
         * @param $keyRes
         * @return float
         */
        public static function getMaxDecryptBlockSize($keyRes){
            $keyDetail = openssl_pkey_get_details($keyRes);
            $modulusSize = $keyDetail['bits'];
            return $modulusSize/8;
        }
    }