Test

package fuiou.sdk;

import java.io.IOException;
import net.sf.json.JSONObject;
import fuiou.sdk.CommonReq;
import fuiou.sdk.QueryOrderReq;

public class TokenTest {

    //测试环境查询接口地址
    public static String url = "详情请咨询富友对接群"; 
    //测试环境商户号
     public static String mchnt_cd = "0002900F0096235";
     //测试环境商户私钥
     public static String mchnt_pri_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDIQi1d74mvJr9nMWgZbqT6A35L3rRfp2trpxR/zmh4lMoMLDFCSwoKt/oEZrQeNIIVXf8W/gVUCq6MUs63pvTVF90f1CDJUmdOOZYuJrUcMwa534qna+OhV4ikP6bV74OMpeR/yZ2Azu3PB+k/qkn5jeeIwPghqqtfD5bWCCxYmNLkajJPQu67vHxJKFOiGSO/8b1nSgGR9MXXTR+IYWIWNL+HshPPdZ9GtKsajuatscVc35d7CFSFCa09FSJnf3/O95ztAk768W82o8JF5lGdFiCKmmFKaQh/J4TEtLnvzFkOa+Ezqy/UlHhB/QIX1A1X5js6TovXWNXsxFB3mKLPAgMBAAECggEAP8PytiqD6OWppK4yu4ZjuchK+tWbEao5pjm5aUjMMzkNbPrVZIqIVAlo7uXVDJkjorIfe66++5e7JKtpb0kWRJQosO3W0sQOaPYlP+TLXoNQ7PalK9JK1SEOPV6XPR/a/3oDXD1SjaVWS8n1F3SdwXzR8NWhLPcvQkGwgc5wcHUIzViofLw4TO8LR+U5tjoEK4v3HEG242Q36QB+LTNKrv974RN1niFiDDhoryzIGvEJuD/XQUzZUdLXiTegTeOszyS0wH4RTMQA/MPU60rjOpH1tRsqmmwOHF64tioElCz6FSO8FV/M7myxx8DViHD4wf2OJoddaez1F471hKB2IQKBgQDpz2ajHrXA9UXkPVKMXRtewmpnY93zjtYrdGvQlSR8qgAAFNMJTYWGhsfMyW632NGu5PVB3AOSLw6z0JHNBUFOWfndoWco++Uq7rP2XtGfjsW1Y9KZY9eYqg59tW5XVf+b9fLX+mHyyt6dnUyZ0/8reol9SSC7HigE3bJQtZnrRQKBgQDbQ5zhtXlwmEvNTPH5YIXpCbJJ/QhnQfDHqcglODZ4KfvKe5CX9TdkygDtVmhGruGAsJ8dni7g37gGkvIEf0r3qqfTi/OmOz3B17Po9opV1biwjGT07YDkwweYJlqE+OEGrNbX8YvyIRjTklqRRapfZj17f/SAekDf670xOcztAwKBgA+Tf5TyYM1gCa3w3+T19vLTJN+KaajmJSJz+RLGPM4JTUK0IHMINRMHO5xK9xNMYHu0SRqAG8TLWcFk7bGo3fboS5sWO5chYN0BVf/dxT1J96Xj5o3Ndvl2qsx5H8a1tfef5PO6fHf+R+JcLlQ5KVCuMyajjoios6XPVyfFUWG5AoGAFkg0tStAABU5/s8G+BS7BweZOrOgGv40fdXfJQklX+jEUbtk4kxlKLTuiy/wwN4cxHsucFAW63tQtDpSpepZr/mDvJfIVuhGSmClQ9NqE3b4odhimhFYu+al6uxa3pB8LbV6z88YLa73WujQk1rFCZTWQqqqKjRCS38lZs886AkCgYEAsDS2JwC2XCMpXGGt/dOksFgfFTqJPRjtryMICHlYnJyEVe46ZDfXIS8XXrNIEg6OikScIwJWXvcSIxXcUOCjuNlCHVoJanp5b/p+5Y4g5Ljzy0FtfFC8JBd3VhC6grwuNOGQuSgx8dQGJIFfshFIkeXC7IjiDfQxPsoCCOmigWo=";
    //测试环境机构公钥
     public static String ins_pub_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDWbeSPxvfbpY2uWHbX2YXcrHXa88NITHajSmjTWdY0wqS9WqF8OyMT3ucIEuumP20/5D5+OaSETSBxrD7+1HA87TdTcCLn/FUuzVWiu4cD7Oln2/FJDwPxC/5/u14APBU30aLRWJbPVu0W8ZPmCF8718fAiYjP0RjvXU0gSnA/wwIDAQAB";

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

    private static void postRequestTest() throws IOException {  
        //查询
        QueryOrderReq query = new QueryOrderReq();
        query.setMchnt_cd(mchnt_cd);
        query.setOrder_date("20200513");
        query.setOrder_id("F123456");
        query.setVer("1.0.0");
        JSONObject jsonParam = JSONObject.fromObject(query); 
        //用富友公钥加密得到密文。
        String message = RsaUtil.encryptByRsaPub(jsonParam.toString(), ins_pub_key, "UTF-8");

        CommonReq req = new CommonReq();
        req.setMchnt_cd("0002900F0096235");
        req.setMessage(message);

        JSONObject reqJson = JSONObject.fromObject(req); 
        JSONObject respJson = HttpRequestUtil.httpPost(url, reqJson);
        System.out.println("返回的报文=="+respJson);
        message = respJson.getString("message");
        //商户私钥解密出来密文
        String plain = RsaUtil.decryptByRsaPri(message, ins_pub_key, "GBK");
        System.out.println("商户私钥解密出来的明文plain=="+plain);
    }  
}

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 ""