示例代码

Test

package fuiou.sdk;

import java.io.IOException;

import net.sf.json.JSONObject;
import fuiou.sdk.CommonReq;
import fuiou.sdk.QueryOrderReq;

public class Test {

    public static void main(String[] args) {  
        try {
            postRequestTest();
        } catch (IOException e) {
            e.printStackTrace();
        }  
    }  

    private static void postRequestTest() throws IOException {  
        //查询
        String url = "https://aggpay-test.fuioupay.com/query.fuiou"; 
        QueryOrderReq query = new QueryOrderReq();
        query.setMchnt_cd("富友分配入网商户代码");
        query.setPaymode_cd("支付方式");//比如:0000000000,QUICKPAY
        query.setOrder_date("20200513");
        query.setOrder_id("F123456");
        query.setVer("1.0.0");
        JSONObject jsonParam = JSONObject.fromObject(query); 
        //加密得到message
        String message = RsaUtil.encryptByRsaPub(jsonParam.toString(), "富友公钥", "GBK");

        CommonReq req = new CommonReq();
        req.setMchnt_cd("富友分配入网商户代码");
        req.setMessage(message);

        JSONObject reqJson = JSONObject.fromObject(req); 
         JSONObject respJson = HttpRequestUtil.httpPost(url, reqJson);
         System.out.println(respJson);
    }  
}

RsaUtil

package fuiou.sdk;

import java.security.Key;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;

public class RsaUtil {

    private static final String KEY_ALGORITHM = "RSA";
    private static final String ALGORITHM_SHA1 = "SHA1withRSA";
    private static final String ALGORITHM_SHA2 = "SHA256withRSA";
    private static final String ALGORITHM_MD5RSA = "MD5WithRSA";

    static {
        try {
            java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** SHA1withRSA     */
    public static String sign2Base64(String base64PriRsa, String inputStr) {
        return sign2Base64ByAlg(base64PriRsa, inputStr, ALGORITHM_SHA1, "GBK");
    }

    /** SHA256withRSA */
    public static String sign2Base64BySha2(String base64PriRsa, String inputStr) {
        return sign2Base64ByAlg(base64PriRsa, inputStr, ALGORITHM_SHA2,"GBK");
    }
    /** MD5WithRSA  */
    public static String sign2Base64ByMd5Rsa(String base64PriRsa, String inputStr) {
        return sign2Base64ByAlg(base64PriRsa, inputStr, ALGORITHM_MD5RSA,"GBK");
    }

    /** SHA1withRSA     */
    public static String sign2Base64(String base64PriRsa, String inputStr,String charset) {
        return sign2Base64ByAlg(base64PriRsa, inputStr, ALGORITHM_SHA1,charset);
    }

    /** SHA256withRSA */
    public static String sign2Base64BySha2(String base64PriRsa, String inputStr,String charset) {
        return sign2Base64ByAlg(base64PriRsa, inputStr, ALGORITHM_SHA2,charset);
    }
    /** MD5WithRSA  */
    public static String sign2Base64ByMd5Rsa(String base64PriRsa, String inputStr,String charset) {
        return sign2Base64ByAlg(base64PriRsa, inputStr, ALGORITHM_MD5RSA,charset);
    }

    private static String sign2Base64ByAlg(String base64PriRsa, String inputStr, String algorithm,String charset) {
        try {
            PrivateKey privateKey = genPrivateKey(base64PriRsa);
            Signature signature = Signature.getInstance(algorithm, "BC");
            signature.initSign(privateKey);
            signature.update(inputStr.getBytes(charset));
            return Base64.encodeBase64String(signature.sign());
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("");
        }
    }

    /**
     * SHA1withRSA
     * @return
     */
    public static boolean verifySign(String base64PubRsa, String src, String base64SignValue) {
        return verifySignByAlg(base64PubRsa, src, base64SignValue, ALGORITHM_SHA1, "GBK");
    }

    /** 
     * SHA256withRSA 
     * @return
     */
    public static boolean verifySignBySha2(String base64PubRsa, String src, String base64SignValue) {
        return verifySignByAlg(base64PubRsa, src, base64SignValue, ALGORITHM_SHA2, "GBK");
    }

    /** 
     * MD5WithRSA  
     * @return
     */
    public static boolean verifySignByMd5Rsa(String base64PubRsa, String src, String base64SignValue) {
        return verifySignByAlg(base64PubRsa, src, base64SignValue, ALGORITHM_MD5RSA, "GBK");
    }

    /**
     * SHA1withRSA
     * @return
     */
    public static boolean verifySign(String base64PubRsa, String src, String base64SignValue,String charset) {
        return verifySignByAlg(base64PubRsa, src, base64SignValue, ALGORITHM_SHA1,charset);
    }

    /** SHA256withRSA
     *  @return
     */
    public static boolean verifySignBySha2(String base64PubRsa, String src, String base64SignValue,String charset) {
        return verifySignByAlg(base64PubRsa, src, base64SignValue, ALGORITHM_SHA2,charset);
    }

    /**MD5WithRSA  */
    public static boolean verifySignByMd5Rsa(String base64PubRsa, String src, String base64SignValue,String charset) {
        return verifySignByAlg(base64PubRsa, src, base64SignValue, ALGORITHM_MD5RSA,charset);
    }

    private static boolean verifySignByAlg(String base64PubRsa, String src, String base64SignValue, String algorithm,String charset) {
        try {
            PublicKey publicKey = genPublicKey(base64PubRsa);
            Signature signature = Signature.getInstance(algorithm, "BC");
            signature.initVerify(publicKey);
            signature.update(src.getBytes(charset));
            return signature.verify(Base64.decodeBase64(base64SignValue));
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("RSA异常");
        }
    }

    private static PrivateKey genPrivateKey(String base64Rsa) {
        try {
            KeyFactory kf = KeyFactory.getInstance(KEY_ALGORITHM, "BC");
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(base64Rsa));
            return kf.generatePrivate(keySpec);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("RSA异常");
        }
    }

    private static PublicKey genPublicKey(String base64Rsa) {
        try {
            KeyFactory kf = KeyFactory.getInstance(KEY_ALGORITHM, "BC");
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decodeBase64(base64Rsa));
            return kf.generatePublic(keySpec);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("RSA异常");
        }
    }

