From f7fa77767756fad3b963547c9aa08676d8c3d992 Mon Sep 17 00:00:00 2001 From: cheney Date: Fri, 29 May 2026 17:18:36 +0800 Subject: [PATCH 01/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/cmbpoc/JmeterTest.java | 67 ++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/test/java/cmbpoc/JmeterTest.java diff --git a/src/test/java/cmbpoc/JmeterTest.java b/src/test/java/cmbpoc/JmeterTest.java new file mode 100644 index 0000000..224e19e --- /dev/null +++ b/src/test/java/cmbpoc/JmeterTest.java @@ -0,0 +1,67 @@ +package cmbpoc; + + +import org.apache.jmeter.config.Arguments; +import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; +import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; +import org.apache.jmeter.samplers.SampleResult; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +public class JmeterTest extends AbstractJavaSamplerClient { + + private byte[] publicKey; + private byte[] plainData; + + @Override + public Arguments getDefaultParameters() { + Arguments params = new Arguments(); + params.addArgument("publicKeyBase64", "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE..."); + params.addArgument("plainText", "Hello JMeter"); + return params; + } + + @Override + public void setupTest(JavaSamplerContext context) { + // 初始化:将 Base64 参数转为字节数组,避免每个循环都转换 + String pubBase64 = context.getParameter("publicKeyBase64"); + String plainText = context.getParameter("plainText"); + publicKey = Base64.getDecoder().decode(pubBase64); + plainData = plainText.getBytes(StandardCharsets.UTF_8); + } + + @Override + public SampleResult runTest(JavaSamplerContext context) { + SampleResult result = new SampleResult(); + // 调用待测函数 + String function = context.getParameter("function"); + + result.sampleStart(); // 开始计时 + try { + switch (function) { + case "SYD_SM2_Sign": + break; + default: + break; + } + + // result.setResponseData(encrypted); // 可选 + result.setSuccessful(true); + result.setResponseCodeOK(); + result.setResponseMessage( function + "执行成功" ); + } catch (Exception e) { + result.setSuccessful(false); + result.setResponseCode("500"); + result.setResponseMessage( function + "执行失败。" + e.getMessage()); + } finally { + result.sampleEnd(); // 结束计时 + } + return result; + } + + @Override + public void teardownTest(JavaSamplerContext context) { + // 释放资源(如有) + } +} \ No newline at end of file From 525987678005d7bb896bd724bf19ebb730f65674 Mon Sep 17 00:00:00 2001 From: cheney Date: Fri, 29 May 2026 18:03:08 +0800 Subject: [PATCH 02/18] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + src/test/java/cmbpoc/JmeterTest.java | 17 ++++ src/test/java/cmbpoc/SignatureVerifyTest.java | 84 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/test/java/cmbpoc/SignatureVerifyTest.java diff --git a/.gitignore b/.gitignore index e240a93..f1869e1 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ src/test/java/com/sunyard/sydapi/sample/*.* ~$* /pack/ /.logs/ +/bin/ diff --git a/src/test/java/cmbpoc/JmeterTest.java b/src/test/java/cmbpoc/JmeterTest.java index 224e19e..8a677c8 100644 --- a/src/test/java/cmbpoc/JmeterTest.java +++ b/src/test/java/cmbpoc/JmeterTest.java @@ -13,6 +13,7 @@ public class JmeterTest extends AbstractJavaSamplerClient { private byte[] publicKey; private byte[] plainData; + private int sleep = 1; @Override public Arguments getDefaultParameters() { @@ -29,6 +30,19 @@ public class JmeterTest extends AbstractJavaSamplerClient { String plainText = context.getParameter("plainText"); publicKey = Base64.getDecoder().decode(pubBase64); plainData = plainText.getBytes(StandardCharsets.UTF_8); + + // 校准测试 + sleep = context.getIntParameter("sleep", 1); + + // 线程开始时准备解密、验签数据 + String function = context.getParameter("function"); + switch (function) { + case "SYD_SM2_Sign": + break; + default: + break; + } + } @Override @@ -42,6 +56,9 @@ public class JmeterTest extends AbstractJavaSamplerClient { switch (function) { case "SYD_SM2_Sign": break; + case "sleep": + Thread.sleep(1); + break; default: break; } diff --git a/src/test/java/cmbpoc/SignatureVerifyTest.java b/src/test/java/cmbpoc/SignatureVerifyTest.java new file mode 100644 index 0000000..8bf43cd --- /dev/null +++ b/src/test/java/cmbpoc/SignatureVerifyTest.java @@ -0,0 +1,84 @@ +package cmbpoc; + +import com.sunyard.RetWrap; +import com.sunyard.SydApi; +import com.sunyard.proto.Util; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import racal.sunyard.main.SydApi4j; + +public class SignatureVerifyTest { + + private SydApi4j api; + private byte[] orgData; + private String SM2dn; + + @Before + public void setUp() { + System.setProperty("com.sunyard.sydapi4j.debug", "true"); + this.api = (SydApi4j) new SydApi4j().connect("192.168.0.100", 8889, null, 1000); + this.orgData = "Hello, World! 测试数据".getBytes(); + this.SM2dn = "C=CN,ST=北京,L=北京,O=信雅达,CN=172.1.40.188"; + } + + @After + public void tearDown() { + if (api != null) { + this.api.disconnect(); + this.api = null; + } + } + + @Test + public void testP1SignAndVerify() { + byte[] sign = this.api.SYD_NakedSign(orgData, SM2dn); + Assert.assertNotNull("P1签名结果不应为空", sign); + System.out.println("P1签名结果: " + Util.bytes2HexString(sign)); + + boolean verifyResult = this.api.SYD_NakedVerify(orgData, sign, SM2dn); + Assert.assertTrue("P1验签应通过", verifyResult); + System.out.println("P1验签结果: " + verifyResult); + } + + @Test + public void testP7DetachSignAndVerify() { + String sign = this.api.detachedSign(1, orgData, SM2dn); + Assert.assertNotNull("P7 Detach签名结果不应为空", sign); + System.out.println("P7 Detach签名结果: " + sign); + + boolean verifyResult = this.api.detachedVerify(1, orgData, sign); + Assert.assertTrue("P7 Detach验签应通过", verifyResult); + System.out.println("P7 Detach验签结果: " + verifyResult); + } + + @Test + public void testP7AttachSignAndVerify() throws Exception { + String sign = this.api.attachedSign(orgData, SM2dn); + Assert.assertNotNull("P7 Attach签名结果不应为空", sign); + System.out.println("P7 Attach签名结果: " + sign); + + boolean verifyResult = this.api.attachedVerify(sign); + Assert.assertTrue("P7 Attach验签应通过", verifyResult); + System.out.println("P7 Attach验签结果: " + verifyResult); + } + + @Test + public void testDigitalEnvelopeEncryptAndDecrypt() { + RetWrap genResult = this.api.generateDEByDn(SM2dn, orgData); + Assert.assertNotNull("数字信封生成结果不应为空", genResult); + + byte[] cipherKey = (byte[]) genResult.get(SydApi.RET_CIPHER_KEY); + Assert.assertNotNull("数字信封密文密钥不应为空", cipherKey); + System.out.println("数字信封密文密钥: " + new String(cipherKey)); + + RetWrap decryptResult = this.api.decryptDEByDN(SM2dn, cipherKey); + Assert.assertNotNull("数字信封解密结果不应为空", decryptResult); + + byte[] decryptedData = (byte[]) decryptResult.get("oData"); + Assert.assertNotNull("解密数据不应为空", decryptedData); + Assert.assertArrayEquals("解密数据应与原始数据一致", orgData, decryptedData); + System.out.println("数字信封解密结果: " + new String(decryptedData)); + } +} \ No newline at end of file From 385e77b766a6f0ed469ed49047555bec91a912f0 Mon Sep 17 00:00:00 2001 From: cheney Date: Mon, 1 Jun 2026 10:02:49 +0800 Subject: [PATCH 03/18] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=EF=BC=8Cindex=20=E6=94=B9=E4=B8=BA=204=20=E5=AD=97=E8=8A=82?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/racal/sunyard/main/SydApi4j.java | 8 +++--- src/test/java/cmbpoc/FunctionTest.java | 28 ++++++++++++++----- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index 1ae4b6d..96f6997 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -6907,12 +6907,12 @@ public class SydApi4j implements SydApi { if ( 1 == dataType ) { // 数据 bb.put(ByteUtil.shortToBytes((short) pOrgData.limit())); bb.put(pOrgData); - bb.put(ByteUtil.shortToBytes((short)idx)); + bb.put(ByteUtil.intToBytes(idx)); } if ( 3 == dataType) { // 仅数据长度 bb.put( ByteUtil.shortToBytes((short) (63*1024)) ); - bb.put( ByteUtil.shortToBytes((short)idx)); + bb.put( ByteUtil.intToBytes(idx)); } @@ -7042,11 +7042,11 @@ public class SydApi4j implements SydApi { if (dataType == 1) { // 密文块 bb.put(ByteUtil.shortToBytes((short) pCipherData.limit())); bb.put(pCipherData); - bb.put(ByteUtil.shortToBytes((short) idx)); + bb.put(ByteUtil.intToBytes(idx)); } if (dataType == 3) { // 请求明文块 bb.put(ByteUtil.shortToBytes((short) (63 * 1024))); - bb.put(ByteUtil.shortToBytes((short) idx)); + bb.put(ByteUtil.intToBytes(idx)); } // 填写总长度(长度字段本身不包含在内) diff --git a/src/test/java/cmbpoc/FunctionTest.java b/src/test/java/cmbpoc/FunctionTest.java index decba4a..6e1ede9 100644 --- a/src/test/java/cmbpoc/FunctionTest.java +++ b/src/test/java/cmbpoc/FunctionTest.java @@ -1,10 +1,8 @@ package cmbpoc; +import com.sunyard.RetWrap; import com.sunyard.proto.Util; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.*; import racal.sunyard.main.SydApi4j; public class FunctionTest { @@ -22,7 +20,7 @@ public class FunctionTest { System.setProperty("com.sunyard.sydapi4j.debug", "true"); // 建立链接(单台) - this.api = (SydApi4j) new SydApi4j().connect("192.168.0.100", 8889, null, 1000); + this.api = (SydApi4j) new SydApi4j().connect("192.168.21.166", 8889, null, 1000); } @@ -37,13 +35,29 @@ public class FunctionTest { + @Ignore + @Test + public void genKeyPair(){ + RetWrap r = this.api.SYD_SM2GenKeyPair(); + String pk = r.get("publicKey").toString(); + String sk = r.get("privateKey").toString(); + + System.out.println("pk=" + pk); + System.out.println("sk=" + sk); + + this.privateKey = Util.hexString2Bytes(sk); + this.publicKey = Util.hexString2Bytes(pk); + } + + + + @Ignore @Test public void getPrivateKeyAndPublickKey(){ this.publicKey = Util.hexString2Bytes( this.api.SM2GetPublicKeyC(keypairIndex) ); - this.privateKey = Util.hexString2Bytes( this.api.SM2GetPrivateKeyC(keypairIndex) ); @@ -61,7 +75,7 @@ public class FunctionTest { @Test public void SYD_SM2_Encrypt(){ - byte[] enData = this.api.SYD_SM2_Encrypt(publicKey, orgData); + byte[] enData = this.api.SYD_SM2Encrypt(publicKey, orgData); byte[] deData = this.api.SYD_SM2Decrypt(privateKey, enData); Assert.assertArrayEquals(enData, deData); } From 88f4bdf603ec28bf6483366c6a674ff22d19efc5 Mon Sep 17 00:00:00 2001 From: cheney Date: Mon, 1 Jun 2026 13:41:28 +0800 Subject: [PATCH 04/18] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assembly.xml | 30 +++++++++++++++++++ pom.xml | 21 +++---------- src/test/java/cmbpoc/FunctionTest.java | 2 +- src/test/java/cmbpoc/JmeterTest.java | 14 ++------- src/test/java/cmbpoc/SignatureVerifyTest.java | 2 +- .../Sample => test/java/sample}/SydUtils.java | 2 +- .../java/com => test/java}/sample/Test.java | 2 +- src/test/java/sample/Test4NongxinCW.java | 3 -- .../Sample => test/java/sample}/TestDemo.java | 2 +- .../java}/test/Test4Jemter.java | 4 +-- 10 files changed, 44 insertions(+), 38 deletions(-) create mode 100644 assembly.xml rename src/{main/java/com/Sample => test/java/sample}/SydUtils.java (97%) rename src/{main/java/com => test/java}/sample/Test.java (99%) rename src/{main/java/com/Sample => test/java/sample}/TestDemo.java (99%) rename src/{main/java/com/jmeter => test/java}/test/Test4Jemter.java (99%) diff --git a/assembly.xml b/assembly.xml new file mode 100644 index 0000000..139b587 --- /dev/null +++ b/assembly.xml @@ -0,0 +1,30 @@ + + assembled-test-jar + + jar + + false + + + + ${project.build.outputDirectory} + / + + + + ${project.build.testOutputDirectory} + / + + + + + + / + true + runtime + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8e7a580..7d45eda 100644 --- a/pom.xml +++ b/pom.xml @@ -182,22 +182,6 @@ - - - - org.apache.maven.plugins - maven-jar-plugin - - - - true - lib/ - com.xxx.uploadFile - - - - - org.apache.maven.plugins maven-dependency-plugin @@ -224,9 +208,12 @@ maven-assembly-plugin + + assembly.xml + - com.Sample.TestDemo + sample.TestDemo diff --git a/src/test/java/cmbpoc/FunctionTest.java b/src/test/java/cmbpoc/FunctionTest.java index 6e1ede9..c1d6491 100644 --- a/src/test/java/cmbpoc/FunctionTest.java +++ b/src/test/java/cmbpoc/FunctionTest.java @@ -20,7 +20,7 @@ public class FunctionTest { System.setProperty("com.sunyard.sydapi4j.debug", "true"); // 建立链接(单台) - this.api = (SydApi4j) new SydApi4j().connect("192.168.21.166", 8889, null, 1000); + this.api = (SydApi4j) new SydApi4j().connect("192.168.21.166", 8889, null, 5000); } diff --git a/src/test/java/cmbpoc/JmeterTest.java b/src/test/java/cmbpoc/JmeterTest.java index 8a677c8..eed2fca 100644 --- a/src/test/java/cmbpoc/JmeterTest.java +++ b/src/test/java/cmbpoc/JmeterTest.java @@ -6,8 +6,6 @@ import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; -import java.nio.charset.StandardCharsets; -import java.util.Base64; public class JmeterTest extends AbstractJavaSamplerClient { @@ -18,19 +16,13 @@ public class JmeterTest extends AbstractJavaSamplerClient { @Override public Arguments getDefaultParameters() { Arguments params = new Arguments(); - params.addArgument("publicKeyBase64", "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE..."); - params.addArgument("plainText", "Hello JMeter"); + params.addArgument("function", "sleep"); + params.addArgument("sleep", "1"); return params; } @Override public void setupTest(JavaSamplerContext context) { - // 初始化:将 Base64 参数转为字节数组,避免每个循环都转换 - String pubBase64 = context.getParameter("publicKeyBase64"); - String plainText = context.getParameter("plainText"); - publicKey = Base64.getDecoder().decode(pubBase64); - plainData = plainText.getBytes(StandardCharsets.UTF_8); - // 校准测试 sleep = context.getIntParameter("sleep", 1); @@ -57,7 +49,7 @@ public class JmeterTest extends AbstractJavaSamplerClient { case "SYD_SM2_Sign": break; case "sleep": - Thread.sleep(1); + Thread.sleep( this.sleep ); break; default: break; diff --git a/src/test/java/cmbpoc/SignatureVerifyTest.java b/src/test/java/cmbpoc/SignatureVerifyTest.java index 8bf43cd..1f02358 100644 --- a/src/test/java/cmbpoc/SignatureVerifyTest.java +++ b/src/test/java/cmbpoc/SignatureVerifyTest.java @@ -18,7 +18,7 @@ public class SignatureVerifyTest { @Before public void setUp() { System.setProperty("com.sunyard.sydapi4j.debug", "true"); - this.api = (SydApi4j) new SydApi4j().connect("192.168.0.100", 8889, null, 1000); + this.api = (SydApi4j) new SydApi4j().connect("192.168.21.166", 8889, null, 2000); this.orgData = "Hello, World! 测试数据".getBytes(); this.SM2dn = "C=CN,ST=北京,L=北京,O=信雅达,CN=172.1.40.188"; } diff --git a/src/main/java/com/Sample/SydUtils.java b/src/test/java/sample/SydUtils.java similarity index 97% rename from src/main/java/com/Sample/SydUtils.java rename to src/test/java/sample/SydUtils.java index cfd989a..73f3680 100644 --- a/src/main/java/com/Sample/SydUtils.java +++ b/src/test/java/sample/SydUtils.java @@ -1,4 +1,4 @@ -package com.Sample; +package sample; import racal.sunyard.main.SydApi4j; diff --git a/src/main/java/com/sample/Test.java b/src/test/java/sample/Test.java similarity index 99% rename from src/main/java/com/sample/Test.java rename to src/test/java/sample/Test.java index 639d317..14ae94f 100644 --- a/src/main/java/com/sample/Test.java +++ b/src/test/java/sample/Test.java @@ -1,4 +1,4 @@ -package com.sample; +package sample; import cn.hutool.core.io.FileUtil; import cn.hutool.json.JSONObject; diff --git a/src/test/java/sample/Test4NongxinCW.java b/src/test/java/sample/Test4NongxinCW.java index 3c649d5..9b6da4b 100644 --- a/src/test/java/sample/Test4NongxinCW.java +++ b/src/test/java/sample/Test4NongxinCW.java @@ -1,10 +1,7 @@ package sample; import cn.hutool.core.date.StopWatch; -import com.Sample.SydUtils; import com.google.gson.Gson; -import com.sunyard.RetWrap; -import com.sunyard.SydApi; import com.sunyard.SydApiException; import com.sunyard.entity.ImportResult; import com.sunyard.proto.Util; diff --git a/src/main/java/com/Sample/TestDemo.java b/src/test/java/sample/TestDemo.java similarity index 99% rename from src/main/java/com/Sample/TestDemo.java rename to src/test/java/sample/TestDemo.java index 1185bf1..4b2fef1 100644 --- a/src/main/java/com/Sample/TestDemo.java +++ b/src/test/java/sample/TestDemo.java @@ -1,4 +1,4 @@ -package com.Sample; +package sample; import com.sunyard.RetWrap; import com.sunyard.proto.Util; diff --git a/src/main/java/com/jmeter/test/Test4Jemter.java b/src/test/java/test/Test4Jemter.java similarity index 99% rename from src/main/java/com/jmeter/test/Test4Jemter.java rename to src/test/java/test/Test4Jemter.java index 7e2a61c..c6bd9f7 100644 --- a/src/main/java/com/jmeter/test/Test4Jemter.java +++ b/src/test/java/test/Test4Jemter.java @@ -1,6 +1,6 @@ -package com.jmeter.test; +package test; -import com.Sample.SydUtils; +import sample.SydUtils; import com.google.gson.Gson; import com.sunyard.SydApiException; import com.sunyard.entity.ImportResult; From 9062a9c17c14cf7d9c9282b09a13de633cb46381 Mon Sep 17 00:00:00 2001 From: cheney Date: Mon, 1 Jun 2026 17:57:23 +0800 Subject: [PATCH 05/18] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/cmbpoc/FunctionTest.java | 54 +++++++------ src/test/java/cmbpoc/SignatureVerifyTest.java | 6 +- .../sunyard/sydapi/loadrunner/ShangTest.java | 50 ------------ .../sydapi/loadrunner/action1/Actions.java | 44 ----------- .../sydapi/loadrunner/raw/Actions.java | 75 ------------------ .../sydapi/loadrunner/raw_uid/Actions.java | 76 ------------------- .../Test4Jmeter.java} | 4 +- src/test/java/lrapi/lr.java | 14 ---- 8 files changed, 35 insertions(+), 288 deletions(-) delete mode 100644 src/test/java/com/sunyard/sydapi/loadrunner/ShangTest.java delete mode 100644 src/test/java/com/sunyard/sydapi/loadrunner/action1/Actions.java delete mode 100644 src/test/java/com/sunyard/sydapi/loadrunner/raw/Actions.java delete mode 100644 src/test/java/com/sunyard/sydapi/loadrunner/raw_uid/Actions.java rename src/test/java/{test/Test4Jemter.java => jmeter/Test4Jmeter.java} (99%) delete mode 100644 src/test/java/lrapi/lr.java diff --git a/src/test/java/cmbpoc/FunctionTest.java b/src/test/java/cmbpoc/FunctionTest.java index c1d6491..751ae0c 100644 --- a/src/test/java/cmbpoc/FunctionTest.java +++ b/src/test/java/cmbpoc/FunctionTest.java @@ -8,10 +8,12 @@ import racal.sunyard.main.SydApi4j; public class FunctionTest { private SydApi4j api; - private String keypairIndex; - private byte[] privateKey; - private byte[] publicKey; - private byte[] orgData; + private String keypairIndex4Sign = "0000000000000sge00000000001"; + private byte[] privateKey4Sign; + private byte[] publicKey4Sign; + private byte[] privateKey4Ende; + private byte[] publicKey4Ende; + private byte[] orgData = new byte[2 * 1024]; private String sign; @Before @@ -20,8 +22,15 @@ public class FunctionTest { System.setProperty("com.sunyard.sydapi4j.debug", "true"); // 建立链接(单台) - this.api = (SydApi4j) new SydApi4j().connect("192.168.21.166", 8889, null, 5000); + this.api = (SydApi4j) new SydApi4j().connect("172.1.41.139", 8889, null, 5000); + RetWrap keypair = this.getPrivateKeyAndPublickKey(keypairIndex4Sign); + this.publicKey4Sign = Util.hexString2Bytes( keypair.get("pk").toString() ); + this.privateKey4Sign = Util.hexString2Bytes( keypair.get("sk").toString() ); + + RetWrap keypair4Ende = this.getPrivateKeyAndPublickKey(keypairIndex4Sign); + this.publicKey4Ende = Util.hexString2Bytes( keypair4Ende.get("pk").toString() ); + this.privateKey4Ende = Util.hexString2Bytes( keypair4Ende.get("sk").toString() ); } @@ -45,41 +54,38 @@ public class FunctionTest { System.out.println("pk=" + pk); System.out.println("sk=" + sk); - this.privateKey = Util.hexString2Bytes(sk); - this.publicKey = Util.hexString2Bytes(pk); + this.privateKey4Sign = Util.hexString2Bytes(sk); + this.publicKey4Sign = Util.hexString2Bytes(pk); } + public RetWrap getPrivateKeyAndPublickKey(String keypairIndex){ + String pk = this.api.SM2GetPublicKeyC(keypairIndex); + String sk = this.api.SM2GetPrivateKeyC(keypairIndex); - @Ignore - @Test - public void getPrivateKeyAndPublickKey(){ - this.publicKey = Util.hexString2Bytes( - this.api.SM2GetPublicKeyC(keypairIndex) - ); + System.out.println("pk=" + pk); + System.out.println("sk=" + sk); - this.privateKey = Util.hexString2Bytes( - this.api.SM2GetPrivateKeyC(keypairIndex) - ); + RetWrap retWrap = new RetWrap(); + retWrap.put("pk", pk); + retWrap.put("sk", sk); + return retWrap; } @Test public void SYD_SM2_Sign(){ - this.api.SYD_SM2_Sign_HA(1, privateKey, publicKey, orgData); - this.api.SYD_SM2_Verify_HA(1, publicKey, orgData, sign); + String sign = this.api.SYD_SM2_Sign_HA(1, privateKey4Sign, publicKey4Sign, orgData); + this.api.SYD_SM2_Verify_HA(1, publicKey4Sign, orgData, sign); } @Test public void SYD_SM2_Encrypt(){ - byte[] enData = this.api.SYD_SM2Encrypt(publicKey, orgData); - byte[] deData = this.api.SYD_SM2Decrypt(privateKey, enData); + byte[] enData = this.api.SYD_SM2Encrypt(privateKey4Ende, orgData); + byte[] deData = this.api.SYD_SM2Decrypt(publicKey4Ende, enData); Assert.assertArrayEquals(enData, deData); } - - - -} +} \ No newline at end of file diff --git a/src/test/java/cmbpoc/SignatureVerifyTest.java b/src/test/java/cmbpoc/SignatureVerifyTest.java index 1f02358..000beca 100644 --- a/src/test/java/cmbpoc/SignatureVerifyTest.java +++ b/src/test/java/cmbpoc/SignatureVerifyTest.java @@ -18,9 +18,9 @@ public class SignatureVerifyTest { @Before public void setUp() { System.setProperty("com.sunyard.sydapi4j.debug", "true"); - this.api = (SydApi4j) new SydApi4j().connect("192.168.21.166", 8889, null, 2000); + this.api = (SydApi4j) new SydApi4j().connect("172.1.41.139", 8889, null, 3000); this.orgData = "Hello, World! 测试数据".getBytes(); - this.SM2dn = "C=CN,ST=北京,L=北京,O=信雅达,CN=172.1.40.188"; + this.SM2dn = "C=cn,O=CFCA OCA1 SM2,OU=Local RA OCA1,OU=Units,CN=041@N91330108762038374D@sunyard@00000064"; } @After @@ -71,7 +71,7 @@ public class SignatureVerifyTest { byte[] cipherKey = (byte[]) genResult.get(SydApi.RET_CIPHER_KEY); Assert.assertNotNull("数字信封密文密钥不应为空", cipherKey); - System.out.println("数字信封密文密钥: " + new String(cipherKey)); + System.out.println("数字信封密文: " + new String(cipherKey)); RetWrap decryptResult = this.api.decryptDEByDN(SM2dn, cipherKey); Assert.assertNotNull("数字信封解密结果不应为空", decryptResult); diff --git a/src/test/java/com/sunyard/sydapi/loadrunner/ShangTest.java b/src/test/java/com/sunyard/sydapi/loadrunner/ShangTest.java deleted file mode 100644 index bb5c4bc..0000000 --- a/src/test/java/com/sunyard/sydapi/loadrunner/ShangTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.sunyard.sydapi.loadrunner; - -import com.sunyard.sydapi.loadrunner.action1.Actions; -import racal.sunyard.main.SydApi4j; -import racal.sunyard.main.SydApiBuilder; - -public class ShangTest { - - private static final String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; - private static final byte[] testData = new byte[ 4 * 1024 ]; - - private SydApi4j api = null; - - public void init() throws Exception { - if ( null == api ) { - api = (SydApi4j) new SydApiBuilder() - .setIp("172.16.18.59") // 192.168.0.162 - .setPort( 8889 ) - .useDebugMode() - .build(); - } - } - - public void action(){ - String sign = api.attachedSign(testData, dn); - boolean ret = api.attachedVerify( sign ); - - assert ret; - - } - - public void end(){ - if (null != this.api ) { - this.api.disconnect(); - } - } - - - public static void main(String[] args) throws Throwable { - System.out.println( SydApi4j.getVersion() ); - -// Actions test = new Actions(); - ShangTest test = new ShangTest(); - test.init(); - test.action(); - test.end(); - - } - -} diff --git a/src/test/java/com/sunyard/sydapi/loadrunner/action1/Actions.java b/src/test/java/com/sunyard/sydapi/loadrunner/action1/Actions.java deleted file mode 100644 index e0e614d..0000000 --- a/src/test/java/com/sunyard/sydapi/loadrunner/action1/Actions.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.sunyard.sydapi.loadrunner.action1; - -/* - * LoadRunner Java script. (Build: _build_number_) - * - * Script Description: - * - */ -import racal.sunyard.main.SydApi4j; -import racal.sunyard.main.SydApiBuilder; - -public class Actions -{ - - private static final byte[] testData = new byte[ 4 * 1024 ]; - - private SydApi4j api = null; - - public int init() throws Throwable { - if ( null == api ) { - api = (SydApi4j) new SydApiBuilder() - .setIp("172.16.18.59") - .setPort( 8889 ) - .useDebugMode() - .build(); - } - - return 0; - }//end of init - - - public int action() throws Throwable { - api.SM3Hash( testData ); - return 0; - }//end of action - - - public int end() throws Throwable { - if (null != this.api ) { - this.api.disconnect(); - } - return 0; - }//end of end -} diff --git a/src/test/java/com/sunyard/sydapi/loadrunner/raw/Actions.java b/src/test/java/com/sunyard/sydapi/loadrunner/raw/Actions.java deleted file mode 100644 index 50f6557..0000000 --- a/src/test/java/com/sunyard/sydapi/loadrunner/raw/Actions.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.sunyard.sydapi.loadrunner.raw; - -/* - * LoadRunner Java script. (Build: _build_number_) - * - * Script Description: - * - */ - -import lrapi.lr; -import racal.sunyard.main.SydApi4j; -import racal.sunyard.main.SydApiBuilder; -import com.sunyard.proto.Util; -import com.sunyard.SydApi; - -public class Actions -{ - - private static final String inHexData = "67c6697351ff4aec29cdbaabf2fbe346"; - - private static String pk ; - private static String sk ; - private SydApi api; - - - - public int init() throws Throwable { - SydApiBuilder builder = new SydApiBuilder().setIp("192.168.1.123"); - api = builder.build(); - - this.pk = api.SM2GetPublicKeyC("123", 0 ); - this.sk = api.SM2GetPrivateKeyC("123"); - - return 0; - }//end of init - - - public int action() throws Throwable { - lr.start_transaction("auto");//设置事务开始点 - - try { - - System.out.println("begin SYD_Sign_P1"); - byte[] sign = this.api.SM2SignC( 0 , sk, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData) ); - System.out.println("sign=" + Util.bytes2HexString( sign )); - - System.out.println("begin SYD_Verify_P1"); - boolean pass = api.SM2Verify(1, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData), sign ); - System.out.println("验签结果 " + pass); - System.out.println("end SYD_Verify_P1"); - - } finally { - lr.end_transaction("auto", lr.AUTO);//设置事务结束点 - } - - return 0; - }//end of action - - - public int end() throws Throwable { - return 0; - }//end of end - - - public static void main(String[] args) throws Throwable { - - Actions actions = new Actions(); - actions.init(); - actions.action(); - actions.end(); - } - -} - - diff --git a/src/test/java/com/sunyard/sydapi/loadrunner/raw_uid/Actions.java b/src/test/java/com/sunyard/sydapi/loadrunner/raw_uid/Actions.java deleted file mode 100644 index 89ffd23..0000000 --- a/src/test/java/com/sunyard/sydapi/loadrunner/raw_uid/Actions.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.sunyard.sydapi.loadrunner.raw_uid; - -/* - * LoadRunner Java script. (Build: _build_number_) - * - * Script Description: - * - */ - -import lrapi.lr; -import racal.sunyard.main.SydApi4j; -import racal.sunyard.main.SydApiBuilder; -import com.sunyard.proto.Util; -import com.sunyard.SydApi; - -public class Actions -{ - - private static final String inHexData = "67c6697351ff4aec29cdbaabf2fbe346"; - private static final String uid = "8765432187654321"; - - private static String pk ; - private static String sk ; - private SydApi api; - - - - public int init() throws Throwable { - SydApiBuilder builder = new SydApiBuilder().setIp("192.168.1.123"); - api = builder.build(); - - this.pk = api.SM2GetPublicKeyC("123", 0 ); - this.sk = api.SM2GetPrivateKeyC("123"); - - return 0; - }//end of init - - - public int action() throws Throwable { - lr.start_transaction("auto");//设置事务开始点 - - try { - - System.out.println("begin SYD_Sign_P1"); - byte[] sign = this.api.SM2SignC( 0, uid , sk, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData) ); - System.out.println("sign=" + Util.bytes2HexString( sign )); - - System.out.println("begin SYD_Verify_P1"); - boolean pass = api.SM2Verify( uid, 1, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData), sign ); - System.out.println("验签结果 " + pass); - System.out.println("end SYD_Verify_P1"); - - } finally { - lr.end_transaction("auto", lr.AUTO);//设置事务结束点 - } - - return 0; - }//end of action - - - public int end() throws Throwable { - return 0; - }//end of end - - - public static void main(String[] args) throws Throwable { - - Actions actions = new Actions(); - actions.init(); - actions.action(); - actions.end(); - } - -} - - diff --git a/src/test/java/test/Test4Jemter.java b/src/test/java/jmeter/Test4Jmeter.java similarity index 99% rename from src/test/java/test/Test4Jemter.java rename to src/test/java/jmeter/Test4Jmeter.java index c6bd9f7..b86a422 100644 --- a/src/test/java/test/Test4Jemter.java +++ b/src/test/java/jmeter/Test4Jmeter.java @@ -1,4 +1,4 @@ -package test; +package jmeter; import sample.SydUtils; import com.google.gson.Gson; @@ -17,7 +17,7 @@ import java.nio.charset.StandardCharsets; import java.util.*; -public class Test4Jemter extends AbstractJavaSamplerClient { +public class Test4Jmeter extends AbstractJavaSamplerClient { private String SM2dn ; private String RSAdn ; diff --git a/src/test/java/lrapi/lr.java b/src/test/java/lrapi/lr.java deleted file mode 100644 index dab1220..0000000 --- a/src/test/java/lrapi/lr.java +++ /dev/null @@ -1,14 +0,0 @@ -package lrapi; - -public class lr { - - public static String AUTO = "AUTO"; - - public static void start_transaction(String t) { - System.out.println("start_transaction" + t); - } - - public static void end_transaction(String t, String type) { - System.out.println("end_transaction" + t); - } -} From 83802b6ef66f564e584c76a0c30c2cfe2d516983 Mon Sep 17 00:00:00 2001 From: cheney Date: Tue, 2 Jun 2026 15:06:29 +0800 Subject: [PATCH 06/18] =?UTF-8?q?=E5=BC=BA=E5=88=B6=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E5=A4=A7=E7=AB=AF=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/racal/sunyard/main/SydApi4j.java | 23 ++++++++++--------- src/test/java/cmbpoc/FunctionTest.java | 6 +++-- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index 96f6997..ecd48a8 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -51,6 +51,7 @@ import java.net.Socket; import java.net.SocketAddress; import java.net.SocketTimeoutException; import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.nio.charset.Charset; import java.security.NoSuchProviderException; import java.security.Security; @@ -6901,18 +6902,18 @@ public class SydApi4j implements SydApi { bb.put("7F".getBytes()); bb.put((byte) dataType); // 数据包标志 if ( 2 == dataType ) { // 公钥 - bb.put(ByteUtil.shortToBytes((short) publicKey.length)); + bb.put(ByteUtil.shortToBytes((short) publicKey.length, ByteOrder.BIG_ENDIAN) ); bb.put(publicKey); } if ( 1 == dataType ) { // 数据 - bb.put(ByteUtil.shortToBytes((short) pOrgData.limit())); + bb.put(ByteUtil.shortToBytes((short) pOrgData.limit(), ByteOrder.BIG_ENDIAN)); bb.put(pOrgData); bb.put(ByteUtil.intToBytes(idx)); } if ( 3 == dataType) { // 仅数据长度 - bb.put( ByteUtil.shortToBytes((short) (63*1024)) ); - bb.put( ByteUtil.intToBytes(idx)); + bb.put( ByteUtil.shortToBytes((short) (63*1024), ByteOrder.BIG_ENDIAN) ); + bb.put( ByteUtil.intToBytes(idx, ByteOrder.BIG_ENDIAN)); } @@ -6943,7 +6944,7 @@ public class SydApi4j implements SydApi { int isLast = bb.get(); byte[] dataLenArr = new byte[2]; bb.get(dataLenArr); - int dataLen = ByteUtil.bytesToInt(dataLenArr); + int dataLen = ByteUtil.bytesToInt(dataLenArr, ByteOrder.BIG_ENDIAN); // byte[] data = new byte[dataLen]; // bb.get(data); ByteBuffer slice = ByteBufferUtil.sliceAndConsume(bb, dataLen); @@ -7036,17 +7037,17 @@ public class SydApi4j implements SydApi { bb.put((byte) dataType); if (dataType == 2) { // 私钥 - bb.put(ByteUtil.shortToBytes((short) privateKey.length)); + bb.put(ByteUtil.shortToBytes((short) privateKey.length, ByteOrder.BIG_ENDIAN)); bb.put(privateKey); } if (dataType == 1) { // 密文块 - bb.put(ByteUtil.shortToBytes((short) pCipherData.limit())); + bb.put(ByteUtil.shortToBytes((short) pCipherData.limit(), ByteOrder.BIG_ENDIAN)); bb.put(pCipherData); - bb.put(ByteUtil.intToBytes(idx)); + bb.put(ByteUtil.intToBytes(idx, ByteOrder.BIG_ENDIAN)); } if (dataType == 3) { // 请求明文块 - bb.put(ByteUtil.shortToBytes((short) (63 * 1024))); - bb.put(ByteUtil.intToBytes(idx)); + bb.put(ByteUtil.shortToBytes((short) (63 * 1024), ByteOrder.BIG_ENDIAN)); + bb.put(ByteUtil.intToBytes(idx, ByteOrder.BIG_ENDIAN)); } // 填写总长度(长度字段本身不包含在内) @@ -7073,7 +7074,7 @@ public class SydApi4j implements SydApi { int isLast = bb.get(); byte[] dataLenArr = new byte[2]; bb.get(dataLenArr); - int dataLen = ByteUtil.bytesToInt(dataLenArr); + int dataLen = ByteUtil.bytesToInt(dataLenArr, ByteOrder.BIG_ENDIAN); ByteBuffer slice = ByteBufferUtil.sliceAndConsume(bb, dataLen); return new MutiReturn7(retCode, isLast, slice); } else { diff --git a/src/test/java/cmbpoc/FunctionTest.java b/src/test/java/cmbpoc/FunctionTest.java index 751ae0c..be21da2 100644 --- a/src/test/java/cmbpoc/FunctionTest.java +++ b/src/test/java/cmbpoc/FunctionTest.java @@ -9,6 +9,7 @@ public class FunctionTest { private SydApi4j api; private String keypairIndex4Sign = "0000000000000sge00000000001"; + private String keypairIndex4Ende = "0000000000000sge00100000001"; private byte[] privateKey4Sign; private byte[] publicKey4Sign; private byte[] privateKey4Ende; @@ -19,6 +20,7 @@ public class FunctionTest { @Before public void start(){ // 调试模式 + System.setProperty("com.sunyard.sydapi4j.debug", "true"); // 建立链接(单台) @@ -85,7 +87,7 @@ public class FunctionTest { @Test public void SYD_SM2_Encrypt(){ byte[] enData = this.api.SYD_SM2Encrypt(privateKey4Ende, orgData); - byte[] deData = this.api.SYD_SM2Decrypt(publicKey4Ende, enData); - Assert.assertArrayEquals(enData, deData); +// byte[] deData = this.api.SYD_SM2Decrypt(publicKey4Ende, enData); +// Assert.assertArrayEquals(enData, deData); } } \ No newline at end of file From b8322538e7499418381a01ef6dbdbbee8564b76f Mon Sep 17 00:00:00 2001 From: cheney Date: Tue, 2 Jun 2026 16:26:54 +0800 Subject: [PATCH 07/18] =?UTF-8?q?QN=20=E6=8C=87=E4=BB=A4=E9=95=BF=E5=BA=A6?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DProto/QN.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DProto/QN.xml b/DProto/QN.xml index 3a7211c..8ddb9d1 100644 --- a/DProto/QN.xml +++ b/DProto/QN.xml @@ -50,8 +50,8 @@ - - + + From 10d9d1b0d6d9a4b59665260e583cdb8d9a2e4489 Mon Sep 17 00:00:00 2001 From: cheney Date: Tue, 2 Jun 2026 17:58:02 +0800 Subject: [PATCH 08/18] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=80=9A=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/sunyard/SydApiException.java | 3 + .../java/racal/sunyard/main/SydApi4j.java | 78 ++++++++++++------- src/test/java/cmbpoc/FunctionTest.java | 50 +++++++----- 3 files changed, 85 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/sunyard/SydApiException.java b/src/main/java/com/sunyard/SydApiException.java index a8b103f..51cd224 100644 --- a/src/main/java/com/sunyard/SydApiException.java +++ b/src/main/java/com/sunyard/SydApiException.java @@ -45,6 +45,9 @@ public class SydApiException extends RuntimeException{ case 4: msg = "DER 编码格式错误(公钥) "; break; + case 0xd: + msg = "公钥加密错误"; + break; case 21: msg = "输入的数据格式错误 "; break; diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index ecd48a8..5cd7f72 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -2587,49 +2587,69 @@ public class SydApi4j implements SydApi { private ByteBuffer read(Socket socket) throws IOException { InputStream is = socket.getInputStream(); - int d; - int high = is.read(); - int low = is.read(); - int len = (high << 8) + low; + byte[] buf = new byte[2]; + int offset = 0; + int remaining = 2; + while (remaining > 0) { + int read = is.read(buf, offset, remaining); + if (read == -1) { + throw new EOFException("Expected 2 bytes, but reached end of stream after reading " + offset + " byte(s)"); + } + offset += read; + remaining -= read; + } + int len = ((buf[0] & 0xFF) << 8) | (buf[1] & 0xFF); len += 2; if(debug){ - print("H\t"); - println(Integer.toHexString(high)); - print("L\t"); - println(Integer.toHexString(low)); - print("Len\t"); + print("接收数据\t"); +// print("H="); +// print(Integer.toHexString(high)); +// print("\tL="); +// print(Integer.toHexString(low)); + print("\tLen="); println(len); } ByteBuffer bb = ByteBuffer.allocate(len); - bb.put((byte) (high & 0xFF)); + bb.put(buf); +// bb.put((byte) (high & 0xFF)); //println(String.format("%d %d %s",len, bb.position() -1, Util.bytes2HexString(new byte[]{(byte) (high & 0xFF)}))); - bb.put((byte) (low & 0xFF)); +// bb.put((byte) (low & 0xFF)); //println(String.format("%d %d %s",len, bb.position() -1, Util.bytes2HexString(new byte[]{(byte) (low & 0xFF)}))); +// int d = 0; +// while (true) { +// d = is.read(); +// if (-1 == d) { +// break; +// } +// +// bb.put((byte) (d & 0xFF)); +// +// //println(String.format("%d %d %s",len, bb.position() -1, Util.bytes2HexString(new byte[]{(byte) (d & 0xFF)}))); +// +// if (bb.position() == len) { +// break; +// } +// } + remaining = len - bb.position(); // 还需要读取的字节数 + byte[] buffer = new byte[8192]; // 批量读取缓冲区 - while (true) { - d = is.read(); - if (-1 == d) { - break; - } - - bb.put((byte) (d & 0xFF)); - - //println(String.format("%d %d %s",len, bb.position() -1, Util.bytes2HexString(new byte[]{(byte) (d & 0xFF)}))); - - if (bb.position() == len) { - break; + while (remaining > 0) { + int toRead = Math.min(remaining, buffer.length); + int bytesRead = is.read(buffer, 0, toRead); + if (bytesRead == -1) { + // 与原逻辑不同:这里抛异常,而不是静默退出 + throw new EOFException("Unexpected end of stream; expected " + remaining + " more bytes"); } + bb.put(buffer, 0, bytesRead); + remaining -= bytesRead; } - - - if (null != builder && builder.isUseEBCDMode()) { // print("EBCD 数据: \t"); // println(Util.bytes2HexString(bb.array())); @@ -2663,8 +2683,10 @@ public class SydApi4j implements SydApi { ByteBuffer bb = read(socket); return bb; - } catch (SocketTimeoutException e) { - throw new SydApiException("接收数据超时", -2); + } catch (EOFException e) { + throw new SydApiException("接收数据不完整", -21, e); + }catch (SocketTimeoutException e) { + throw new SydApiException("接收数据超时", -2, e); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/test/java/cmbpoc/FunctionTest.java b/src/test/java/cmbpoc/FunctionTest.java index be21da2..4721d34 100644 --- a/src/test/java/cmbpoc/FunctionTest.java +++ b/src/test/java/cmbpoc/FunctionTest.java @@ -1,6 +1,8 @@ package cmbpoc; import com.sunyard.RetWrap; +import com.sunyard.SydApi; +import com.sunyard.proto.PacketSection; import com.sunyard.proto.Util; import org.junit.*; import racal.sunyard.main.SydApi4j; @@ -14,7 +16,7 @@ public class FunctionTest { private byte[] publicKey4Sign; private byte[] privateKey4Ende; private byte[] publicKey4Ende; - private byte[] orgData = new byte[2 * 1024]; + private byte[] orgData = new byte[15]; private String sign; @Before @@ -24,15 +26,19 @@ public class FunctionTest { System.setProperty("com.sunyard.sydapi4j.debug", "true"); // 建立链接(单台) - this.api = (SydApi4j) new SydApi4j().connect("172.1.41.139", 8889, null, 5000); + this.api = (SydApi4j) new SydApi4j().connect("192.168.100.145", 8889, null, 5000); - RetWrap keypair = this.getPrivateKeyAndPublickKey(keypairIndex4Sign); - this.publicKey4Sign = Util.hexString2Bytes( keypair.get("pk").toString() ); - this.privateKey4Sign = Util.hexString2Bytes( keypair.get("sk").toString() ); +// RetWrap keypair = this.getPrivateKeyAndPublickKey(keypairIndex4Sign); +// this.publicKey4Sign = Util.hexString2Bytes( keypair.get("pk").toString() ); +// this.privateKey4Sign = Util.hexString2Bytes( keypair.get("sk").toString() ); +// +// RetWrap keypair4Ende = this.getPrivateKeyAndPublickKey(keypairIndex4Sign); +// this.publicKey4Ende = Util.hexString2Bytes( keypair4Ende.get("pk").toString() ); +// this.privateKey4Ende = Util.hexString2Bytes( keypair4Ende.get("sk").toString() ); - RetWrap keypair4Ende = this.getPrivateKeyAndPublickKey(keypairIndex4Sign); - this.publicKey4Ende = Util.hexString2Bytes( keypair4Ende.get("pk").toString() ); - this.privateKey4Ende = Util.hexString2Bytes( keypair4Ende.get("sk").toString() ); + + this.publicKey4Ende = Util.hexString2Bytes("03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7"); + this.privateKey4Ende = Util.hexString2Bytes("00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000"); } @@ -49,15 +55,21 @@ public class FunctionTest { @Ignore @Test public void genKeyPair(){ - RetWrap r = this.api.SYD_SM2GenKeyPair(); - String pk = r.get("publicKey").toString(); - String sk = r.get("privateKey").toString(); +// RetWrap r = this.api.SYD_SM2GenKeyPair(); +// String pk = r.get("publicKey").toString(); +// String sk = r.get("privateKey").toString(); +// +// System.out.println("pk=" + pk); +// System.out.println("sk=" + sk); +// +// this.privateKey4Sign = Util.hexString2Bytes(sk); +// this.publicKey4Sign = Util.hexString2Bytes(pk); - System.out.println("pk=" + pk); - System.out.println("sk=" + sk); - - this.privateKey4Sign = Util.hexString2Bytes(sk); - this.publicKey4Sign = Util.hexString2Bytes(pk); + RetWrap rw = this.api.SM2GenKeyPair(); + byte[] pk = (byte[]) rw.get(SydApi.RET_PUBLIC_KEY); + byte[] sk = (byte[]) rw.get(SydApi.RET_PRIVATE_KEY); + System.out.println("pk=" + Util.bytes2HexString(pk)); + System.out.println("sk=" + Util.bytes2HexString(sk)); } @@ -86,8 +98,10 @@ public class FunctionTest { @Test public void SYD_SM2_Encrypt(){ - byte[] enData = this.api.SYD_SM2Encrypt(privateKey4Ende, orgData); + byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData); // byte[] deData = this.api.SYD_SM2Decrypt(publicKey4Ende, enData); // Assert.assertArrayEquals(enData, deData); } -} \ No newline at end of file +} + + From 02fd07def2936b233b306e9f28719dce5a07a035 Mon Sep 17 00:00:00 2001 From: cheney Date: Wed, 3 Jun 2026 11:44:40 +0800 Subject: [PATCH 09/18] =?UTF-8?q?byte=20=E8=BD=AC=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racal/sunyard/main/SydApi4j.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index 5cd7f72..f4c47f9 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -6966,7 +6966,7 @@ public class SydApi4j implements SydApi { int isLast = bb.get(); byte[] dataLenArr = new byte[2]; bb.get(dataLenArr); - int dataLen = ByteUtil.bytesToInt(dataLenArr, ByteOrder.BIG_ENDIAN); + int dataLen = ByteUtil.bytesToShort(dataLenArr, ByteOrder.BIG_ENDIAN); // byte[] data = new byte[dataLen]; // bb.get(data); ByteBuffer slice = ByteBufferUtil.sliceAndConsume(bb, dataLen); @@ -7096,7 +7096,7 @@ public class SydApi4j implements SydApi { int isLast = bb.get(); byte[] dataLenArr = new byte[2]; bb.get(dataLenArr); - int dataLen = ByteUtil.bytesToInt(dataLenArr, ByteOrder.BIG_ENDIAN); + int dataLen = ByteUtil.bytesToShort(dataLenArr, ByteOrder.BIG_ENDIAN); ByteBuffer slice = ByteBufferUtil.sliceAndConsume(bb, dataLen); return new MutiReturn7(retCode, isLast, slice); } else { From 0dc2d7c026a74329dda86b276d73b5b3aa15deb2 Mon Sep 17 00:00:00 2001 From: cheney Date: Wed, 3 Jun 2026 14:08:19 +0800 Subject: [PATCH 10/18] =?UTF-8?q?=E5=B0=8F=E6=95=B0=E6=8D=AE=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/racal/sunyard/main/SydApi4j.java | 1 + src/test/java/cmbpoc/FunctionTest.java | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index f4c47f9..d2d9dad 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -7059,6 +7059,7 @@ public class SydApi4j implements SydApi { bb.put((byte) dataType); if (dataType == 2) { // 私钥 + bb.put("999999999999999999999999999".getBytes()); bb.put(ByteUtil.shortToBytes((short) privateKey.length, ByteOrder.BIG_ENDIAN)); bb.put(privateKey); } diff --git a/src/test/java/cmbpoc/FunctionTest.java b/src/test/java/cmbpoc/FunctionTest.java index 4721d34..703be55 100644 --- a/src/test/java/cmbpoc/FunctionTest.java +++ b/src/test/java/cmbpoc/FunctionTest.java @@ -99,8 +99,9 @@ public class FunctionTest { @Test public void SYD_SM2_Encrypt(){ byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData); -// byte[] deData = this.api.SYD_SM2Decrypt(publicKey4Ende, enData); -// Assert.assertArrayEquals(enData, deData); + System.out.println("密文=" + Util.bytes2HexString(enData)); + byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); + Assert.assertArrayEquals(orgData, deData); } } From 983dc8a47d454b2c5b63919d7a58a4e14fc976f3 Mon Sep 17 00:00:00 2001 From: cheney Date: Wed, 3 Jun 2026 16:34:41 +0800 Subject: [PATCH 11/18] =?UTF-8?q?=E5=B0=8F=E4=BA=8E=204M=20=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/sunyard/SydApiException.java | 3 + src/main/java/com/sunyard/util/BytesUtil.java | 174 ++++++++++++++++++ .../java/racal/sunyard/main/SydApi4j.java | 113 +++++++++--- src/test/java/cmbpoc/FunctionTest.java | 34 +++- 4 files changed, 298 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/sunyard/util/BytesUtil.java diff --git a/src/main/java/com/sunyard/SydApiException.java b/src/main/java/com/sunyard/SydApiException.java index 51cd224..1f95e2b 100644 --- a/src/main/java/com/sunyard/SydApiException.java +++ b/src/main/java/com/sunyard/SydApiException.java @@ -45,6 +45,9 @@ public class SydApiException extends RuntimeException{ case 4: msg = "DER 编码格式错误(公钥) "; break; + case 31: + msg = "缓冲区索引错误"; + break; case 0xd: msg = "公钥加密错误"; break; diff --git a/src/main/java/com/sunyard/util/BytesUtil.java b/src/main/java/com/sunyard/util/BytesUtil.java new file mode 100644 index 0000000..06233b7 --- /dev/null +++ b/src/main/java/com/sunyard/util/BytesUtil.java @@ -0,0 +1,174 @@ +package com.sunyard.util; + +import java.nio.ByteOrder; + +/** + * 字节数组转换工具类,提供常见的基本类型与字节数组之间的互转功能。 + * 默认采用大端字节序(高位在前),也支持通过参数指定字节序。 + * + * @author YourName + */ +public final class BytesUtil { + + private BytesUtil() { + // 私有构造方法,防止实例化 + } + + // ==================== 无符号 short 转字节数组 ==================== + + /** + * 将无符号 short 值(以 int 形式传入,范围 0 ~ 65535)转换为大端字节序的字节数组(2字节)。 + * + * @param value 无符号 short 值(0 到 65535) + * @return 长度为 2 的字节数组,高位在前 + * @throws IllegalArgumentException 如果 value 超出 0~65535 范围 + */ + public static byte[] unsignedShortToBytes(int value) { + if (value < 0 || value > 0xFFFF) { + throw new IllegalArgumentException("Value out of range for unsigned short: " + value); + } + return new byte[]{ + (byte) ((value >> 8) & 0xFF), + (byte) (value & 0xFF) + }; + } + + /** + * 将无符号 short 值(以 short 类型传入,自动按无符号处理)转换为大端字节序的字节数组(2字节)。 + * 例如,传入 (short) 0x8001 会得到 { (byte)0x80, (byte)0x01 }。 + * + * @param value short 类型,将按其二进制补码表示直接作为无符号值处理 + * @return 长度为 2 的字节数组,高位在前 + */ + public static byte[] unsignedShortToBytes(short value) { + // 将 short 转为无符号 int + int unsignedValue = value & 0xFFFF; + return unsignedShortToBytes(unsignedValue); + } + + /** + * 将无符号 short 值(int 类型)转换为指定字节序的字节数组(2字节)。 + * + * @param value 无符号 short 值(0 到 65535) + * @param byteOrder 字节序,ByteOrder.BIG_ENDIAN 或 ByteOrder.LITTLE_ENDIAN + * @return 长度为 2 的字节数组 + * @throws IllegalArgumentException 如果 value 超出范围或 byteOrder 为 null + */ + public static byte[] unsignedShortToBytes(int value, ByteOrder byteOrder) { + if (byteOrder == null) { + throw new IllegalArgumentException("ByteOrder must not be null"); + } + byte[] bytes = unsignedShortToBytes(value); + if (byteOrder == ByteOrder.LITTLE_ENDIAN) { + // 交换高低位 + byte tmp = bytes[0]; + bytes[0] = bytes[1]; + bytes[1] = tmp; + } + return bytes; + } + + /** + * 将无符号 short 值(short 类型)转换为指定字节序的字节数组(2字节)。 + * + * @param value short 类型,按无符号处理 + * @param byteOrder 字节序 + * @return 长度为 2 的字节数组 + */ + public static byte[] unsignedShortToBytes(short value, ByteOrder byteOrder) { + return unsignedShortToBytes(value & 0xFFFF, byteOrder); + } + + // ==================== 字节数组转无符号 short ==================== + + /** + * 从字节数组的起始位置读取 2 字节,按大端序转换为无符号 short 值(返回 int 类型)。 + * + * @param bytes 字节数组 + * @param offset 起始偏移量 + * @return 无符号 short 值(0 ~ 65535) + * @throws IllegalArgumentException 如果 bytes 为 null 或长度不足 + */ + public static int bytesToUnsignedShort(byte[] bytes, int offset) { + if (bytes == null || offset + 2 > bytes.length) { + throw new IllegalArgumentException("Invalid byte array or offset"); + } + return ((bytes[offset] & 0xFF) << 8) | (bytes[offset + 1] & 0xFF); + } + + /** + * 从字节数组的起始位置读取 2 字节,按指定字节序转换为无符号 short 值。 + * + * @param bytes 字节数组 + * @param offset 起始偏移量 + * @param byteOrder 字节序 + * @return 无符号 short 值 + */ + public static int bytesToUnsignedShort(byte[] bytes, int offset, ByteOrder byteOrder) { + if (byteOrder == ByteOrder.LITTLE_ENDIAN) { + return ((bytes[offset + 1] & 0xFF) << 8) | (bytes[offset] & 0xFF); + } else { + return bytesToUnsignedShort(bytes, offset); + } + } + + // ==================== 其他常见转换(扩展) ==================== + + /** + * 将 int 值转换为大端序的 4 字节数组。 + */ + public static byte[] intToBytes(int value) { + return new byte[]{ + (byte) ((value >> 24) & 0xFF), + (byte) ((value >> 16) & 0xFF), + (byte) ((value >> 8) & 0xFF), + (byte) (value & 0xFF) + }; + } + + /** + * 将大端序的 4 字节数组转换为 int 值。 + */ + public static int bytesToInt(byte[] bytes, int offset) { + if (bytes == null || offset + 4 > bytes.length) { + throw new IllegalArgumentException("Invalid byte array or offset"); + } + return ((bytes[offset] & 0xFF) << 24) + | ((bytes[offset + 1] & 0xFF) << 16) + | ((bytes[offset + 2] & 0xFF) << 8) + | (bytes[offset + 3] & 0xFF); + } + + /** + * 将 long 值转换为大端序的 8 字节数组。 + */ + public static byte[] longToBytes(long value) { + return new byte[]{ + (byte) ((value >> 56) & 0xFF), + (byte) ((value >> 48) & 0xFF), + (byte) ((value >> 40) & 0xFF), + (byte) ((value >> 32) & 0xFF), + (byte) ((value >> 24) & 0xFF), + (byte) ((value >> 16) & 0xFF), + (byte) ((value >> 8) & 0xFF), + (byte) (value & 0xFF) + }; + } + + /** + * 将大端序的 8 字节数组转换为 long 值。 + */ + public static long bytesToLong(byte[] bytes, int offset) { + if (bytes == null || offset + 8 > bytes.length) { + throw new IllegalArgumentException("Invalid byte array or offset"); + } + return ((long) (bytes[offset] & 0xFF) << 56) + | ((long) (bytes[offset + 1] & 0xFF) << 48) + | ((long) (bytes[offset + 2] & 0xFF) << 40) + | ((long) (bytes[offset + 3] & 0xFF) << 32) + | ((long) (bytes[offset + 4] & 0xFF) << 24) + | ((long) (bytes[offset + 5] & 0xFF) << 16) + | ((long) (bytes[offset + 6] & 0xFF) << 8) + | ((long) (bytes[offset + 7] & 0xFF)); + } +} diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index d2d9dad..6ba343f 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -46,10 +46,7 @@ import racal.sunyard.main.proto.*; import javax.security.auth.x500.X500Principal; import java.io.*; -import java.net.InetSocketAddress; -import java.net.Socket; -import java.net.SocketAddress; -import java.net.SocketTimeoutException; +import java.net.*; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.Charset; @@ -6857,19 +6854,19 @@ public class SydApi4j implements SydApi { ParamChecker.checkNotNull(publicKey, "publicKey"); ParamChecker.checkNotNull(pOrgData, "pOrgData"); - final int CHUNK_SIZE = 63 * 1024; // 64 KB + final int CHUNK_SIZE = 62 * 1024; // 62 KB int dataLen = pOrgData.length; int offset = 0; while (offset < dataLen) { int len = Math.min(CHUNK_SIZE, dataLen - offset); // 调用底层支持偏移量的加密方法 - SYD_SM2Encrypt(1, publicKey, ByteBuffer.wrap(pOrgData, offset, len), offset); + SYD_SM2Encrypt(1, publicKey, ByteBuffer.wrap(pOrgData, offset, len), offset, len, dataLen); offset += len; } // 计算 - SYD_SM2Encrypt(2, publicKey, null, 0); + SYD_SM2Encrypt(2, publicKey, null, 0, 0, dataLen); // 取回 int idx = 0; @@ -6878,7 +6875,7 @@ public class SydApi4j implements SydApi { int isLast = 0; while (isLast != 1) { - MutiReturn7 ret = SYD_SM2Encrypt(3, null, null, idx); + MutiReturn7 ret = SYD_SM2Encrypt(3, null, null, idx, 0, 0); ByteBuffer bb = ret.getData(); isLast = ret.getIsLast(); @@ -6905,7 +6902,7 @@ public class SydApi4j implements SydApi { // @Override // 支持大包加解密 - private MutiReturn7 SYD_SM2Encrypt(int dataType, byte[] publicKey, ByteBuffer pOrgData, int idx) { + private MutiReturn7 SYD_SM2Encrypt(int dataType, byte[] publicKey, ByteBuffer pOrgData, int idx, int inLen, int totalLen) { // 参数检查 ParamChecker.checkInRange(dataType, "dataType", 1, 3); @@ -6928,13 +6925,13 @@ public class SydApi4j implements SydApi { bb.put(publicKey); } if ( 1 == dataType ) { // 数据 - bb.put(ByteUtil.shortToBytes((short) pOrgData.limit(), ByteOrder.BIG_ENDIAN)); + bb.put(com.sunyard.util.BytesUtil.unsignedShortToBytes(inLen, ByteOrder.BIG_ENDIAN)); bb.put(pOrgData); - bb.put(ByteUtil.intToBytes(idx)); + bb.put(ByteUtil.intToBytes(idx, ByteOrder.BIG_ENDIAN)); } if ( 3 == dataType) { // 仅数据长度 - bb.put( ByteUtil.shortToBytes((short) (63*1024), ByteOrder.BIG_ENDIAN) ); + bb.put( ByteUtil.shortToBytes((short) (62*1024), ByteOrder.BIG_ENDIAN) ); bb.put( ByteUtil.intToBytes(idx, ByteOrder.BIG_ENDIAN)); } @@ -6947,8 +6944,41 @@ public class SydApi4j implements SydApi { // 通信 synchronized (this) { - // 响应解析 - bb = syncRead(syncSend(bb)); + int originalTimeout = -1; + boolean timeoutModified = false; + + try { + // 仅当 dataType == 2 时才执行超时时间调整逻辑 + if (false && dataType == 2) { + // 获取当前 Socket 超时时间(毫秒),可能为 -1(无限等待)或 0(立即超时) + originalTimeout = this.socket.getSoTimeout(); + + // 计算 totalLen 是 1MB 的多少倍(整数倍,如 2M→2,3M→3,4M→4) + int rate = totalLen / (1024 * 1024); + + // 仅当倍数 >1 且原超时时间为正数时,才按比例放大超时时间 + if (rate > 1 && originalTimeout > 0) { + int newTimeout = originalTimeout * rate; + this.socket.setSoTimeout(newTimeout); + timeoutModified = true; // 标记已修改,以便 finally 中恢复 + } + } + + // 响应解析(无论是否调整超时,都正常执行) + bb = syncRead(syncSend(bb)); + + } catch (SocketException e) { + throw new SydApiException("socket 状态错误", -5, e); + } finally { + // 仅当超时时间被实际修改过时才恢复原值 + if (timeoutModified) { + try { + this.socket.setSoTimeout(originalTimeout); + } catch (SocketException e) { + // 恢复失败时可根据需要记录日志,此处忽略 + } + } + } } bb.flip(); @@ -6966,9 +6996,10 @@ public class SydApi4j implements SydApi { int isLast = bb.get(); byte[] dataLenArr = new byte[2]; bb.get(dataLenArr); - int dataLen = ByteUtil.bytesToShort(dataLenArr, ByteOrder.BIG_ENDIAN); + int dataLen = Short.toUnsignedInt(ByteUtil.bytesToShort(dataLenArr, ByteOrder.BIG_ENDIAN)); // byte[] data = new byte[dataLen]; // bb.get(data); + // 从返回数据中创建密文切片,此时不拷贝数据。 ByteBuffer slice = ByteBufferUtil.sliceAndConsume(bb, dataLen); return new MutiReturn7(retCode, isLast, slice); } else { @@ -6989,19 +7020,19 @@ public class SydApi4j implements SydApi { ParamChecker.checkNotNull(privateKey, "privateKey"); ParamChecker.checkNotNull(pCipherData, "pCipherData"); - final int CHUNK_SIZE = 63 * 1024; // 63 KB,与加密保持一致 + final int CHUNK_SIZE = 62 * 1024; // 62 KB,与加密保持一致 int dataLen = pCipherData.length; int offset = 0; // 1. 分块传入密文 while (offset < dataLen) { int len = Math.min(CHUNK_SIZE, dataLen - offset); - SYD_SM2Decrypt(1, privateKey, ByteBuffer.wrap(pCipherData, offset, len), offset); + SYD_SM2Decrypt(1, privateKey, ByteBuffer.wrap(pCipherData, offset, len), offset, len, 0); offset += len; } // 2. 传入私钥,触发解密处理 - SYD_SM2Decrypt(2, privateKey, null, 0); + SYD_SM2Decrypt(2, privateKey, null, 0, 0, dataLen); // 3. 分块取回明文 int idx = 0; @@ -7010,7 +7041,7 @@ public class SydApi4j implements SydApi { int isLast = 0; while (isLast != 1) { - MutiReturn7 ret = SYD_SM2Decrypt(3, null, null, idx); + MutiReturn7 ret = SYD_SM2Decrypt(3, null, null, idx, 0, 0); ByteBuffer bb = ret.getData(); isLast = ret.getIsLast(); @@ -7042,7 +7073,7 @@ public class SydApi4j implements SydApi { * @param idx 偏移量索引 * @return MutiReturn7 包含返回码、isLast标志及数据片 */ - private MutiReturn7 SYD_SM2Decrypt(int dataType, byte[] privateKey, ByteBuffer pCipherData, int idx) { + private MutiReturn7 SYD_SM2Decrypt(int dataType, byte[] privateKey, ByteBuffer pCipherData, int idx, int inLen, int totalLen) { ParamChecker.checkInRange(dataType, "dataType", 1, 3); // 估算缓存大小 @@ -7064,12 +7095,12 @@ public class SydApi4j implements SydApi { bb.put(privateKey); } if (dataType == 1) { // 密文块 - bb.put(ByteUtil.shortToBytes((short) pCipherData.limit(), ByteOrder.BIG_ENDIAN)); + bb.put( com.sunyard.util.BytesUtil.unsignedShortToBytes(inLen, ByteOrder.BIG_ENDIAN) ); bb.put(pCipherData); bb.put(ByteUtil.intToBytes(idx, ByteOrder.BIG_ENDIAN)); } if (dataType == 3) { // 请求明文块 - bb.put(ByteUtil.shortToBytes((short) (63 * 1024), ByteOrder.BIG_ENDIAN)); + bb.put(ByteUtil.shortToBytes((short) (62 * 1024), ByteOrder.BIG_ENDIAN)); bb.put(ByteUtil.intToBytes(idx, ByteOrder.BIG_ENDIAN)); } @@ -7080,7 +7111,41 @@ public class SydApi4j implements SydApi { // 通信 synchronized (this) { - bb = syncRead(syncSend(bb)); + int originalTimeout = -1; + boolean timeoutModified = false; + + try { + // 仅当 dataType == 2 时才执行超时时间调整逻辑 + if (false && dataType == 2) { + // 获取当前 Socket 超时时间(毫秒),可能为 -1(无限等待)或 0(立即超时) + originalTimeout = this.socket.getSoTimeout(); + + // 计算 totalLen 是 1MB 的多少倍(整数倍,如 2M→2,3M→3,4M→4) + int rate = totalLen / (1024 * 1024); + + // 仅当倍数 >1 且原超时时间为正数时,才按比例放大超时时间 + if (rate > 1 && originalTimeout > 0) { + int newTimeout = originalTimeout * rate; + this.socket.setSoTimeout(newTimeout); + timeoutModified = true; // 标记已修改,以便 finally 中恢复 + } + } + + // 响应解析(无论是否调整超时,都正常执行) + bb = syncRead(syncSend(bb)); + + } catch (SocketException e) { + throw new SydApiException("socket 状态错误", -5, e); + } finally { + // 仅当超时时间被实际修改过时才恢复原值 + if (timeoutModified) { + try { + this.socket.setSoTimeout(originalTimeout); + } catch (SocketException e) { + // 恢复失败时可根据需要记录日志,此处忽略 + } + } + } } bb.flip(); @@ -7097,7 +7162,7 @@ public class SydApi4j implements SydApi { int isLast = bb.get(); byte[] dataLenArr = new byte[2]; bb.get(dataLenArr); - int dataLen = ByteUtil.bytesToShort(dataLenArr, ByteOrder.BIG_ENDIAN); + int dataLen = Short.toUnsignedInt(ByteUtil.bytesToShort(dataLenArr, ByteOrder.BIG_ENDIAN)); ByteBuffer slice = ByteBufferUtil.sliceAndConsume(bb, dataLen); return new MutiReturn7(retCode, isLast, slice); } else { diff --git a/src/test/java/cmbpoc/FunctionTest.java b/src/test/java/cmbpoc/FunctionTest.java index 703be55..026024d 100644 --- a/src/test/java/cmbpoc/FunctionTest.java +++ b/src/test/java/cmbpoc/FunctionTest.java @@ -16,7 +16,10 @@ public class FunctionTest { private byte[] publicKey4Sign; private byte[] privateKey4Ende; private byte[] publicKey4Ende; - private byte[] orgData = new byte[15]; + private byte[] orgData = new byte[1024]; + private byte[] orgData2K = new byte[ 2 * 1024 ]; + private byte[] orgData2M = new byte[ 2 * 1024 * 1024 ]; + private byte[] orgData10M = new byte[ 4 * 1024 * 1024 - 1024]; private String sign; @Before @@ -26,7 +29,7 @@ public class FunctionTest { System.setProperty("com.sunyard.sydapi4j.debug", "true"); // 建立链接(单台) - this.api = (SydApi4j) new SydApi4j().connect("192.168.100.145", 8889, null, 5000); + this.api = (SydApi4j) new SydApi4j().connect("192.168.100.145", 8889, null, 30000); // RetWrap keypair = this.getPrivateKeyAndPublickKey(keypairIndex4Sign); // this.publicKey4Sign = Util.hexString2Bytes( keypair.get("pk").toString() ); @@ -103,6 +106,33 @@ public class FunctionTest { byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); Assert.assertArrayEquals(orgData, deData); } + + + @Test + public void SYD_SM2_Encrypt_2K(){ + byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2K); + System.out.println("密文=" + Util.bytes2HexString(enData)); + byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); + Assert.assertArrayEquals(orgData2K, deData); + } + + + @Test + public void SYD_SM2_Encrypt_2M(){ + byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2M); + System.out.println("密文=" + Util.bytes2HexString(enData)); + byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); + Assert.assertArrayEquals(orgData2M, deData); + } + + + @Test + public void SYD_SM2_Encrypt_10M(){ + byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData10M); + System.out.println("密文=" + Util.bytes2HexString(enData)); + byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); + Assert.assertArrayEquals(orgData10M, deData); + } } From 76809e5521fd14e65a23ddfe5f78cd9cbc41ccc3 Mon Sep 17 00:00:00 2001 From: cheney Date: Wed, 3 Jun 2026 17:23:41 +0800 Subject: [PATCH 12/18] =?UTF-8?q?=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/cmbpoc/FunctionTest.java | 6 +-- src/test/java/cmbpoc/JmeterTest.java | 59 ++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/test/java/cmbpoc/FunctionTest.java b/src/test/java/cmbpoc/FunctionTest.java index 026024d..aa40682 100644 --- a/src/test/java/cmbpoc/FunctionTest.java +++ b/src/test/java/cmbpoc/FunctionTest.java @@ -19,7 +19,7 @@ public class FunctionTest { private byte[] orgData = new byte[1024]; private byte[] orgData2K = new byte[ 2 * 1024 ]; private byte[] orgData2M = new byte[ 2 * 1024 * 1024 ]; - private byte[] orgData10M = new byte[ 4 * 1024 * 1024 - 1024]; + private byte[] orgData10M = new byte[ 10 * 1024 * 1024 ]; private String sign; @Before @@ -130,8 +130,8 @@ public class FunctionTest { public void SYD_SM2_Encrypt_10M(){ byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData10M); System.out.println("密文=" + Util.bytes2HexString(enData)); - byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); - Assert.assertArrayEquals(orgData10M, deData); +// byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); +// Assert.assertArrayEquals(orgData10M, deData); } } diff --git a/src/test/java/cmbpoc/JmeterTest.java b/src/test/java/cmbpoc/JmeterTest.java index eed2fca..2309c00 100644 --- a/src/test/java/cmbpoc/JmeterTest.java +++ b/src/test/java/cmbpoc/JmeterTest.java @@ -1,34 +1,87 @@ package cmbpoc; +import com.sunyard.proto.Util; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; +import racal.sunyard.main.SydApi4j; + +import java.util.Random; public class JmeterTest extends AbstractJavaSamplerClient { private byte[] publicKey; private byte[] plainData; + private byte[] encryptedData; private int sleep = 1; + private byte[] privateKey4Ende; + private byte[] publicKey4Ende; + + private String ip ; + private int port; + private int timeout; + private String ip2 ; + + private ThreadLocal api; + @Override public Arguments getDefaultParameters() { Arguments params = new Arguments(); params.addArgument("function", "sleep"); params.addArgument("sleep", "1"); + params.addArgument("plainDataLen", "1024"); + params.addArgument("publicKey4Ende", "", "加密用的公钥"); + params.addArgument("privateKey4Ende", "", "解密用的公钥"); + params.addArgument("ip", "192.168.100.145"); + params.addArgument("ip2", "172.1.41.96"); + params.addArgument("port", "8889"); + params.addArgument("timeout", "3000"); return params; } @Override public void setupTest(JavaSamplerContext context) { + // 加密密钥对 + String publicKey4Ende = context.getParameter("publicKey4Ende"); + if ( null != publicKey4Ende && !publicKey4Ende.isEmpty()) { + this.publicKey4Ende = Util.hexString2Bytes(publicKey4Ende); + } + String privateKey4Ende = context.getParameter("privateKey4Ende"); + if ( null != privateKey4Ende && !privateKey4Ende.isEmpty()) { + this.privateKey4Ende = Util.hexString2Bytes(privateKey4Ende); + } + + // 原始数据 + int plainDataLen = context.getIntParameter("plainDataLen"); + plainData = new byte[plainDataLen]; + new Random().nextBytes(plainData); + System.out.println("原始数据长度:" + plainDataLen); + // 校准测试 sleep = context.getIntParameter("sleep", 1); + // api + ip = context.getParameter("ip"); + ip2 = context.getParameter("ip2"); + port = context.getIntParameter("port"); + timeout = context.getIntParameter("timeout"); + api = new ThreadLocal() { + @Override + public SydApi4j initialValue() { + return (SydApi4j) new SydApi4j().connect(ip, port, null, timeout); + } + }; + // 线程开始时准备解密、验签数据 String function = context.getParameter("function"); switch (function) { + case "SYD_SM2_Decrypt": + this.encryptedData = this.api.get().SYD_SM2Encrypt(this.publicKey4Ende, this.plainData); + break; case "SYD_SM2_Sign": break; default: @@ -46,6 +99,12 @@ public class JmeterTest extends AbstractJavaSamplerClient { result.sampleStart(); // 开始计时 try { switch (function) { + case "SYD_SM2_Encrypt": + this.api.get().SYD_SM2Encrypt(privateKey4Ende, this.plainData); + break; + case "SYD_SM2_Decrypt": + this.api.get().SYD_SM2Decrypt(privateKey4Ende, this.encryptedData); + break; case "SYD_SM2_Sign": break; case "sleep": From 01be8429be666ac9d92918e287a50486720936ea Mon Sep 17 00:00:00 2001 From: cheney Date: Mon, 8 Jun 2026 14:08:25 +0800 Subject: [PATCH 13/18] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 21 + jmeter/boc 测试.jmx | 16 + jmeter/招行测试计划.jmx | 749 +++++++++++++++++++++++ src/test/java/cmbpoc/FunctionTest.java | 6 +- src/test/java/cmbpoc/JmeterTest.java | 2 +- src/test/java/cmbpoc/JmeterTestTest.java | 51 ++ 6 files changed, 841 insertions(+), 4 deletions(-) create mode 100644 jmeter/boc 测试.jmx create mode 100644 jmeter/招行测试计划.jmx create mode 100644 src/test/java/cmbpoc/JmeterTestTest.java diff --git a/README.md b/README.md index 203bdce..3a42fa6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,24 @@ # sydapi4j 加密机 java 接口,基于 jna。 + + + +## 启动 Slave +在作为负载机的 Slave 机器上进行配置,主要有两种方式: + +方法一:配置 jmeter.properties 文件 +编辑 $JMETER_HOME/bin/jmeter.properties 文件,确认或取消注释以下配置: +``` +server.rmi.ssl.disable=true # 禁用SSL,简化内网配置 +server.rmi.localport=1099 # 固定RMI端口,方便防火墙放行 +java.rmi.server.hostname=Slave_IP # 绑定本机网卡IP,避免多网卡问题 +``` + +配置完成后,在 Slave 的 bin 目录下启动服务: + +Windows: 双击 jmeter-server.bat +Linux/Mac: 运行 ./jmeter-server + + + diff --git a/jmeter/boc 测试.jmx b/jmeter/boc 测试.jmx new file mode 100644 index 0000000..9235b7f --- /dev/null +++ b/jmeter/boc 测试.jmx @@ -0,0 +1,16 @@ + + + + + + false + true + false + + + + D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\dependences\bcpkix-jdk15on-1.65.jar,D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\dependences\bcprov-jdk15on-1.65.jar,D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\dependences\groovy-all-2.4.15.jar,D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\dependences\hutool-core-5.8.4.M1.jar,D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\dependences\proto-1.0.20220921.jar,D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\dependences\sydapi4j-1.13.20221010.jar,D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\cfitsignapi-1.0.jar + + + + diff --git a/jmeter/招行测试计划.jmx b/jmeter/招行测试计划.jmx new file mode 100644 index 0000000..0a261aa --- /dev/null +++ b/jmeter/招行测试计划.jmx @@ -0,0 +1,749 @@ + + + + + + false + true + true + + + + + + + continue + + false + 1000 + + 1 + 1 + false + + + true + + + + + + + function + sleep + = + + + sleep + 100 + = + + + plainDataLen + 1024 + = + + + publicKey4Ende + + = + + + privateKey4Ende + + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + + continue + + false + 10000 + + 1 + 1 + false + + + true + + + + + + + function + sleep + = + + + sleep + 1 + = + + + plainDataLen + 1024 + = + + + publicKey4Ende + + = + + + privateKey4Ende + + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + + continue + + false + 10 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Encrypt + = + + + sleep + 1 + = + + + plainDataLen + 2048 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + + + diff --git a/src/test/java/cmbpoc/FunctionTest.java b/src/test/java/cmbpoc/FunctionTest.java index aa40682..e9e85a4 100644 --- a/src/test/java/cmbpoc/FunctionTest.java +++ b/src/test/java/cmbpoc/FunctionTest.java @@ -120,7 +120,7 @@ public class FunctionTest { @Test public void SYD_SM2_Encrypt_2M(){ byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2M); - System.out.println("密文=" + Util.bytes2HexString(enData)); + System.out.println("密文长度=" + enData.length); byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); Assert.assertArrayEquals(orgData2M, deData); } @@ -130,8 +130,8 @@ public class FunctionTest { public void SYD_SM2_Encrypt_10M(){ byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData10M); System.out.println("密文=" + Util.bytes2HexString(enData)); -// byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); -// Assert.assertArrayEquals(orgData10M, deData); + byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); + Assert.assertArrayEquals(orgData10M, deData); } } diff --git a/src/test/java/cmbpoc/JmeterTest.java b/src/test/java/cmbpoc/JmeterTest.java index 2309c00..f610bd0 100644 --- a/src/test/java/cmbpoc/JmeterTest.java +++ b/src/test/java/cmbpoc/JmeterTest.java @@ -100,7 +100,7 @@ public class JmeterTest extends AbstractJavaSamplerClient { try { switch (function) { case "SYD_SM2_Encrypt": - this.api.get().SYD_SM2Encrypt(privateKey4Ende, this.plainData); + this.api.get().SYD_SM2Encrypt(publicKey4Ende, this.plainData); break; case "SYD_SM2_Decrypt": this.api.get().SYD_SM2Decrypt(privateKey4Ende, this.encryptedData); diff --git a/src/test/java/cmbpoc/JmeterTestTest.java b/src/test/java/cmbpoc/JmeterTestTest.java new file mode 100644 index 0000000..de11b89 --- /dev/null +++ b/src/test/java/cmbpoc/JmeterTestTest.java @@ -0,0 +1,51 @@ +package cmbpoc; + +import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; + + +import org.apache.jmeter.config.Arguments; +import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; +import org.apache.jmeter.samplers.SampleResult; + +public class JmeterTestTest { + + public static void main(String[] args) { + // 1. 创建 JMeter 参数集,并填入您提供的所有参数 + Arguments jmeterArgs = new Arguments(); + jmeterArgs.addArgument("function", "SYD_SM2_Encrypt"); + jmeterArgs.addArgument("sleep", "1"); + jmeterArgs.addArgument("plainDataLen", "2048"); + jmeterArgs.addArgument("publicKey4Ende", "03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7"); + jmeterArgs.addArgument("privateKey4Ende", "00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000"); + jmeterArgs.addArgument("ip", "192.168.100.145"); + jmeterArgs.addArgument("ip2", "172.1.41.96"); + jmeterArgs.addArgument("port", "8889"); + jmeterArgs.addArgument("timeout", "3000"); + + // 2. 基于参数集创建 JMeter 上下文对象 + JavaSamplerContext context = new JavaSamplerContext(jmeterArgs); + + // 3. 创建您的测试类实例 + JmeterTest jmeterTest = new JmeterTest(); + + try { + // 4. 模拟 JMeter 执行周期:setup -> run -> teardown + jmeterTest.setupTest(context); + + SampleResult result = jmeterTest.runTest(context); + if (result != null) { + System.out.println("SampleResult 状态: " + result.getResponseCode()); + System.out.println("响应数据: " + result.getResponseDataAsString()); + System.out.println("是否成功: " + result.isSuccessful()); + } else { + System.out.println("runTest 返回 null,请检查实现逻辑"); + } + + jmeterTest.teardownTest(context); + + } catch (Exception e) { + System.err.println("执行测试时发生异常:"); + e.printStackTrace(); + } + } +} \ No newline at end of file From d6a27f33419c3083b7aff1b0f1827064a94a72af Mon Sep 17 00:00:00 2001 From: cheney Date: Mon, 8 Jun 2026 15:14:22 +0800 Subject: [PATCH 14/18] =?UTF-8?q?=E6=80=A7=E8=83=BD=E7=AE=80=E5=8D=95?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/cmbpoc/PerformanceTest.java | 139 ++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/test/java/cmbpoc/PerformanceTest.java diff --git a/src/test/java/cmbpoc/PerformanceTest.java b/src/test/java/cmbpoc/PerformanceTest.java new file mode 100644 index 0000000..435beb4 --- /dev/null +++ b/src/test/java/cmbpoc/PerformanceTest.java @@ -0,0 +1,139 @@ +package cmbpoc; + +import com.sunyard.RetWrap; +import com.sunyard.SydApi; +import com.sunyard.proto.Util; +import org.junit.*; +import racal.sunyard.main.SydApi4j; + +public class PerformanceTest { + + private SydApi4j api; + private byte[] privateKey4Ende; + private byte[] publicKey4Ende; + private byte[] orgData2K = new byte[2 * 1024]; + private byte[] orgData2M = new byte[2 * 1024 * 1024]; + private byte[] orgData10M = new byte[10 * 1024 * 1024]; + private static final int TEST_ROUNDS = 10; + + @Before + public void start() { + System.setProperty("com.sunyard.sydapi4j.debug", "false"); + this.api = (SydApi4j) new SydApi4j().connect("192.168.100.145", 8889, null, 30000); + this.publicKey4Ende = Util.hexString2Bytes("03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7"); + this.privateKey4Ende = Util.hexString2Bytes("00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000"); + } + + @After + public void stop() { + if (null != api) { + this.api.disconnect(); + this.api = null; + } + } + + @Test + public void SYD_SM2_Encrypt_Performance_2K() { + System.out.println("=== SYD_SM2_Encrypt Performance Test - 2K Data ==="); + long totalEncryptTime = 0; + long totalDecryptTime = 0; + + for (int i = 0; i < TEST_ROUNDS; i++) { + long encryptStart = System.currentTimeMillis(); + byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2K); + long encryptEnd = System.currentTimeMillis(); + + long decryptStart = System.currentTimeMillis(); + byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); + long decryptEnd = System.currentTimeMillis(); + + totalEncryptTime += (encryptEnd - encryptStart); + totalDecryptTime += (decryptEnd - decryptStart); + + Assert.assertArrayEquals(orgData2K, deData); + } + + double avgEncryptTime = (double) totalEncryptTime / TEST_ROUNDS; + double avgDecryptTime = (double) totalDecryptTime / TEST_ROUNDS; + double encryptThroughput = (2.0 / avgEncryptTime) * 1000; + double decryptThroughput = (2.0 / avgDecryptTime) * 1000; + + System.out.println("Test Rounds: " + TEST_ROUNDS); + System.out.println("Data Size: 2KB"); + System.out.printf("Average Encrypt Time: %.2f ms%n", avgEncryptTime); + System.out.printf("Average Decrypt Time: %.2f ms%n", avgDecryptTime); + System.out.printf("Encrypt Throughput: %.2f KB/s%n", encryptThroughput); + System.out.printf("Decrypt Throughput: %.2f KB/s%n", decryptThroughput); + System.out.println(); + } + + @Test + public void SYD_SM2_Encrypt_Performance_2M() { + System.out.println("=== SYD_SM2_Encrypt Performance Test - 2M Data ==="); + long totalEncryptTime = 0; + long totalDecryptTime = 0; + + for (int i = 0; i < TEST_ROUNDS; i++) { + long encryptStart = System.currentTimeMillis(); + byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2M); + long encryptEnd = System.currentTimeMillis(); + + long decryptStart = System.currentTimeMillis(); + byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); + long decryptEnd = System.currentTimeMillis(); + + totalEncryptTime += (encryptEnd - encryptStart); + totalDecryptTime += (decryptEnd - decryptStart); + + Assert.assertArrayEquals(orgData2M, deData); + } + + double avgEncryptTime = (double) totalEncryptTime / TEST_ROUNDS; + double avgDecryptTime = (double) totalDecryptTime / TEST_ROUNDS; + double encryptThroughput = (2048.0 / avgEncryptTime) * 1000; + double decryptThroughput = (2048.0 / avgDecryptTime) * 1000; + + System.out.println("Test Rounds: " + TEST_ROUNDS); + System.out.println("Data Size: 2MB"); + System.out.printf("Average Encrypt Time: %.2f ms%n", avgEncryptTime); + System.out.printf("Average Decrypt Time: %.2f ms%n", avgDecryptTime); + System.out.printf("Encrypt Throughput: %.2f KB/s%n", encryptThroughput); + System.out.printf("Decrypt Throughput: %.2f KB/s%n", decryptThroughput); + System.out.println(); + } + + @Test + public void SYD_SM2_Encrypt_Performance_10M() { + System.out.println("=== SYD_SM2_Encrypt Performance Test - 10M Data ==="); + long totalEncryptTime = 0; + long totalDecryptTime = 0; + + for (int i = 0; i < TEST_ROUNDS; i++) { + long encryptStart = System.currentTimeMillis(); + byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData10M); + long encryptEnd = System.currentTimeMillis(); + + long decryptStart = System.currentTimeMillis(); + byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); + long decryptEnd = System.currentTimeMillis(); + + totalEncryptTime += (encryptEnd - encryptStart); + totalDecryptTime += (decryptEnd - decryptStart); + + Assert.assertArrayEquals(orgData10M, deData); + } + + double avgEncryptTime = (double) totalEncryptTime / TEST_ROUNDS; + double avgDecryptTime = (double) totalDecryptTime / TEST_ROUNDS; + double encryptThroughput = (10240.0 / avgEncryptTime) * 1000; + double decryptThroughput = (10240.0 / avgDecryptTime) * 1000; + + System.out.println("Test Rounds: " + TEST_ROUNDS); + System.out.println("Data Size: 10MB"); + System.out.printf("Average Encrypt Time: %.2f ms%n", avgEncryptTime); + System.out.printf("Average Decrypt Time: %.2f ms%n", avgDecryptTime); + System.out.printf("Encrypt Throughput: %.2f KB/s%n", encryptThroughput); + System.out.printf("Decrypt Throughput: %.2f KB/s%n", decryptThroughput); + System.out.println(); + } +} From b82002801d2fc268dca2054cde7f252e0219fb37 Mon Sep 17 00:00:00 2001 From: cheney Date: Mon, 8 Jun 2026 17:03:46 +0800 Subject: [PATCH 15/18] =?UTF-8?q?jmeter=20=E6=B5=8B=E8=AF=95=E5=8F=AF?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jmeter/招行测试计划.jmx | 553 +++++++++++++++++- .../java/com/sunyard/entity/TimeStats.java | 103 ++++ .../java/racal/sunyard/main/SydApi4j.java | 144 +++++ src/test/java/cmbpoc/PerformanceTest.java | 45 ++ 4 files changed, 838 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/sunyard/entity/TimeStats.java diff --git a/jmeter/招行测试计划.jmx b/jmeter/招行测试计划.jmx index 0a261aa..2a5b3ff 100644 --- a/jmeter/招行测试计划.jmx +++ b/jmeter/招行测试计划.jmx @@ -9,6 +9,7 @@ + @@ -243,7 +244,7 @@ - + continue false @@ -475,11 +476,11 @@ - + continue false - 10 + 10000 1 1 @@ -542,7 +543,7 @@ cmbpoc.JmeterTest - + false saveConfig @@ -579,7 +580,7 @@ - + false saveConfig @@ -653,7 +654,7 @@ - + false saveConfig @@ -698,7 +699,545 @@ false - + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + + continue + + false + 100 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Encrypt + = + + + sleep + 1 + = + + + plainDataLen + 2097152 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + + continue + + false + 100 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Encrypt + = + + + sleep + 1 + = + + + plainDataLen + 2048 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + false saveConfig diff --git a/src/main/java/com/sunyard/entity/TimeStats.java b/src/main/java/com/sunyard/entity/TimeStats.java new file mode 100644 index 0000000..ce53ed1 --- /dev/null +++ b/src/main/java/com/sunyard/entity/TimeStats.java @@ -0,0 +1,103 @@ +package com.sunyard.entity; + +/** + * SM2加密解密时间统计类 + * 用于记录发送、计算、接收三个阶段的耗时 + */ +public class TimeStats { + // 发送阶段总耗时(毫秒)- 循环发送数据块的时间 + private long sendTime; + // 计算阶段耗时(毫秒)- 执行加密/解密计算的时间 + private long computeTime; + // 接收阶段总耗时(毫秒)- 循环接收数据块的时间 + private long receiveTime; + // 发送次数 + private int sendCount; + // 接收次数 + private int receiveCount; + + public TimeStats() { + this.sendTime = 0; + this.computeTime = 0; + this.receiveTime = 0; + this.sendCount = 0; + this.receiveCount = 0; + } + + public long getSendTime() { + return sendTime; + } + + public void setSendTime(long sendTime) { + this.sendTime = sendTime; + } + + public long getComputeTime() { + return computeTime; + } + + public void setComputeTime(long computeTime) { + this.computeTime = computeTime; + } + + public long getReceiveTime() { + return receiveTime; + } + + public void setReceiveTime(long receiveTime) { + this.receiveTime = receiveTime; + } + + public int getSendCount() { + return sendCount; + } + + public void setSendCount(int sendCount) { + this.sendCount = sendCount; + } + + public int getReceiveCount() { + return receiveCount; + } + + public void setReceiveCount(int receiveCount) { + this.receiveCount = receiveCount; + } + + /** + * 获取总耗时 + */ + public long getTotalTime() { + return sendTime + computeTime + receiveTime; + } + + /** + * 获取各阶段耗时占比 + */ + public double getSendPercentage() { + long total = getTotalTime(); + return total == 0 ? 0 : (double) sendTime / total * 100; + } + + public double getComputePercentage() { + long total = getTotalTime(); + return total == 0 ? 0 : (double) computeTime / total * 100; + } + + public double getReceivePercentage() { + long total = getTotalTime(); + return total == 0 ? 0 : (double) receiveTime / total * 100; + } + + @Override + public String toString() { + return String.format( + "TimeStats{sendTime=%dms(%.1f%%), computeTime=%dms(%.1f%%), receiveTime=%dms(%.1f%%), total=%dms, sendCount=%d, receiveCount=%d}", + sendTime, getSendPercentage(), + computeTime, getComputePercentage(), + receiveTime, getReceivePercentage(), + getTotalTime(), + sendCount, receiveCount + ); + } +} diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index 6ba343f..7cc4334 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -16,6 +16,7 @@ import com.sunyard.constant.CertUsage; import com.sunyard.entity.ImportResult; import com.sunyard.entity.MutiReturn7; import com.sunyard.entity.Struct; +import com.sunyard.entity.TimeStats; import com.sunyard.log.ILogFactory; import com.sunyard.log.ILogger; import com.sunyard.proto.Packet; @@ -7170,6 +7171,149 @@ public class SydApi4j implements SydApi { } } + /** + * SM2 加密(支持大包分段)- 带时间统计 + * @param publicKey 公钥字节数组 + * @param pOrgData 明文数据 + * @param timeStats 时间统计对象(输出参数) + * @return 加密后的密文 + */ + public byte[] SYD_SM2Encrypt(byte[] publicKey, byte[] pOrgData, TimeStats timeStats){ + ParamChecker.checkNotNull(publicKey, "publicKey"); + ParamChecker.checkNotNull(pOrgData, "pOrgData"); + + final int CHUNK_SIZE = 62 * 1024; // 62 KB + int dataLen = pOrgData.length; + int offset = 0; + long sendStartTime = System.currentTimeMillis(); + int sendCount = 0; + + while (offset < dataLen) { + int len = Math.min(CHUNK_SIZE, dataLen - offset); + SYD_SM2Encrypt(1, publicKey, ByteBuffer.wrap(pOrgData, offset, len), offset, len, dataLen); + offset += len; + sendCount++; + } + long sendEndTime = System.currentTimeMillis(); + + long computeStartTime = System.currentTimeMillis(); + SYD_SM2Encrypt(2, publicKey, null, 0, 0, dataLen); + long computeEndTime = System.currentTimeMillis(); + + long receiveStartTime = System.currentTimeMillis(); + int idx = 0; + List chunks = new ArrayList<>(); + int totalLen = 0; + int isLast = 0; + int receiveCount = 0; + + while (isLast != 1) { + MutiReturn7 ret = SYD_SM2Encrypt(3, null, null, idx, 0, 0); + ByteBuffer bb = ret.getData(); + isLast = ret.getIsLast(); + + if (bb != null && bb.remaining() > 0) { + int remaining = bb.remaining(); + chunks.add(bb); + totalLen += remaining; + idx += remaining; + receiveCount++; + } else if (isLast != 1) { + throw new RuntimeException("Empty chunk with isLast=0"); + } + } + long receiveEndTime = System.currentTimeMillis(); + + byte[] result = new byte[totalLen]; + int offset2 = 0; + for (ByteBuffer chunk : chunks) { + int len = chunk.remaining(); + chunk.get(result, offset2, len); + offset2 += len; + } + + if (timeStats != null) { + timeStats.setSendTime(sendEndTime - sendStartTime); + timeStats.setComputeTime(computeEndTime - computeStartTime); + timeStats.setReceiveTime(receiveEndTime - receiveStartTime); + timeStats.setSendCount(sendCount); + timeStats.setReceiveCount(receiveCount); + } + + return result; + } + + /** + * SM2 解密(支持大包分段)- 带时间统计 + * @param privateKey 私钥字节数组 + * @param pCipherData 密文数据 + * @param timeStats 时间统计对象(输出参数) + * @return 解密后的明文 + */ + public byte[] SYD_SM2Decrypt(byte[] privateKey, byte[] pCipherData, TimeStats timeStats) { + ParamChecker.checkNotNull(privateKey, "privateKey"); + ParamChecker.checkNotNull(pCipherData, "pCipherData"); + + final int CHUNK_SIZE = 62 * 1024; // 62 KB,与加密保持一致 + int dataLen = pCipherData.length; + int offset = 0; + long sendStartTime = System.currentTimeMillis(); + int sendCount = 0; + + while (offset < dataLen) { + int len = Math.min(CHUNK_SIZE, dataLen - offset); + SYD_SM2Decrypt(1, privateKey, ByteBuffer.wrap(pCipherData, offset, len), offset, len, 0); + offset += len; + sendCount++; + } + long sendEndTime = System.currentTimeMillis(); + + long computeStartTime = System.currentTimeMillis(); + SYD_SM2Decrypt(2, privateKey, null, 0, 0, dataLen); + long computeEndTime = System.currentTimeMillis(); + + long receiveStartTime = System.currentTimeMillis(); + int idx = 0; + List chunks = new ArrayList<>(); + int totalLen = 0; + int isLast = 0; + int receiveCount = 0; + + while (isLast != 1) { + MutiReturn7 ret = SYD_SM2Decrypt(3, null, null, idx, 0, 0); + ByteBuffer bb = ret.getData(); + isLast = ret.getIsLast(); + + if (bb != null && bb.remaining() > 0) { + chunks.add(bb); + totalLen += bb.remaining(); + idx += bb.remaining(); + receiveCount++; + } else if (isLast != 1) { + throw new RuntimeException("Empty plain chunk with isLast=0"); + } + } + long receiveEndTime = System.currentTimeMillis(); + + byte[] result = new byte[totalLen]; + int offset2 = 0; + for (ByteBuffer chunk : chunks) { + int len = chunk.remaining(); + chunk.get(result, offset2, len); + offset2 += len; + } + + if (timeStats != null) { + timeStats.setSendTime(sendEndTime - sendStartTime); + timeStats.setComputeTime(computeEndTime - computeStartTime); + timeStats.setReceiveTime(receiveEndTime - receiveStartTime); + timeStats.setSendCount(sendCount); + timeStats.setReceiveCount(receiveCount); + } + + return result; + } + @Override public String SM2Encrypt(byte[] publicKey, byte[] pOrgData) { diff --git a/src/test/java/cmbpoc/PerformanceTest.java b/src/test/java/cmbpoc/PerformanceTest.java index 435beb4..ee2eaec 100644 --- a/src/test/java/cmbpoc/PerformanceTest.java +++ b/src/test/java/cmbpoc/PerformanceTest.java @@ -2,6 +2,7 @@ package cmbpoc; import com.sunyard.RetWrap; import com.sunyard.SydApi; +import com.sunyard.entity.TimeStats; import com.sunyard.proto.Util; import org.junit.*; import racal.sunyard.main.SydApi4j; @@ -136,4 +137,48 @@ public class PerformanceTest { System.out.printf("Decrypt Throughput: %.2f KB/s%n", decryptThroughput); System.out.println(); } + + @Test + public void SYD_SM2_Encrypt_TimeStats_2M() { + System.out.println("=== SYD_SM2_Encrypt Time Statistics Test - 2M Data ==="); + + // 预热 + for (int i = 0; i < 2; i++) { + byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2M); + byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData); + } + + // 正式测试 + TimeStats encryptStats = new TimeStats(); + TimeStats decryptStats = new TimeStats(); + + byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2M, encryptStats); + byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData, decryptStats); + + Assert.assertArrayEquals(orgData2M, deData); + + System.out.println("\n--- 加密阶段时间统计 ---"); + System.out.printf("发送阶段: %d ms (%.1f%%), 发送次数: %d%n", + encryptStats.getSendTime(), encryptStats.getSendPercentage(), encryptStats.getSendCount()); + System.out.printf("计算阶段: %d ms (%.1f%%)%n", + encryptStats.getComputeTime(), encryptStats.getComputePercentage()); + System.out.printf("接收阶段: %d ms (%.1f%%), 接收次数: %d%n", + encryptStats.getReceiveTime(), encryptStats.getReceivePercentage(), encryptStats.getReceiveCount()); + System.out.printf("加密总耗时: %d ms%n", encryptStats.getTotalTime()); + + System.out.println("\n--- 解密阶段时间统计 ---"); + System.out.printf("发送阶段: %d ms (%.1f%%), 发送次数: %d%n", + decryptStats.getSendTime(), decryptStats.getSendPercentage(), decryptStats.getSendCount()); + System.out.printf("计算阶段: %d ms (%.1f%%)%n", + decryptStats.getComputeTime(), decryptStats.getComputePercentage()); + System.out.printf("接收阶段: %d ms (%.1f%%), 接收次数: %d%n", + decryptStats.getReceiveTime(), decryptStats.getReceivePercentage(), decryptStats.getReceiveCount()); + System.out.printf("解密总耗时: %d ms%n", decryptStats.getTotalTime()); + + System.out.println("\n--- 汇总 ---"); + long totalTime = encryptStats.getTotalTime() + decryptStats.getTotalTime(); + System.out.printf("加密解密总耗时: %d ms%n", totalTime); + System.out.printf("加密吞吐量: %.2f KB/s%n", (2048.0 / encryptStats.getTotalTime()) * 1000); + System.out.printf("解密吞吐量: %.2f KB/s%n", (2048.0 / decryptStats.getTotalTime()) * 1000); + } } From 664bd110c643ecce168ef6338dd75f0c558ae2f6 Mon Sep 17 00:00:00 2001 From: cheney Date: Mon, 8 Jun 2026 18:08:10 +0800 Subject: [PATCH 16/18] =?UTF-8?q?jmeter=20=E6=B5=8B=E8=AF=95=E5=8F=AF?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jmeter/boc 测试.jmx | 16 - jmeter/招行测试计划.jmx | 891 +++++++++++++++++- .../java/racal/sunyard/main/SydApi4j.java | 6 +- src/test/java/cmbpoc/FunctionTest.java | 15 +- src/test/java/cmbpoc/JmeterTest.java | 23 +- 5 files changed, 928 insertions(+), 23 deletions(-) delete mode 100644 jmeter/boc 测试.jmx diff --git a/jmeter/boc 测试.jmx b/jmeter/boc 测试.jmx deleted file mode 100644 index 9235b7f..0000000 --- a/jmeter/boc 测试.jmx +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - false - true - false - - - - D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\dependences\bcpkix-jdk15on-1.65.jar,D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\dependences\bcprov-jdk15on-1.65.jar,D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\dependences\groovy-all-2.4.15.jar,D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\dependences\hutool-core-5.8.4.M1.jar,D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\dependences\proto-1.0.20220921.jar,D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\dependences\sydapi4j-1.13.20221010.jar,D:\sunyard\sydapi-boc\Shujujiaohuan\pack\jar\cfitsignapi-1.0.jar - - - - diff --git a/jmeter/招行测试计划.jmx b/jmeter/招行测试计划.jmx index 2a5b3ff..de0fdc4 100644 --- a/jmeter/招行测试计划.jmx +++ b/jmeter/招行测试计划.jmx @@ -54,6 +54,16 @@ = + + publicKey4Sign + + = + + + privateKey4Sign + + = + ip 192.168.100.145 @@ -286,6 +296,16 @@ = + + publicKey4Sign + + = + + + privateKey4Sign + + = + ip 192.168.100.145 @@ -476,7 +496,7 @@ - + continue false @@ -518,6 +538,16 @@ 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 = + + publicKey4Sign + + = + + + privateKey4Sign + + = + ip 192.168.100.145 @@ -745,7 +775,844 @@ - + + continue + + false + 10000 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Sign + = + + + sleep + 1 + = + + + plainDataLen + 2048 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + publicKey4Sign + 034200046508BC4FE965BEB1D57715C180135FC861FDC6527E82E7FD7A6CA25E7CE9CE75101181D7E2CDD16C1974652B38A045465C7FFBAC700E49F949AE5F4C57AEBEAA + = + + + privateKey4Sign + 00010000CF945DAFF49B9F3AB67A23E1E1BA700FF1F78F5DFFCBB518FD1303A51DABA6FC0000000000000000000000000000000000000000000000000000000000000000 + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + + continue + + false + 100 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Sign + = + + + sleep + 1 + = + + + plainDataLen + 2048000 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + publicKey4Sign + 034200046508BC4FE965BEB1D57715C180135FC861FDC6527E82E7FD7A6CA25E7CE9CE75101181D7E2CDD16C1974652B38A045465C7FFBAC700E49F949AE5F4C57AEBEAA + = + + + privateKey4Sign + 00010000CF945DAFF49B9F3AB67A23E1E1BA700FF1F78F5DFFCBB518FD1303A51DABA6FC0000000000000000000000000000000000000000000000000000000000000000 + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + + continue + + false + 100 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Sign + = + + + sleep + 1 + = + + + plainDataLen + 2097152 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + publicKey4Sign + 034200046508BC4FE965BEB1D57715C180135FC861FDC6527E82E7FD7A6CA25E7CE9CE75101181D7E2CDD16C1974652B38A045465C7FFBAC700E49F949AE5F4C57AEBEAA + = + + + privateKey4Sign + 00010000CF945DAFF49B9F3AB67A23E1E1BA700FF1F78F5DFFCBB518FD1303A51DABA6FC0000000000000000000000000000000000000000000000000000000000000000 + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + continue false @@ -787,6 +1654,16 @@ 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 = + + publicKey4Sign + + = + + + privateKey4Sign + + = + ip 192.168.100.145 @@ -1056,6 +1933,16 @@ 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 = + + publicKey4Sign + + = + + + privateKey4Sign + + = + ip 192.168.100.145 diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index 7cc4334..77a5542 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -332,6 +332,10 @@ public class SydApi4j implements SydApi { byte[] publicKey, byte[] orgData) { + if (SydApi.DATA_HASH == nOrgDataType) { + orgData = SM3Hash(Util.bytes2HexString(publicKey), orgData); + } + Proto74 proto = new Proto74(); // 填充数据 @@ -7797,7 +7801,7 @@ public class SydApi4j implements SydApi { } - ByteBuffer bb = ByteBuffer.allocate(4 * 1024 + 128); + ByteBuffer bb = ByteBuffer.allocate(5 * 1024); byte[] ret = new byte[2]; bb.put(ret); diff --git a/src/test/java/cmbpoc/FunctionTest.java b/src/test/java/cmbpoc/FunctionTest.java index e9e85a4..d6b8e4c 100644 --- a/src/test/java/cmbpoc/FunctionTest.java +++ b/src/test/java/cmbpoc/FunctionTest.java @@ -39,7 +39,9 @@ public class FunctionTest { // this.publicKey4Ende = Util.hexString2Bytes( keypair4Ende.get("pk").toString() ); // this.privateKey4Ende = Util.hexString2Bytes( keypair4Ende.get("sk").toString() ); - + // 使用预置密钥对 + this.publicKey4Sign = Util.hexString2Bytes( "034200046508BC4FE965BEB1D57715C180135FC861FDC6527E82E7FD7A6CA25E7CE9CE75101181D7E2CDD16C1974652B38A045465C7FFBAC700E49F949AE5F4C57AEBEAA" ); + this.privateKey4Sign = Util.hexString2Bytes( "00010000CF945DAFF49B9F3AB67A23E1E1BA700FF1F78F5DFFCBB518FD1303A51DABA6FC0000000000000000000000000000000000000000000000000000000000000000" ); this.publicKey4Ende = Util.hexString2Bytes("03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7"); this.privateKey4Ende = Util.hexString2Bytes("00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000"); } @@ -93,8 +95,15 @@ public class FunctionTest { @Test public void SYD_SM2_Sign(){ - String sign = this.api.SYD_SM2_Sign_HA(1, privateKey4Sign, publicKey4Sign, orgData); - this.api.SYD_SM2_Verify_HA(1, publicKey4Sign, orgData, sign); + String sign = this.api.SYD_SM2_Sign_HA( SydApi.DATA_HASH, privateKey4Sign, publicKey4Sign, orgData); + this.api.SYD_SM2_Verify_HA(SydApi.DATA_HASH, publicKey4Sign, orgData, sign); + } + + + @Test + public void SYD_SM2_Sign_2M(){ + String sign = this.api.SYD_SM2_Sign_HA( SydApi.DATA_HASH, privateKey4Sign, publicKey4Sign, orgData2M); + this.api.SYD_SM2_Verify_HA(SydApi.DATA_HASH, publicKey4Sign, orgData2M, sign); } diff --git a/src/test/java/cmbpoc/JmeterTest.java b/src/test/java/cmbpoc/JmeterTest.java index f610bd0..c0088b2 100644 --- a/src/test/java/cmbpoc/JmeterTest.java +++ b/src/test/java/cmbpoc/JmeterTest.java @@ -1,6 +1,7 @@ package cmbpoc; +import com.sunyard.SydApi; import com.sunyard.proto.Util; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; @@ -16,8 +17,11 @@ public class JmeterTest extends AbstractJavaSamplerClient { private byte[] publicKey; private byte[] plainData; private byte[] encryptedData; + private String sign; private int sleep = 1; + private byte[] privateKey4Sign; + private byte[] publicKey4Sign; private byte[] privateKey4Ende; private byte[] publicKey4Ende; @@ -36,6 +40,8 @@ public class JmeterTest extends AbstractJavaSamplerClient { params.addArgument("plainDataLen", "1024"); params.addArgument("publicKey4Ende", "", "加密用的公钥"); params.addArgument("privateKey4Ende", "", "解密用的公钥"); + params.addArgument("publicKey4Sign", "", "验签用的公钥"); + params.addArgument("privateKey4Sign", "", "签名用的公钥"); params.addArgument("ip", "192.168.100.145"); params.addArgument("ip2", "172.1.41.96"); params.addArgument("port", "8889"); @@ -45,6 +51,16 @@ public class JmeterTest extends AbstractJavaSamplerClient { @Override public void setupTest(JavaSamplerContext context) { + // 签名密钥对 + String publicKey4Sign = context.getParameter("publicKey4Sign"); + if ( null != publicKey4Sign && !publicKey4Sign.isEmpty()) { + this.publicKey4Sign = Util.hexString2Bytes(publicKey4Sign); + } + String privateKey4Sign = context.getParameter("privateKey4Sign"); + if ( null != privateKey4Sign && !privateKey4Sign.isEmpty()) { + this.privateKey4Sign = Util.hexString2Bytes(privateKey4Sign); + } + // 加密密钥对 String publicKey4Ende = context.getParameter("publicKey4Ende"); if ( null != publicKey4Ende && !publicKey4Ende.isEmpty()) { @@ -82,7 +98,8 @@ public class JmeterTest extends AbstractJavaSamplerClient { case "SYD_SM2_Decrypt": this.encryptedData = this.api.get().SYD_SM2Encrypt(this.publicKey4Ende, this.plainData); break; - case "SYD_SM2_Sign": + case "SYD_SM2_Verify": + this.sign = this.api.get().SYD_SM2_Sign_HA(SydApi.DATA_HASH, this.privateKey4Sign, this.publicKey4Sign, this.plainData); break; default: break; @@ -106,6 +123,10 @@ public class JmeterTest extends AbstractJavaSamplerClient { this.api.get().SYD_SM2Decrypt(privateKey4Ende, this.encryptedData); break; case "SYD_SM2_Sign": + this.api.get().SYD_SM2_Sign_HA(SydApi.DATA_HASH, privateKey4Sign, publicKey4Sign, this.plainData); + break; + case "SYD_SM2_Verify": + this.api.get().SYD_SM2_Verify_HA(SydApi.DATA_HASH, publicKey4Sign, this.plainData, this.sign); break; case "sleep": Thread.sleep( this.sleep ); From 83e5033ba0da61248ba050875fb8f1fb75cbe3a5 Mon Sep 17 00:00:00 2001 From: cheney Date: Tue, 9 Jun 2026 09:41:10 +0800 Subject: [PATCH 17/18] =?UTF-8?q?jmeter=20=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jmeter/招行测试计划.jmx | 2240 +++++++++++++++++++++--- src/test/java/cmbpoc/FunctionTest.java | 8 + 2 files changed, 1965 insertions(+), 283 deletions(-) diff --git a/jmeter/招行测试计划.jmx b/jmeter/招行测试计划.jmx index de0fdc4..c961d09 100644 --- a/jmeter/招行测试计划.jmx +++ b/jmeter/招行测试计划.jmx @@ -496,285 +496,6 @@ - - continue - - false - 10000 - - 1 - 1 - false - - - true - - - - - - - function - SYD_SM2_Encrypt - = - - - sleep - 1 - = - - - plainDataLen - 2048 - = - - - publicKey4Ende - 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 - = - - - privateKey4Ende - 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 - = - - - publicKey4Sign - - = - - - privateKey4Sign - - = - - - ip - 192.168.100.145 - = - - - ip2 - 172.1.41.96 - = - - - port - 8889 - = - - - timeout - 3000 - = - - - - cmbpoc.JmeterTest - - - - false - - saveConfig - - - true - true - true - - true - true - true - true - false - true - true - false - false - false - true - false - false - false - true - 0 - true - true - true - true - true - true - - - - - - - false - - saveConfig - - - true - true - true - - true - true - true - true - false - true - true - false - false - false - true - false - false - false - true - 0 - true - true - true - true - true - true - - - - - - - false - - saveConfig - - - true - true - true - - true - true - true - true - false - true - true - false - false - false - true - false - false - false - true - 0 - true - true - true - true - true - true - - - - - - - false - - saveConfig - - - true - true - true - - true - true - true - true - false - true - true - false - false - false - true - false - false - false - true - 0 - true - true - true - true - true - true - - - - 500 - false - - - - - false - false - - - - false - - saveConfig - - - true - true - true - - true - true - true - true - false - true - true - false - false - false - true - false - false - false - true - 0 - true - true - true - true - true - true - - - - 1000 - false - - - - - false - false - - - continue @@ -1054,7 +775,286 @@ - + + continue + + false + 10000 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Verify + = + + + sleep + 1 + = + + + plainDataLen + 2048 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + publicKey4Sign + 034200046508BC4FE965BEB1D57715C180135FC861FDC6527E82E7FD7A6CA25E7CE9CE75101181D7E2CDD16C1974652B38A045465C7FFBAC700E49F949AE5F4C57AEBEAA + = + + + privateKey4Sign + 00010000CF945DAFF49B9F3AB67A23E1E1BA700FF1F78F5DFFCBB518FD1303A51DABA6FC0000000000000000000000000000000000000000000000000000000000000000 + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + continue false @@ -1333,7 +1333,286 @@ - + + continue + + false + 100 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Verify + = + + + sleep + 1 + = + + + plainDataLen + 2048000 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + publicKey4Sign + 034200046508BC4FE965BEB1D57715C180135FC861FDC6527E82E7FD7A6CA25E7CE9CE75101181D7E2CDD16C1974652B38A045465C7FFBAC700E49F949AE5F4C57AEBEAA + = + + + privateKey4Sign + 00010000CF945DAFF49B9F3AB67A23E1E1BA700FF1F78F5DFFCBB518FD1303A51DABA6FC0000000000000000000000000000000000000000000000000000000000000000 + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + continue false @@ -1362,7 +1641,7 @@ plainDataLen - 2097152 + 10485760 = @@ -1612,6 +1891,843 @@ + + continue + + false + 100 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Verify + = + + + sleep + 1 + = + + + plainDataLen + 10485760 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + publicKey4Sign + 034200046508BC4FE965BEB1D57715C180135FC861FDC6527E82E7FD7A6CA25E7CE9CE75101181D7E2CDD16C1974652B38A045465C7FFBAC700E49F949AE5F4C57AEBEAA + = + + + privateKey4Sign + 00010000CF945DAFF49B9F3AB67A23E1E1BA700FF1F78F5DFFCBB518FD1303A51DABA6FC0000000000000000000000000000000000000000000000000000000000000000 + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + + continue + + false + 10000 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Encrypt + = + + + sleep + 1 + = + + + plainDataLen + 2048 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + publicKey4Sign + + = + + + privateKey4Sign + + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + + continue + + false + 10000 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Decrypt + = + + + sleep + 1 + = + + + plainDataLen + 2048 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + publicKey4Sign + + = + + + privateKey4Sign + + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + continue @@ -1891,6 +3007,285 @@ + + continue + + false + 100 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Decrypt + = + + + sleep + 1 + = + + + plainDataLen + 2097152 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + publicKey4Sign + + = + + + privateKey4Sign + + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + continue @@ -1920,7 +3315,286 @@ plainDataLen - 2048 + 10485760 + = + + + publicKey4Ende + 03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7 + = + + + privateKey4Ende + 00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000 + = + + + publicKey4Sign + + = + + + privateKey4Sign + + = + + + ip + 192.168.100.145 + = + + + ip2 + 172.1.41.96 + = + + + port + 8889 + = + + + timeout + 3000 + = + + + + cmbpoc.JmeterTest + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 500 + false + + + + + false + false + + + + false + + saveConfig + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + true + false + false + false + true + 0 + true + true + true + true + true + true + + + + 1000 + false + + + + + false + false + + + + + continue + + false + 100 + + 1 + 1 + false + + + true + + + + + + + function + SYD_SM2_Decrypt + = + + + sleep + 1 + = + + + plainDataLen + 10485760 = diff --git a/src/test/java/cmbpoc/FunctionTest.java b/src/test/java/cmbpoc/FunctionTest.java index d6b8e4c..5c35467 100644 --- a/src/test/java/cmbpoc/FunctionTest.java +++ b/src/test/java/cmbpoc/FunctionTest.java @@ -108,6 +108,14 @@ public class FunctionTest { + @Test + public void SYD_SM2_Sign_10M(){ + String sign = this.api.SYD_SM2_Sign_HA( SydApi.DATA_HASH, privateKey4Sign, publicKey4Sign, orgData10M); + this.api.SYD_SM2_Verify_HA(SydApi.DATA_HASH, publicKey4Sign, orgData10M, sign); + } + + + @Test public void SYD_SM2_Encrypt(){ byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData); From 09a910006f4e9006f1730f65ecaa3efa9b53e7b4 Mon Sep 17 00:00:00 2001 From: cheney Date: Wed, 10 Jun 2026 10:21:15 +0800 Subject: [PATCH 18/18] =?UTF-8?q?SM3=20=E6=80=A7=E8=83=BD=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/racal/sunyard/main/SydApi4j.java | 4 ++-- src/test/java/cmbpoc/PerformanceTest.java | 4 +++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3a42fa6..939dc3a 100644 --- a/README.md +++ b/README.md @@ -20,5 +20,5 @@ java.rmi.server.hostname=Slave_IP # 绑定本机网卡IP,避免多网卡问 Windows: 双击 jmeter-server.bat Linux/Mac: 运行 ./jmeter-server - +./jmeter-server -Djava.rmi.server.hostname=172.1.41.24 -Dserver.rmi.localport=1099 diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index 77a5542..fc84e7a 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -7745,7 +7745,7 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - final int packLen = 4064; // 2048 + final int packLen = 8192; //4064 // 2048 int loop = msg.length / packLen; if (msg.length % packLen > 0) { loop++; @@ -7801,7 +7801,7 @@ public class SydApi4j implements SydApi { } - ByteBuffer bb = ByteBuffer.allocate(5 * 1024); + ByteBuffer bb = ByteBuffer.allocate(10 * 1024 ); byte[] ret = new byte[2]; bb.put(ret); diff --git a/src/test/java/cmbpoc/PerformanceTest.java b/src/test/java/cmbpoc/PerformanceTest.java index ee2eaec..00da06b 100644 --- a/src/test/java/cmbpoc/PerformanceTest.java +++ b/src/test/java/cmbpoc/PerformanceTest.java @@ -20,7 +20,9 @@ public class PerformanceTest { @Before public void start() { System.setProperty("com.sunyard.sydapi4j.debug", "false"); - this.api = (SydApi4j) new SydApi4j().connect("192.168.100.145", 8889, null, 30000); + String ip = System.getProperty("ip", "192.168.100.145"); + System.out.println("ip=" + ip); + this.api = (SydApi4j) new SydApi4j().connect(ip, 8889, null, 30000); this.publicKey4Ende = Util.hexString2Bytes("03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7"); this.privateKey4Ende = Util.hexString2Bytes("00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000"); }