From 914ba8eee5662d960439efc1b6f28ab1f8f6ba8e Mon Sep 17 00:00:00 2001 From: cheney Date: Fri, 29 May 2026 16:25:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=A7=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 22 ++ .../java/com/sunyard/entity/MutiReturn7.java | 42 ++ .../java/com/sunyard/util/ByteBufferUtil.java | 44 +++ .../java/com/sunyard/util/ParamChecker.java | 367 ++++++++++++++++++ .../java/racal/sunyard/main/SydApi4j.java | 130 +++++++ src/test/java/cmbpoc/FunctionTest.java | 72 ++++ .../com/sunyard/sydapi/test/CertTest.java | 2 +- .../sydapi/test/TestShangqingXingye.java | 85 ++-- 8 files changed, 721 insertions(+), 43 deletions(-) create mode 100644 src/main/java/com/sunyard/entity/MutiReturn7.java create mode 100644 src/main/java/com/sunyard/util/ByteBufferUtil.java create mode 100644 src/main/java/com/sunyard/util/ParamChecker.java create mode 100644 src/test/java/cmbpoc/FunctionTest.java diff --git a/pom.xml b/pom.xml index 0a0ef4c..c26844e 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,28 @@ + + + org.bouncycastle + bcprov-jdk15on + 1.65 + + + + + + org.bouncycastle + bcpkix-jdk15on + 1.65 + + + + + com.github.houbb + junitperf + 1.0.3 + + com.sunyard proto diff --git a/src/main/java/com/sunyard/entity/MutiReturn7.java b/src/main/java/com/sunyard/entity/MutiReturn7.java new file mode 100644 index 0000000..ed80f49 --- /dev/null +++ b/src/main/java/com/sunyard/entity/MutiReturn7.java @@ -0,0 +1,42 @@ +package com.sunyard.entity; + +import java.nio.ByteBuffer; + +public class MutiReturn7 { + // 返回码 + private int ret; + // 0-不是最后 1-是最后 + private int isLast; + // 数据 + private ByteBuffer data; + + public MutiReturn7(int ret, int isLast, ByteBuffer data) { + this.ret = ret; + this.isLast = isLast; + this.data = data; + } + + public int getRet() { + return ret; + } + + public void setRet(int ret) { + this.ret = ret; + } + + public int getIsLast() { + return isLast; + } + + public void setIsLast(int isLast) { + this.isLast = isLast; + } + + public ByteBuffer getData() { + return data; + } + + public void setData(ByteBuffer data) { + this.data = data; + } +} diff --git a/src/main/java/com/sunyard/util/ByteBufferUtil.java b/src/main/java/com/sunyard/util/ByteBufferUtil.java new file mode 100644 index 0000000..c7762bd --- /dev/null +++ b/src/main/java/com/sunyard/util/ByteBufferUtil.java @@ -0,0 +1,44 @@ +package com.sunyard.util; + +import java.nio.ByteBuffer; + +public class ByteBufferUtil { + public static ByteBuffer sliceRange(ByteBuffer source, int start, int length) { + // 保存原 buffer 的 position 和 limit + int oldPos = source.position(); + int oldLimit = source.limit(); + + // 设置新的 position 和 limit 为目标区间 + source.position(start); + source.limit(start + length); + + // 创建共享视图(零拷贝) + ByteBuffer slice = source.slice(); + + // 恢复原 buffer 的 position 和 limit + source.position(oldPos); + source.limit(oldLimit); + + return slice; + } + + public static ByteBuffer sliceAndConsume(ByteBuffer source, int length) { + // 保存原 buffer 的 position 和 limit + int oldPos = source.position(); + int oldLimit = source.limit(); + + // 设置新的 position 和 limit 为目标区间 + int start = oldPos; + source.position(start); + source.limit(start + length); + + // 创建共享视图(零拷贝) + ByteBuffer slice = source.slice(); + + // 恢复原 buffer 的 position 和 limit + source.position(oldPos + length); + source.limit(oldLimit); + + return slice; + } +} diff --git a/src/main/java/com/sunyard/util/ParamChecker.java b/src/main/java/com/sunyard/util/ParamChecker.java new file mode 100644 index 0000000..72ee0db --- /dev/null +++ b/src/main/java/com/sunyard/util/ParamChecker.java @@ -0,0 +1,367 @@ +package com.sunyard.util; + +import java.util.Collection; +import java.util.Map; + +/** + * 参数检查工具类,提供常见的参数校验方法。 + *

+ * 所有方法在校验失败时均抛出 {@link IllegalArgumentException} 异常, + * 并附带包含参数名的明确错误信息。成功时返回被校验的值,便于链式调用。 + *

+ * + *

使用示例: + *

{@code
+ * public void setAge(int age) {
+ *     ParamChecker.checkInRange(age, "age", 0, 150);
+ * }
+ *
+ * public void setName(String name) {
+ *     ParamChecker.checkNotBlank(name, "name");
+ * }
+ *
+ * public void setStatus(int status) {
+ *     ParamChecker.checkInValues(status, "status", 1, 2, 3);
+ * }
+ * }
+ *

+ * + * @author Cheney + */ +public final class ParamChecker { + + private ParamChecker() { + // 私有构造器,防止实例化 + } + + /** + * 检查表达式是否为真,若为假则抛出异常。 + * + * @param expression 待检查的布尔表达式 + * @param message 异常信息 + * @throws IllegalArgumentException 如果 expression 为 false + */ + public static void checkArgument(boolean expression, String message) { + if (!expression) { + throw new IllegalArgumentException(message); + } + } + + // ==================== 对象非空检查 ==================== + + /** + * 检查对象不为 null。 + * + * @param obj 待检查的对象 + * @param paramName 参数名称(用于异常信息) + * @param 对象类型 + * @return 传入的对象(便于链式调用) + * @throws IllegalArgumentException 如果 obj 为 null + */ + public static T checkNotNull(T obj, String paramName) { + if (obj == null) { + throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null"); + } + return obj; + } + + // ==================== 字符串检查 ==================== + + /** + * 检查字符串不为 null 且长度大于 0(不忽略空白字符)。 + * + * @param str 待检查的字符串 + * @param paramName 参数名称 + * @return 传入的字符串 + * @throws IllegalArgumentException 如果 str 为 null 或空字符串 + */ + public static String checkNotEmpty(String str, String paramName) { + if (str == null || str.isEmpty()) { + throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null 或空字符串"); + } + return str; + } + + /** + * 检查字符串不为 null 且去除首尾空白后长度大于 0。 + * + * @param str 待检查的字符串 + * @param paramName 参数名称 + * @return 传入的字符串(原值,非 trim 后结果) + * @throws IllegalArgumentException 如果 str 为 null 或仅包含空白字符 + */ + public static String checkNotBlank(String str, String paramName) { + if (str == null || str.isBlank()) { + throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null、空字符串或仅包含空白字符"); + } + return str; + } + + /** + * 检查字符串长度在指定范围内(包含边界)。 + * + * @param str 待检查的字符串 + * @param paramName 参数名称 + * @param min 最小长度(包含) + * @param max 最大长度(包含) + * @return 传入的字符串 + * @throws IllegalArgumentException 如果 str 为 null,或长度不在 [min, max] 范围内 + */ + public static String checkLengthBetween(String str, String paramName, int min, int max) { + checkNotNull(str, paramName); + if (min > max) { + throw new IllegalArgumentException("内部错误:长度检查的最小值 " + min + " 不能大于最大值 " + max); + } + int len = str.length(); + if (len < min || len > max) { + throw new IllegalArgumentException( + String.format("参数 '%s' 的长度 %d 不在允许范围 [%d, %d] 内", paramName, len, min, max)); + } + return str; + } + + // ==================== 整数(int)检查 ==================== + + /** + * 检查 int 值大于 0。 + * + * @param value 待检查的值 + * @param paramName 参数名称 + * @return 传入的值 + * @throws IllegalArgumentException 如果 value <= 0 + */ + public static int checkPositive(int value, String paramName) { + if (value <= 0) { + throw new IllegalArgumentException("参数 '" + paramName + "' 必须为正数,当前值: " + value); + } + return value; + } + + /** + * 检查 int 值 >= 0。 + * + * @param value 待检查的值 + * @param paramName 参数名称 + * @return 传入的值 + * @throws IllegalArgumentException 如果 value < 0 + */ + public static int checkNonNegative(int value, String paramName) { + if (value < 0) { + throw new IllegalArgumentException("参数 '" + paramName + "' 不能为负数,当前值: " + value); + } + return value; + } + + /** + * 检查 int 值在指定范围内(包含边界)。 + * + * @param value 待检查的值 + * @param paramName 参数名称 + * @param min 最小值(包含) + * @param max 最大值(包含) + * @return 传入的值 + * @throws IllegalArgumentException 如果 value 不在 [min, max] 范围内,或 min > max + */ + public static int checkInRange(int value, String paramName, int min, int max) { + if (min > max) { + throw new IllegalArgumentException("内部错误:范围检查的最小值 " + min + " 不能大于最大值 " + max); + } + if (value < min || value > max) { + throw new IllegalArgumentException( + String.format("参数 '%s' 的值 %d 不在允许范围 [%d, %d] 内", paramName, value, min, max)); + } + return value; + } + + /** + * 检查 int 值是否等于给定集合中的任意一个值。 + * + * @param value 待检查的值 + * @param paramName 参数名称 + * @param validValues 允许的值列表 + * @return 传入的值 + * @throws IllegalArgumentException 如果 value 不在 validValues 中,或 validValues 为空 + */ + public static int checkInValues(int value, String paramName, int... validValues) { + if (validValues == null || validValues.length == 0) { + throw new IllegalArgumentException("内部错误:允许的值集合不能为空"); + } + for (int v : validValues) { + if (value == v) { + return value; + } + } + // 构建允许值字符串用于提示 + StringBuilder allowed = new StringBuilder(); + for (int i = 0; i < validValues.length; i++) { + if (i > 0) allowed.append(", "); + allowed.append(validValues[i]); + } + throw new IllegalArgumentException( + String.format("参数 '%s' 的值 %d 不在允许值集合 [%s] 中", paramName, value, allowed)); + } + + // ==================== 长整型(long)检查 ==================== + + /** + * 检查 long 值大于 0。 + * + * @param value 待检查的值 + * @param paramName 参数名称 + * @return 传入的值 + * @throws IllegalArgumentException 如果 value <= 0 + */ + public static long checkPositive(long value, String paramName) { + if (value <= 0) { + throw new IllegalArgumentException("参数 '" + paramName + "' 必须为正数,当前值: " + value); + } + return value; + } + + /** + * 检查 long 值 >= 0。 + * + * @param value 待检查的值 + * @param paramName 参数名称 + * @return 传入的值 + * @throws IllegalArgumentException 如果 value < 0 + */ + public static long checkNonNegative(long value, String paramName) { + if (value < 0) { + throw new IllegalArgumentException("参数 '" + paramName + "' 不能为负数,当前值: " + value); + } + return value; + } + + /** + * 检查 long 值在指定范围内(包含边界)。 + * + * @param value 待检查的值 + * @param paramName 参数名称 + * @param min 最小值(包含) + * @param max 最大值(包含) + * @return 传入的值 + * @throws IllegalArgumentException 如果 value 不在 [min, max] 范围内,或 min > max + */ + public static long checkInRange(long value, String paramName, long min, long max) { + if (min > max) { + throw new IllegalArgumentException("内部错误:范围检查的最小值 " + min + " 不能大于最大值 " + max); + } + if (value < min || value > max) { + throw new IllegalArgumentException( + String.format("参数 '%s' 的值 %d 不在允许范围 [%d, %d] 内", paramName, value, min, max)); + } + return value; + } + + // ==================== 浮点数(double)简单检查 ==================== + + /** + * 检查 double 值大于 0(考虑极小正数,不做精度处理)。 + * + * @param value 待检查的值 + * @param paramName 参数名称 + * @return 传入的值 + * @throws IllegalArgumentException 如果 value <= 0 或为 NaN + */ + public static double checkPositive(double value, String paramName) { + if (value <= 0 || Double.isNaN(value)) { + throw new IllegalArgumentException("参数 '" + paramName + "' 必须为正数,当前值: " + value); + } + return value; + } + + /** + * 检查 double 值 >= 0。 + * + * @param value 待检查的值 + * @param paramName 参数名称 + * @return 传入的值 + * @throws IllegalArgumentException 如果 value < 0 或为 NaN + */ + public static double checkNonNegative(double value, String paramName) { + if (value < 0 || Double.isNaN(value)) { + throw new IllegalArgumentException("参数 '" + paramName + "' 不能为负数或 NaN,当前值: " + value); + } + return value; + } + + // ==================== 集合与数组非空检查 ==================== + + /** + * 检查集合不为 null 且非空。 + * + * @param coll 待检查的集合 + * @param paramName 参数名称 + * @param 集合元素类型 + * @return 传入的集合 + * @throws IllegalArgumentException 如果 coll 为 null 或空集合 + */ + public static Collection checkNotEmpty(Collection coll, String paramName) { + if (coll == null || coll.isEmpty()) { + throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null 或空集合"); + } + return coll; + } + + /** + * 检查 Map 不为 null 且非空。 + * + * @param map 待检查的 Map + * @param paramName 参数名称 + * @param 键类型 + * @param 值类型 + * @return 传入的 Map + * @throws IllegalArgumentException 如果 map 为 null 或空 Map + */ + public static Map checkNotEmpty(Map map, String paramName) { + if (map == null || map.isEmpty()) { + throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null 或空 Map"); + } + return map; + } + + /** + * 检查数组不为 null 且长度大于 0。 + * + * @param array 待检查的数组 + * @param paramName 参数名称 + * @param 数组元素类型 + * @return 传入的数组 + * @throws IllegalArgumentException 如果 array 为 null 或长度为 0 + */ + public static T[] checkNotEmpty(T[] array, String paramName) { + if (array == null || array.length == 0) { + throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null 或空数组"); + } + return array; + } + + // ==================== 布尔条件检查 ==================== + + /** + * 检查布尔条件为 true。 + * + * @param condition 待检查的条件 + * @param paramName 参数名称(描述条件含义) + * @throws IllegalArgumentException 如果 condition 为 false + */ + public static void checkTrue(boolean condition, String paramName) { + if (!condition) { + throw new IllegalArgumentException("参数条件 '" + paramName + "' 必须为 true"); + } + } + + /** + * 检查布尔条件为 false。 + * + * @param condition 待检查的条件 + * @param paramName 参数名称(描述条件含义) + * @throws IllegalArgumentException 如果 condition 为 true + */ + public static void checkFalse(boolean condition, String paramName) { + if (condition) { + throw new IllegalArgumentException("参数条件 '" + paramName + "' 必须为 false"); + } + } +} diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index 69dbaf0..9e5d7de 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.StopWatch; import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.ByteUtil; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.sunyard.RetWrap; @@ -13,6 +14,7 @@ import com.sunyard.SydApiException; import com.sunyard.cert.X509; import com.sunyard.constant.CertUsage; import com.sunyard.entity.ImportResult; +import com.sunyard.entity.MutiReturn7; import com.sunyard.entity.Struct; import com.sunyard.log.ILogFactory; import com.sunyard.log.ILogger; @@ -6825,6 +6827,134 @@ public class SydApi4j implements SydApi { return SYMUtil.derEnData2Base64EnC1C2C3(((PacketSection) ret.get("cipherData")).getBytes()); } + + public byte[] SYD_SM2Encrypt(byte[] publicKey, byte[] pOrgData){ + ParamChecker.checkNotNull(publicKey, "publicKey"); + ParamChecker.checkNotNull(pOrgData, "pOrgData"); + + final int CHUNK_SIZE = 63 * 1024; // 64 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); + offset += len; + } + + // 计算 + SYD_SM2Encrypt(2, publicKey, null, 0); + + // 取回 + int idx = 0; + List chunks = new ArrayList<>(); + int totalLen = 0; + int isLast = 0; + + while (isLast != 1) { + MutiReturn7 ret = SYD_SM2Encrypt(3, null, null, idx); + ByteBuffer bb = ret.getData(); + isLast = ret.getIsLast(); + + if (bb != null && bb.remaining() > 0) { + int remaining = bb.remaining(); + chunks.add(bb); // 直接存储,不需要 duplicate + totalLen += remaining; + idx += remaining; + } else if (isLast != 1) { + // 没有数据但还不是最后一块,异常 + throw new RuntimeException("Empty chunk with isLast=0"); + } + } + + byte[] result = new byte[totalLen]; + int offset2 = 0; + for (ByteBuffer chunk : chunks) { + int len = chunk.remaining(); + chunk.get(result, offset2, len); + offset2 += len; + } + return result; + } + + // @Override + // 支持大包加解密 + private MutiReturn7 SYD_SM2Encrypt(int dataType, byte[] publicKey, ByteBuffer pOrgData, int idx) { + // 参数检查 + ParamChecker.checkInRange(dataType, "dataType", 1, 3); + + // 最大缓存 64K + // 根据输入数据快速估算缓存大小 + int buffLen = 4096; + if( 1 == dataType ) { + buffLen = pOrgData.limit() > 4096 ? 64*1024 : 5*1024; + } + + + ByteBuffer bb = ByteBuffer.allocate(buffLen ); + PacketSN sn = PacketSN.gen(); + bb.put(new byte[2]); + bb.put(sn.getSn().array()); + bb.put("7F".getBytes()); + bb.put((byte) dataType); // 数据包标志 + if ( 2 == dataType ) { // 公钥 + bb.put(ByteUtil.shortToBytes((short) publicKey.length)); + bb.put(publicKey); + } + if ( 1 == dataType ) { // 数据 + bb.put(ByteUtil.shortToBytes((short) pOrgData.limit())); + bb.put(pOrgData); + bb.put(ByteUtil.shortToBytes((short)idx)); + } + + if ( 3 == dataType) { // 仅数据长度 + bb.put( ByteUtil.shortToBytes((short) (63*1024)) ); + bb.put( ByteUtil.shortToBytes((short)idx)); + } + + + // 重新计算长度 + int len = bb.position(); + len -= 2; + byte[] lenArray = new byte[2]; + lenArray[0] = (byte) ((len >> 8) & 0xFF); + lenArray[1] = (byte) (len & 0xFF); + bb.put(lenArray); + + // 通信 + synchronized (this) { + // 响应解析 + bb = syncRead(syncSend(bb)); + } + + bb.flip(); + bb.position(12); + byte[] code = new byte[2]; + bb.get(code); + + int retCode = Integer.valueOf(new String(code)); + if (0 != retCode) { + throw new SydApiException(retCode); + } + + // 只有类型是 3 时有数据结果 + if ( 3 == dataType ){ + int isLast = bb.get(); + byte[] dataLenArr = new byte[2]; + bb.get(dataLenArr); + int dataLen = ByteUtil.bytesToInt(dataLenArr); +// byte[] data = new byte[dataLen]; +// bb.get(data); + ByteBuffer slice = ByteBufferUtil.sliceAndConsume(bb, dataLen); + return new MutiReturn7(retCode, isLast, slice); + } else { + return new MutiReturn7(retCode, 0, null); + } + } + + + @Override public String SM2Encrypt(byte[] publicKey, byte[] pOrgData) { Proto77 proto = new Proto77(); diff --git a/src/test/java/cmbpoc/FunctionTest.java b/src/test/java/cmbpoc/FunctionTest.java new file mode 100644 index 0000000..18ebd11 --- /dev/null +++ b/src/test/java/cmbpoc/FunctionTest.java @@ -0,0 +1,72 @@ +package cmbpoc; + +import com.sunyard.proto.Util; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +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 sign; + + @Before + public void start(){ + // 调试模式 + System.setProperty("com.sunyard.sydapi4j.debug", "true"); + + // 建立链接(单台) + this.api = (SydApi4j) new SydApi4j().connect("192.168.0.100", 8889, null, 1000); + + } + + + @After + public void stop(){ + if ( null != api ) { + this.api.disconnect(); + this.api = null; + } + } + + + + @Test + public void getPrivateKeyAndPublickKey(){ + this.publicKey = Util.hexString2Bytes( + this.api.SM2GetPublicKeyC(keypairIndex) + ); + + + this.privateKey = Util.hexString2Bytes( + this.api.SM2GetPrivateKeyC(keypairIndex) + ); + } + + + + @Test + public void SYD_SM2_Sign(){ + this.api.SYD_SM2_Sign_HA(1, privateKey, publicKey, orgData); + } + + + @Test + public void SYD_SM2_Verify(){ + this.api.SYD_SM2_Verify_HA(1, publicKey, orgData, sign); + } + + + @Test + public void SYD_SM2_Encrypt(){ + this.api.SM2Decrypt(); + } + + + +} diff --git a/src/test/java/com/sunyard/sydapi/test/CertTest.java b/src/test/java/com/sunyard/sydapi/test/CertTest.java index a2f66d8..61aa20b 100644 --- a/src/test/java/com/sunyard/sydapi/test/CertTest.java +++ b/src/test/java/com/sunyard/sydapi/test/CertTest.java @@ -22,7 +22,7 @@ public class CertTest { public static void main(String[] args) { //System.setProperty("com.sunyard.sydapi4j.debug", "true"); - SydApi4j api = (SydApi4j) new SydApi4j().connect("172.16.18.5", 8889, null, 1000); + SydApi4j api = (SydApi4j) new SydApi4j().connect("192.168.1.129", 8889, null, 1000); try{ Listdns = api.getAllCert(); System.out.println(Arrays.toString( dns.toArray() ) ); diff --git a/src/test/java/com/sunyard/sydapi/test/TestShangqingXingye.java b/src/test/java/com/sunyard/sydapi/test/TestShangqingXingye.java index e250b75..5e746d2 100644 --- a/src/test/java/com/sunyard/sydapi/test/TestShangqingXingye.java +++ b/src/test/java/com/sunyard/sydapi/test/TestShangqingXingye.java @@ -8,54 +8,55 @@ import racal.sunyard.main.SydApiBuilder; public class TestShangqingXingye { private static final byte[] orgDataBig = new byte[ 5 * 1024]; private static final byte[] orgData2k = new byte[ 2 * 1024]; - private static final String pcDnEN = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + private static final String pcDnEN = "C=CN,O=CFCA TEST SM2 OCA31,OU=SGE,OU=Organizational-2,CN=SGE051@7028-702821-b70280002@Z000000001@1"; + private static final String pcDnEN2 = "C=CN,O=OCA31SM2,OU=Local RA OCA31,OU=Organizational-2,CN=CFCA@sunyard@N91330108762038374D@52"; public static void main(String[] args) { //System.setProperty("com.sunyard.sydapi4j.debug", "true"); long startTime = System.currentTimeMillis(); //获取开始时间 - SydApiBuilder builder = new SydApiBuilder().setIp("192.168.0.100"); + SydApiBuilder builder = new SydApiBuilder().setIp("172.1.41.214"); SydApi4j api = (SydApi4j) builder.build(); try { - /** - * 产生对称密钥,通过公钥加密生成数字信封,。 - * @param pcDnEN: 清算所的公钥证书 - * @param pcData: 明文数据。 - * @return RetWrap: 数字信封 - */ - RetWrap signData = api.generateDEByDn(pcDnEN, orgDataBig); - byte[] cpkey2 = (byte[]) signData.get("CipherKey"); - System.out.println("加密信封:" + new String(cpkey2)); - - /** - * 接收用私钥解密数字信封,还原原文。。 - * @param pcDnEN: 清算所的公钥证书 - * @param cpkey: 数字信封。 - * @return preData: 原数据 - */ - RetWrap oDataWrap = api.decryptDEByDN(pcDnEN, cpkey2); - byte[] preData = (byte[]) oDataWrap.get("oData"); - System.out.println("解密信封:" + Util.bytes2HexString(preData)); - - - /** - * 通过指定的私钥对指定的原始数据编制带公钥证书的数字签名(遵循PKCS#7), - * @param orgData: 待签名的原始数据 - * @param pcDnEN: 签名者证书 DN - * @return signsData: 签名数据 - */ - String signsData = api.SYD_SM2_DetachedSign_HA(orgData2k, pcDnEN); - System.out.println("签名数据:" + signsData); - - /** - * 利用清算所的公钥证书对签名数据进行验签。 - * @param dataType: 数据模式, 0:输入的数据为 HASH 后的数据, 1:输入的数据为原始数据库(本次传入1) - * @param orgData: 待签名的原始数据 - * @param sign: 签名数据。 - */ - boolean result = api.detachedVerify(1, orgData2k, signsData); - System.out.println("验签结果:" + result); +// /** +// * 产生对称密钥,通过公钥加密生成数字信封,。 +// * @param pcDnEN: 清算所的公钥证书 +// * @param pcData: 明文数据。 +// * @return RetWrap: 数字信封 +// */ +// RetWrap signData = api.generateDEByDn(pcDnEN, orgDataBig); +// byte[] cpkey2 = (byte[]) signData.get("CipherKey"); +// System.out.println("加密信封:" + new String(cpkey2)); +// +// /** +// * 接收用私钥解密数字信封,还原原文。。 +// * @param pcDnEN: 清算所的公钥证书 +// * @param cpkey: 数字信封。 +// * @return preData: 原数据 +// */ +// RetWrap oDataWrap = api.decryptDEByDN(pcDnEN, cpkey2); +// byte[] preData = (byte[]) oDataWrap.get("oData"); +// System.out.println("解密信封:" + Util.bytes2HexString(preData)); +// +// +// /** +// * 通过指定的私钥对指定的原始数据编制带公钥证书的数字签名(遵循PKCS#7), +// * @param orgData: 待签名的原始数据 +// * @param pcDnEN: 签名者证书 DN +// * @return signsData: 签名数据 +// */ +// String signsData = api.SYD_SM2_DetachedSign_HA(orgData2k, pcDnEN); +// System.out.println("签名数据:" + signsData); +// +// /** +// * 利用清算所的公钥证书对签名数据进行验签。 +// * @param dataType: 数据模式, 0:输入的数据为 HASH 后的数据, 1:输入的数据为原始数据库(本次传入1) +// * @param orgData: 待签名的原始数据 +// * @param sign: 签名数据。 +// */ +// boolean result = api.detachedVerify(1, orgData2k, signsData); +// System.out.println("验签结果:" + result); /** @@ -67,7 +68,7 @@ public class TestShangqingXingye { * @param pcData: 明文数据。 * @return pcSignData: 签名数字信封 */ - String pcSignData = api.SYD_SignDigitalEnvelope(pcDnEN, pcDnEN, orgData2k); + String pcSignData = api.SYD_SignDigitalEnvelope(pcDnEN2, pcDnEN2, orgData2k); System.out.println("签名数字信封:" + pcSignData); /** @@ -79,7 +80,7 @@ public class TestShangqingXingye { * @return pcData:原数据 */ - byte[] oData = api.SYD_VerifyDigitalEnveloe(pcDnEN, pcDnEN, pcSignData); + byte[] oData = api.SYD_VerifyDigitalEnveloe(pcDnEN2, pcDnEN2, pcSignData); System.out.println("原数据:" + Util.bytes2HexString(oData));