    public static String convertPubRsaFile2Str(String fileStr){
        PublicKey pk = genPublicKey(fileStr);
        return Base64.encodeBase64String(pk.getEncoded());
    }

    public static String convertPriRsaFile2Str(String fileStr){
        PrivateKey pk = genPrivateKey(fileStr);
        return Base64.encodeBase64String(pk.getEncoded());
    }



    public static String encryptByRsaPub(String src, String base64PubRsa) {
        try {
            PublicKey publicKey = genPublicKey(base64PubRsa);
            byte[] encryptBytes = encryptByKey(src.getBytes("GBK"), publicKey);
            return Base64.encodeBase64String(encryptBytes);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("RSA异常");
        }
    }

    public static String encryptByRsaPub(String src, String base64PubRsa,String charset) {
        try {
            PublicKey publicKey = genPublicKey(base64PubRsa);
            byte[] encryptBytes = encryptByKey(src.getBytes(charset), publicKey);
            return Base64.encodeBase64String(encryptBytes);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("RSA异常");
        }
    }

    public static String encryptByRsaPri(String src, String base64PubRsa) {
        try {
            PrivateKey privateKey = genPrivateKey(base64PubRsa);
            byte[] encryptBytes = encryptByKey(src.getBytes("GBK"), privateKey);
            return Base64.encodeBase64String(encryptBytes);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("RSA异常");
        }
    }

    public static String encryptByRsaPri(String src, String base64PubRsa,String charset) {
        try {
            PrivateKey privateKey = genPrivateKey(base64PubRsa);
            byte[] encryptBytes = encryptByKey(src.getBytes(charset), privateKey);
            return Base64.encodeBase64String(encryptBytes);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("RSA异常");
        }
    }

    public static String decryptByRsaPri(String base64Src, String base64PriRsa) {
        try {
            PrivateKey privateKey = genPrivateKey(base64PriRsa);
            return new String(decryptKey(Base64.decodeBase64(base64Src), privateKey), "GBK");
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("RSA异常");
        }
    }

    public static String decryptByRsaPri(String base64Src, String base64PriRsa,String charset) {
        try {
            PrivateKey privateKey = genPrivateKey(base64PriRsa);
            return new String(decryptKey(Base64.decodeBase64(base64Src), privateKey), charset);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("RSA异常");
        }
    }

    public static String decryptByRsaPub(String base64Src, String base64PriRsa) {
        try {
            PublicKey publicKey = genPublicKey(base64PriRsa);
            return new String(decryptKey(Base64.decodeBase64(base64Src), publicKey), "GBK");
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("RSA异常");
        }
    }

    public static String decryptByRsaPub(String base64Src, String base64PriRsa,String charset) {
        try {
            PublicKey publicKey = genPublicKey(base64PriRsa);
            return new String(decryptKey(Base64.decodeBase64(base64Src), publicKey),charset);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("RSA异常");
        }
    }

