From e8a62c1d4a7d23bf161c331bc9591b84cd220322 Mon Sep 17 00:00:00 2001 From: cheney Date: Mon, 18 May 2026 17:15:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E4=B8=AA=20xml=20=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.json | 6 ++ src/main/java/com/sample/Test.java | 125 +++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 config.json create mode 100644 src/main/java/com/sample/Test.java diff --git a/config.json b/config.json new file mode 100644 index 0000000..38d23fb --- /dev/null +++ b/config.json @@ -0,0 +1,6 @@ +{ + "ip1": "172.1.41.3", + "ip2": "172.1.41.139", + "port": 8889, + "dn": "C=cn,O=CFCA OCA1 SM2,OU=Local RA OCA1,OU=Units,CN=041@N91330108762038374D@杭州信雅达科技有限公司@00000002" +} \ No newline at end of file diff --git a/src/main/java/com/sample/Test.java b/src/main/java/com/sample/Test.java new file mode 100644 index 0000000..639d317 --- /dev/null +++ b/src/main/java/com/sample/Test.java @@ -0,0 +1,125 @@ +package com.sample; + +import cn.hutool.core.io.FileUtil; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import com.sunyard.SydApiException; +import racal.sunyard.main.SydApi4j; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class Test { + + private static final String odata = "BankAcctTransfer1710469760912INNERDEBIT966229539600100010640王二类1566217788308301020599王二类1402/402361018886安徽农金00.001001"; + + private static String ip1 = null; + private static String ip2 = null; + private static int port = 0; + private static String dn = null; + + + private static int success = 0; + private static int error = 0; + + private static void printInfo(){ + System.out.println(String.format("调用总数=%d\t成功总数=%d\t失败总数=%d", success+error, success, error)); + } + + private static void checkOK(){ + success += 1; + printInfo(); + } + + private static void checkError(){ + error += 1; + printInfo(); + } + + + public static void case1(){ + SydApi4j.SYD_Set_Parameter_HA(ip1, ip2, port, 3000); + SydApi4j api = (SydApi4j) new SydApi4j(); + try { + // 获取签名证书 + String cert1 = api.getCertByDn(dn,0); + // 获取加密证书 + String cert2 = api.getCertByDn(dn,0); + String odata = "BankAcctTransfer1710469760912INNERDEBIT966229539600100010640王二类1566217788308301020599王二类1402/402361018886安徽农金00.001001"; + + byte[] nakedSign = api.SYD_NakedSign(dn, odata.getBytes(), null); + boolean nakedVerify2 = api.SYD_NakedVerifyAll4NX(cert1,4, odata.getBytes(), nakedSign, dn); + + api.importCert(cert1.getBytes(), null); + api.importCert(cert2.getBytes(), null); + System.out.println("验签结果:" + nakedVerify2 ); + } catch (SydApiException e) { + throw e; + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } finally { + api.disconnect(); + } + } + + public static void case2(){ + SydApi4j.SYD_Set_Parameter_HA(ip1, ip2, port, 3000); + SydApi4j api = (SydApi4j) new SydApi4j(); + try { + String sde = api.detachedSignAll(4, odata.getBytes(), dn); + System.out.println("SM2detach签名:"+sde); + boolean bde = api.detachedVerifyAll(4, odata.getBytes(), sde); + System.out.println("SM2detach验签:"+bde); + } finally { + api.disconnect(); + } + + } + + + + public static void main(String[] args) { + System.out.println("载入配置文件 ./config.json"); + String workDir = System.getProperty("user.dir"); + Path filePath = Paths.get(workDir,"config.json"); + String configStr = FileUtil.readString(filePath.toFile(), StandardCharsets.UTF_8); + JSONObject config = JSONUtil.parseObj(configStr); + ip1 = config.getStr("ip1"); + ip2 = config.getStr("ip2"); + port = config.getInt("port"); + dn = config.getStr("dn"); + System.out.println("ip1=" + ip1); + System.out.println("ip2=" + ip2); + System.out.println("port=" + port); + System.out.println("dn=" + dn); + System.out.println("载入配置完成"); + + + + System.out.println("开始持续交易, HA 模式,短链接模式"); + while (true) { + try { + case1(); + checkOK(); + Thread.sleep(3000); + +// case2(); +// checkOK(); +// Thread.sleep(3000); + + + } catch (Exception e) { + System.err.println("发生错误"); + e.printStackTrace(); + checkError(); + try { + Thread.sleep(3000); + } catch (InterruptedException ex) { + throw new RuntimeException(ex); + } + } + } + } +}