    /**
     * 用公钥加密
     */
    private static byte[] encryptByKey(byte[] srcData, Key publicKey) {
        try {
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            int blockSize = cipher.getOutputSize(srcData.length) - 11;
            byte[] encryptedData = null;
            for (int i = 0; i < srcData.length; i += blockSize) {
                byte[] doFinal = cipher.doFinal(subarray(srcData, i, i + blockSize));
                encryptedData = addAll(encryptedData, doFinal);
            }
            return encryptedData;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static byte[] decryptKey(byte[] srcData, Key key) {
        try {
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.DECRYPT_MODE, key);
            int blockSize = cipher.getOutputSize(srcData.length);
            byte[] decryptData = null;

            for (int i = 0; i < srcData.length; i += blockSize) {
                byte[] doFinal = cipher.doFinal(subarray(srcData, i, i + blockSize));

                decryptData = addAll(decryptData, doFinal);
            }
            return decryptData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static byte[] subarray(byte[] array, int startIndexInclusive, int endIndexExclusive) {
        if (array == null) {
            return null;
        }
        if (startIndexInclusive < 0) {
            startIndexInclusive = 0;
        }
        if (endIndexExclusive > array.length) {
            endIndexExclusive = array.length;
        }
        int newSize = endIndexExclusive - startIndexInclusive;

        if (newSize <= 0) {
            return new byte[0];
        }

        byte[] subarray = new byte[newSize];

        System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);

        return subarray;
    }

    private static byte[] addAll(byte[] array1, byte[] array2) {
        if (array1 == null) {
            return clone(array2);
        } else if (array2 == null) {
            return clone(array1);
        }
        byte[] joinedArray = new byte[array1.length + array2.length];
        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
        return joinedArray;
    }

    public static byte[] clone(byte[] array) {
        if (array == null) {
            return null;
        }
        return (byte[]) array.clone();
    }
}

查询订单请求数据

package fuiou.sdk;

public class QueryOrderReq {
    //商户代码
    private String mchnt_cd = "";
    //支付方式
    private String paymode_cd = "";
    //订单日期
    private String order_date = "";
    //订单流水号
    private String order_id = "";
    //版本号 默认1.0.0
    private String ver = "";

    public String getMchnt_cd() {
        return mchnt_cd;
    }
    public void setMchnt_cd(String mchnt_cd) {
        this.mchnt_cd = mchnt_cd;
    }
    public String getPaymode_cd() {
        return paymode_cd;
    }
    public void setPaymode_cd(String paymode_cd) {
        this.paymode_cd = paymode_cd;
    }
    public String getOrder_date() {
        return order_date;
    }
    public void setOrder_date(String order_date) {
        this.order_date = order_date;
    }
    public String getOrder_id() {
        return order_id;
    }
    public void setOrder_id(String order_id) {
        this.order_id = order_id;
    }
    public String getVer() {
        return ver;
    }
    public void setVer(String ver) {
        this.ver = ver;
    }
}

HttpRequestUtil http请求工具

package fuiou.sdk;

import java.io.IOException;
import java.net.URLDecoder;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpRequestUtil {

    public static JSONObject httpGet(String url){  
        JSONObject jsonResult = null;  
        try {  
            DefaultHttpClient client = new DefaultHttpClient();  
            HttpGet request = new HttpGet(url);  
            HttpResponse response = client.execute(request);  

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
                String strResult = EntityUtils.toString(response.getEntity());  
                jsonResult = JSONObject.fromObject(strResult);  
                url = URLDecoder.decode(url, "UTF-8");  
            } else {  

            }  
        } catch (IOException e) {  
            e.printStackTrace();
        }  
        return jsonResult;  
    }  

    public static JSONObject httpPost(String url,JSONObject jsonParam){  
        return httpPost(url, jsonParam.toString(), false);  
    }  


    public static JSONObject httpPost(String url,String json, boolean noNeedResponse){  
        DefaultHttpClient httpClient = new DefaultHttpClient();  
        JSONObject jsonResult = null;  
        HttpPost method = new HttpPost(url);  
        try {  
            if (null != json) {  
                StringEntity entity = new StringEntity(json, "utf-8");  
                entity.setContentEncoding("UTF-8");  
                entity.setContentType("application/json");  
                method.setEntity(entity);  
            }  
            HttpResponse result = httpClient.execute(method);  
            url = URLDecoder.decode(url, "UTF-8");  
            if (result.getStatusLine().getStatusCode() == 200) {  
                String str = "";  
                try {  
                    str = EntityUtils.toString(result.getEntity());  
                    if (noNeedResponse) {  
                        return null;  
                    }  
                    jsonResult = JSONObject.fromObject(str);  
                } catch (Exception e) {  
                    e.printStackTrace();
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace();
        }  
        return jsonResult;  
    }  
}

公共请求数据

package fuiou.sdk;

public class CommonReq {
    //商户代码
    private String mchnt_cd;
    //密文
    private String message;
    public String getMchnt_cd() {
        return mchnt_cd;
    }
    public void setMchnt_cd(String mchnt_cd) {
        this.mchnt_cd = mchnt_cd;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

results matching ""

    No results matching ""