diff --git a/src/main/java/com/sunyard/SydApiException.java b/src/main/java/com/sunyard/SydApiException.java index 366dc08..28cdce5 100644 --- a/src/main/java/com/sunyard/SydApiException.java +++ b/src/main/java/com/sunyard/SydApiException.java @@ -13,78 +13,83 @@ public class SydApiException extends RuntimeException{ return retCode; } - - public void transMsg(){ - switch (this.retCode){ + private static String transMsg(int code){ + String msg = "未定义的错误"; + switch (code){ case -1: case -2: case -3: - this.msg = "网络错误"; + msg = "网络错误"; break; case -4: - this.msg = "返回包的序列号有误"; + msg = "返回包的序列号有误"; break; case -5: - this.msg = "返回包的命令码有误"; + msg = "返回包的命令码有误"; break; case -6: - this.msg = "输出缓冲区预留太小"; + msg = "输出缓冲区预留太小"; break; case -25: - this.msg = "输入的 HASH 值长度应该为 32 个字节"; + msg = "输入的 HASH 值长度应该为 32 个字节"; break; case -26: - this.msg = "用户标识超长(应该<=16) "; + msg = "用户标识超长(应该<=16) "; break; case -27: - this.msg = "数据类型不合法 "; + msg = "数据类型不合法 "; break; case 33: - this.msg = "检索的数据或密钥不存在"; + msg = "检索的数据或密钥不存在"; break; case 4: - this.msg = "DER 编码格式错误(公钥) "; + msg = "DER 编码格式错误(公钥) "; break; case 21: - this.msg = "输入的数据格式错误 "; + msg = "输入的数据格式错误 "; break; case 19: - this.msg = "数据解密错(C3) "; + msg = "数据解密错(C3) "; break; case 2: - this.msg = "签名验证错误 "; + msg = "签名验证错误 "; break; case 129: - this.msg = "没有该指令"; + msg = "没有该指令"; break; case 0x8006 : { - this.msg = "输入的数据格式错误"; + msg = "输入的数据格式错误"; break; } case 32770: - this.msg = "签名验证错误(P7) "; + msg = "签名验证错误(P7) "; break; case 32773: - this.msg = "证书不可信"; + msg = "证书不可信"; break; case 32884: - this.msg = "证书或签名数据的格式错"; + msg = "证书或签名数据的格式错"; break; case 36868: - this.msg = "DN 所指向的实体不存在"; + msg = "DN 所指向的实体不存在"; break; case 40000: - this.msg = "输入的大数据处理异常"; + msg = "输入的大数据处理异常"; default: - this.msg = "未定义的错误"; + msg = "未定义的错误"; } + return msg; + } + + public void transMsg(){ + this.msg = transMsg( this.retCode ); } public SydApiException(int retCode) { @@ -95,7 +100,8 @@ public class SydApiException extends RuntimeException{ public SydApiException(String msg, int retCode){ - super("SydApiException(" + String.format("%04X", retCode) + ":" + msg +")" ); + super("SydApiException(" + String.format("%04X", retCode) + ":" + ( null == msg ? transMsg(retCode) : msg ) + ")" ); + this.retCode = retCode; if (null == msg || "".equals(msg)){ transMsg(); }else { @@ -119,7 +125,7 @@ public class SydApiException extends RuntimeException{ sb.append(Integer.toHexString(this.retCode)); sb.append(" ) "); sb.append(" msg = "); - sb.append(this.msg); + sb.append(msg); return sb.toString(); } } diff --git a/src/main/java/com/sunyard/inf/customize/SydHAApi.java b/src/main/java/com/sunyard/inf/customize/SydHAApi.java index 95b48e2..ed45c12 100644 --- a/src/main/java/com/sunyard/inf/customize/SydHAApi.java +++ b/src/main/java/com/sunyard/inf/customize/SydHAApi.java @@ -126,4 +126,11 @@ public interface SydHAApi { byte[] orgData, byte[] sign ); + + public boolean SM2Verify( + int nOrgDataType, + byte[] publicKey, + byte[] orgData, + String base64Sign + ); } diff --git a/src/main/java/com/sunyard/inf/customize/SydSDNXApiImpl.java b/src/main/java/com/sunyard/inf/customize/SydSDNXApiImpl.java deleted file mode 100644 index 9959469..0000000 --- a/src/main/java/com/sunyard/inf/customize/SydSDNXApiImpl.java +++ /dev/null @@ -1,185 +0,0 @@ -package com.sunyard.inf.customize; - -import com.sunyard.SydApiException; -import com.sunyard.cert.X509; -import com.sunyard.proto.Packet; -import com.sunyard.proto.PacketSection; -import com.sunyard.proto.Util; -import com.sunyard.trans.PacketSN; -import racal.sunyard.main.SydApi4j; -import racal.sunyard.main.proto.Proto8003; -import racal.sunyard.main.proto.Proto8004; -import racal.sunyard.main.proto.Proto8012; - -import java.io.UnsupportedEncodingException; -import java.nio.ByteBuffer; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -public class SydSDNXApiImpl extends SydApi4j implements SydSDNXApi { - - - public String nakedSign(byte[] orgData, String sCertDN) { - Proto8003 proto = new Proto8003(); //pset.getProtocolReqRender("75"); - byte[] dn = new byte[256]; - byte[] s = null; - try { - s = sCertDN.getBytes("GBK"); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - - System.arraycopy(s, 0, dn, 0, s.length); - - // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); - packet.put("data", data); - PacketSN sn = PacketSN.gen(); - packet.put("header", sn.getSn().array()); - data.put("signatureDataType$option", 1); - data.put("DN", dn); - data.put("data", orgData); - // 渲染发送 - ByteBuffer bb = proto.rend(packet); - - synchronized (this) { - - - // 响应解析 - bb = syncRead(syncSend(bb)); - } - - - Packet p = proto.parse(bb); - Map ret = p.toMap(); - - if ( - !Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes()) - ) { - throw new SydApiException("包序号不匹配", -4); - } - ret = (Map) ret.get("data"); - PacketSection error = (PacketSection) ret.get("error"); - byte[] be = error.getBytes(); - if ( - (0 != (be[0] ^ be[1]))) { - - throw new SydApiException(error.getInfo(), error.autoInt()); - - } - - return Util.encodeBase64(((PacketSection) ret.get("signature")).getBytes()); - //return ((PacketSection) ret.get("signature")).getBytes(); - } - - @Override - public boolean SYD_NakedVerify(byte[] orgData, String sign, String organizationNum) { - Proto8004 proto = new Proto8004(); //pset.getProtocolReqRender("75"); - - byte[] dn = new byte[256]; - byte[] s = null; - - - // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); - packet.put("data", data); - PacketSN sn = PacketSN.gen(); - packet.put("header", sn.getSn().array()); - data.put("signatureDataType$option", 2); - data.put("organizationNum", organizationNum); - data.put("data", orgData); - data.put("signature", Util.decodeBase64(sign)); - data.put("returnSign$option", 0); - // 渲染发送 - ByteBuffer bb = proto.rend(packet); - - synchronized (this) { - - - // 响应解析 - bb = syncRead(syncSend(bb)); - } - - Packet p = proto.parse(bb, packet); - Map ret = p.toMap(); - - if ( - !Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes()) - ) { - throw new SydApiException("包序号不匹配", -4); - } - ret = (Map) ret.get("data"); - PacketSection error = (PacketSection) ret.get("error"); - byte[] be = error.getBytes(); - if ((0 != (be[0] ^ be[1]))) { - - throw new SydApiException(error.getInfo(), error.autoInt()); - } - return true; - } - - @Override - public X509 SYD_Get_X509_CertInfo_HA(String bNumber, byte[] orgData, String sign) { - - Proto8012 proto = new Proto8012(); //pset.getProtocolReqRender("75"); - - - // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); - packet.put("data", data); - PacketSN sn = PacketSN.gen(); - packet.put("header", sn.getSn().array()); - data.put("originData", orgData); - data.put("signData", sign.getBytes()); - data.put("flag$option", 2); - data.put("organizationNum", bNumber); - // 渲染发送 - ByteBuffer bb = proto.rend(packet); - - synchronized (this) { - - - // 响应解析 - bb = syncRead(syncSend(bb)); - } - - Packet p = proto.parse(bb, packet); - Map ret = p.toMap(); - - if ( - !Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes()) - ) { - throw new SydApiException("包序号不匹配", -4); - } - ret = (Map) ret.get("data"); - PacketSection error = (PacketSection) ret.get("error"); - byte[] be = error.getBytes(); - if ((0 != (be[0] ^ be[1]))) { - - throw new SydApiException(error.getInfo(), error.autoInt()); - } - - PacketSection info = (PacketSection) ret.get("info"); - - byte[] pCertInfo = info.getBytes(); -// X509 cinfo = new X509(); -// cinfo.setIssuer(Util.toCleanString(pCertInfo, 0, 257)); -// cinfo.setSerialNumber(Util.toCleanString(pCertInfo, 257, 65)); -// cinfo.setSubject(Util.toCleanString(pCertInfo, 257 + 65, 257)); -// cinfo.setNotBefore(Util.toCleanString(pCertInfo, 257 + 65 + 257, 15)); -// cinfo.setNotAfter(Util.toCleanString(pCertInfo, 257 + 65 + 257 + 15, 15)); -// byte[] blen = new byte[]{pCertInfo[257 + 65 + 257 + 15 + 15], pCertInfo[257 + 65 + 257 + 15 + 16]}; -// int len = Util.b2i(blen); -// byte[] pem = new byte[len]; -// System.arraycopy(pCertInfo, 257 + 65 + 257 + 15 + 17, pem, 0, len); -// String pp = new String(pem); - // 解析证书 - return GetX509CertInfo(new String(pCertInfo)); - } - - -} diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index 7d7a5a9..73c6d25 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -3991,7 +3991,7 @@ public class SydApi4j implements SydApi { * @param orgData 要签名的信息 * @return 签名结果 */ - public String SM2SignC(int flag, String privateKey, byte[] publicKey, byte[] orgData) { + public byte[] SM2SignC(int flag, String privateKey, byte[] publicKey, byte[] orgData) { return SM2SignC(flag, "1234567812345678", privateKey, publicKey, orgData); } @@ -4209,6 +4209,17 @@ public class SydApi4j implements SydApi { return ((PacketSection) ret.get("signature")).getString(); } + @Override + public boolean SM2Verify( + int nOrgDataType, + byte[] publicKey, + byte[] orgData, + byte[] sign + ) { + String certNo = "1234567812345678"; + return SM2Verify(certNo, nOrgDataType, publicKey, orgData, sign); + } + @Override public boolean SM2Verify( int nOrgDataType, @@ -4217,9 +4228,10 @@ public class SydApi4j implements SydApi { String base64Sign ) { String certNo = "1234567812345678"; - return SM2Verify(certNo, nOrgDataType, publicKey, orgData, base64Sign); + return SM2Verify(certNo, nOrgDataType, publicKey, orgData, Util.decodeBase64(base64Sign) ); } + @Override public String SM2Sign( int nOrgDataType, diff --git a/src/test/java/com/sunyard/sydapi/test/TestHT.java b/src/test/java/com/sunyard/sydapi/test/TestHT.java index 4e4d6d9..e25715b 100644 --- a/src/test/java/com/sunyard/sydapi/test/TestHT.java +++ b/src/test/java/com/sunyard/sydapi/test/TestHT.java @@ -23,9 +23,8 @@ public class TestHT { @Before public void before() { // 通过设置debug的属性开启debug,如果不设置则默认不打开 -// System.setProperty("com.sunyard.sydapi4j.debug", "true"); - api = (SydApi4j) new SydApi4j().connect("172.16.18.59", 8889, null, 1000); -// api = (SydApi4j) new SydApi4j().connect("192.168.0.66", 8889, null, 1000); + System.setProperty("com.sunyard.sydapi4j.debug", "true"); + api = (SydApi4j) new SydApi4j().connect("192.168.0.200", 8889, null, 1000); } @After @@ -300,6 +299,7 @@ public class TestHT { @Test public void testDecryptDE54k() throws UnsupportedEncodingException { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; byte[] oData = new byte[4 * 1024]; @@ -310,6 +310,7 @@ public class TestHT { RetWrap retWrap1 = api.decryptDEByDN(dn, CipherKey); printRetWrap(retWrap1); + } diff --git a/src/test/java/com/sunyard/sydapi/test/TestSDNXApi.java b/src/test/java/com/sunyard/sydapi/test/TestSDNXApi.java deleted file mode 100644 index 8ea2dee..0000000 --- a/src/test/java/com/sunyard/sydapi/test/TestSDNXApi.java +++ /dev/null @@ -1,162 +0,0 @@ -package com.sunyard.sydapi.test; - -import com.sunyard.SydApi; -import com.sunyard.cert.X509; -import com.sunyard.inf.customize.SydSDNXApiImpl; -import racal.sunyard.main.SydApi4j; - -public class TestSDNXApi { - - private static final String testCertNo = "C=cn,O=CFCA TEST CA,OU=NCS2,OU=Enterprises,CN=041@Z402451000010@ShanDong@00000001"; - //private static final String testCertNo = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; - //private static final String testCertNo = "C=CN,O=Sunyard,OU=8001,CN=8001"; - //private static final String testData = "1015570000408702018091900000000012018-09-19T10:13:391015570000408702015970000397542018-09-1900000001123456,测试"; - private static final String testData = "0000000000000002019022310000129102019-02-23T09:29:19CC00"; - private static final String testUser = "201597"; - //private static final String testUser = "8001"; - //private static final String testUser = "402030000011"; - - public static void main(String[] args) { - - - SydApi api = null; - System.out.println("\n\n----- connect -----"); - - // 建议从配置文件读取配置 - - // 单服务器测试使用 -// PoolManager.INSTANCE.createPool("default", "{\n" + -// " \"mode\" : \"solo\", // (必填)\n" + -// " \"oipc\" : \"com.sunyard.pool4sydapi.SydApiSDNXInPool\", " + -// " \"ip\" : \"172.16.18.71\", // (必填)\n" + -// " \"port\": 8889, // (必填)【数字】\n" + -// " \"pwd\": \"\", // 没有就不填写此项\n" + -// " \"timeout\": 1000 //【数字】不限超时时间就不填写此项\n" + -// "}"); - - - // // 推荐配置 -// PoolManager.INSTANCE.createPool("default", "{\n" + -// " \"mode\" : \"round-robin\", // (必填)\n" + -// " \"oipc\" : \"com.sunyard.pool4sydapi.SydApiSDNXInPool\", // (必填)\n" + -// " \"ips\" : [ // (必填)\n" + -// " \"172.16.18.69\",\n" + -// " \"172.16.18.70\"\n" + -// " ],\n" + -// " \"port\": 8889, //(必填)【数字】\n" + -// " \"pwd\": \"\", //【数字】没有就不填写此项\n" + -// " \"timeout\": 1000 , //【数字】【数字】链接和响应超时时限,不限超时时间就不填写此项\n" + -// " \"connectTimeout\" : 1000, //【数字】【数字】链接超时时限,如果没有传递,但是存在 timeout 就等于 timeout 的值\n" + -// " \"waitRecvTimeout\" : 1000, //【数字】【数字】响应超时时限,如果没有传递,但是存在 timeout 就等于 timeout 的值\n" + -// " \"maxContinuousErrorTime\" : 3, //【数字】 单台加密机连续出错次数\n" + -// " \"recoveryTime\" : 300000, //【数字】 恢复时间,单位毫秒,默认 5 分钟,最大 2^63\n" + -// " \"keepAlive\" : 1, //【数字】 保持链接个数,默认是链接池个数的一半\n" + -// " \"maxAliveTime\" : 300000000, //【数字】 最大生存时间\n" + -// " \"maxIdleTime\" : 300000 , //【数字】 最大空闲时间,默认 5 分钟,最大 2^63\n" + -// " \"checkMode\" : \"before/after/deamon\", // 链接检测模式,使用前/使用后/后台轮询\n" + -// " \"checkGap\" : 300000 // 后台轮询检测间隔\n" + -// "}"); - - // 自定义配置 -// PoolManager.INSTANCE.createPool("default", "{\n" + -// " \"mode\" : \"weighted-round-robin\", // (必填)\n" + -// " \"oipc\" : \"com.sunyard.pool4sydapi.SydApiSDNXInPool\", // (必填)\n" + -// " \"ips\" : { // (必填)\n" + -// " \"172.16.18.69\" : 1,\n" + -// " \"172.16.18.70\" : 3\n" + -// " },\n" + -// " \"port\": 8889, //(必填)【数字】\n" + -// " \"pwd\": \"\", //【数字】没有就不填写此项\n" + -// " \"timeout\": 1000 , //【数字】【数字】链接和响应超时时限,不限超时时间就不填写此项\n" + -// " \"connectTimeout\" : 1000, //【数字】【数字】链接超时时限,如果没有传递,但是存在 timeout 就等于 timeout 的值\n" + -// " \"waitRecvTimeout\" : 1000, //【数字】【数字】响应超时时限,如果没有传递,但是存在 timeout 就等于 timeout 的值\n" + -// " \"maxContinuousErrorTime\" : 3, //【数字】 单台加密机连续出错次数\n" + -// " \"recoveryTime\" : 300000, //【数字】 恢复时间,单位毫秒,默认 5 分钟,最大 2^63\n" + -// " \"keepAlive\" : 1, //【数字】 保持链接个数,默认是链接池个数的一半\n" + -// " \"maxAliveTime\" : 300000000, //【数字】 最大生存时间\n" + -// " \"maxIdleTime\" : 300000 , //【数字】 最大空闲时间,默认 5 分钟,最大 2^63\n" + -// " \"checkMode\" : \"before/after/deamon\", // 链接检测模式,使用前/使用后/后台轮询\n" + -// " \"checkGap\" : 300000 // 后台轮询检测间隔\n" + -// "}"); - - - try { - - - //热备模式连接 - SydApi4j.SYD_Set_Parameter_HA("unkown", "122.224.86.107", 8889, 1000); - api = new SydApi4j(); - - - // 链接池和负载均衡模式 - //api = (SydSDNXApiImpl) PoolManager.INSTANCE.getPool().get().getOrigin(); - - System.out.println("连接加密机成功"); - } catch (Exception e) { - e.printStackTrace(); - } - - - try { - - - //用证书DN签名 - //case1(api); - - //根据机构号验签,上传并返回证书内容,并更新机构号 - case2(api); - - - //裸签名 - //case3(api); - - //验签 - //case4(api); - - } finally { - System.out.println("\n\n----- disconnect -----"); - api.disconnect(); - } - - System.out.println("执行结束\n\n"); - } - - public static void case1(SydSDNXApiImpl api) { - - System.out.println("\n\n----- 签名 基础接口原始数据模式 -----"); - String sign = api.detachedSign(SydApi4j.DATA_ORIGIN, testData.getBytes(), testCertNo); - System.out.println("\t签名结果: " + sign); - - - } - - //需要上传验签结果,测试的时候签名和验签不能同时进行,签名以后要删除数据库数据才可以验签(仅限测试可以这样操作) - public static void case2(SydApi api) { - - System.out.println("\n\n----- 验签 ,上传并返回证书内容,并更新机构号 -----"); - - String sign = "MIIEYQYKKoEcz1UGAQQCAqCCBFEwggRNAgEBMQ4wDAYIKoEcz1UBgxEFADAMBgoqgRzPVQYBBAIBoIIDUjCCA04wggLyoAMCAQICBUCEF1MkMAwGCCqBHM9VAYN1BQAwXDELMAkGA1UEBhMCQ04xMDAuBgNVBAoMJ0NoaW5hIEZpbmFuY2lhbCBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTEbMBkGA1UEAwwSQ0ZDQSBBQ1MgU00yIE9DQTMxMB4XDTE4MDExNzAxMTExMFoXDTIzMDExNzAxMTExMFowgYIxCzAJBgNVBAYTAkNOMRswGQYDVQQKDBJDRkNBIEFDUyBTTTIgT0NBMzExDjAMBgNVBAsMBVNIQ1BFMRkwFwYDVQQLDBBPcmdhbml6YXRpb25hbC0xMSswKQYDVQQDDCIwNTFA5LiK5rW356Wo5o2u5Lqk5piT5omAQEFBQUFBQUAxMFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEz79HajSQLou2kkL6n26pCKkcKcbHTWR6MbArRQruWY+WDiyPhRaYLU4W6d8wBTDTOS6T5dCtSNZBxeb5qoeeNaOCAXYwggFyMGwGCCsGAQUFBwEBBGAwXjAoBggrBgEFBQcwAYYcaHR0cDovL29jc3AuY2ZjYS5jb20uY24vb2NzcDAyBggrBgEFBQcwAoYmaHR0cDovL2NybC5jZmNhLmNvbS5jbi9vY2EzMS9vY2EzMS5jZXIwHwYDVR0jBBgwFoAUCNjRJsRIfZzsrJjp8X9iuYDOqUUwDAYDVR0TAQH/BAIwADBIBgNVHSAEQTA/MD0GCGCBHIbvKgEEMDEwLwYIKwYBBQUHAgEWI2h0dHA6Ly93d3cuY2ZjYS5jb20uY24vdXMvdXMtMTQuaHRtMDsGA1UdHwQ0MDIwMKAuoCyGKmh0dHA6Ly9jcmwuY2ZjYS5jb20uY24vb2NhMzEvU00yL2NybDcyLmNybDAOBgNVHQ8BAf8EBAMCBsAwHQYDVR0OBBYEFIzDvi38AZLlWA/Wqfk93ZPe7qNgMB0GA1UdJQQWMBQGCCsGAQUFBwMCBggrBgEFBQcDBDAMBggqgRzPVQGDdQUAA0gAMEUCICDVyelEL86xbj0mAeMl3knQD0n2QKERtni/e82bQz/nAiEAjq5SKIaVJu9T/lkds3QQfgLpBX65Rwj2Scgc7cC2dVcxgdMwgdACAQEwZTBcMQswCQYDVQQGEwJDTjEwMC4GA1UECgwnQ2hpbmEgRmluYW5jaWFsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MRswGQYDVQQDDBJDRkNBIEFDUyBTTTIgT0NBMzECBUCEF1MkMAwGCCqBHM9VAYMRBQAwDQYJKoEcz1UBgi0BBQAERzBFAiEA03ogUebmqNyLjD5/OXr4gSdVQfqp3yqrVVPgRmGPy1QCIBkHrzpSxw/ZPloL+t1GHlEp2rW7qVgy966/lQqE7UVH"; - X509 x509 = api.SYD_Get_X509_CertInfo_HA(testData.getBytes(), sign); - System.out.println("验签结果" + x509); - } - - - public static void case3(SydSDNXApiImpl api) { - - System.out.println("\n\n----- 裸签名 -----"); - String sign = api.nakedSign(testData.getBytes(), testCertNo); - System.out.println("签名结果 " + sign); - } - - public static void case4(SydSDNXApiImpl api) { - - System.out.println("\n\n----- 裸签名 -----"); - String sign = api.nakedSign(testData.getBytes(), testCertNo); - System.out.println("签名结果 " + sign); - //String sign = "dFOUuU+JVNEZO8494MV+xtPuMVhEjnXlilHlymK9Cz/jQ3m+lRKTbv4tfyo5Eb7MM7lqQi6cdwKZ/CLnkOa+Vsye4DDYYSweqsraLwdtrVcc1sw2KDHeJUao2SXIhdhgxSw+puiOCDq/4FsJADdnYprIp+GSLfjj7tuhbBBb0JCV6rnTutIozq2QDPQKEspzMxQ2TtbVQezfL3uMA5GuYNdJYz4eAasQtxTQS5dhe3/CaWbjBeL2kKhsvuMbP6wsx2HeuXvaf99OEBkyXq2fFPJtuU7WB4SIYKN1wI0BK/IMa0ULj1Q1SBakd5+iO8ckrokJJ6E2JH3Nd/mT+5DdRg=="; - - System.out.println("\n\n----- 验签 -----"); - boolean pass = api.SYD_NakedVerify(testData.getBytes(), sign, testUser); - System.out.println("验签结果 " + pass); - } -} diff --git a/src/test/java/sample/Sample.java b/src/test/java/sample/Sample.java new file mode 100644 index 0000000..a93a34f --- /dev/null +++ b/src/test/java/sample/Sample.java @@ -0,0 +1,415 @@ +package sample; + +import com.sunyard.RetWrap; +import com.sunyard.SydApi; +import com.sunyard.cert.X509; +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; + +import java.io.UnsupportedEncodingException; + + +/** + * 上清例子 + */ +public class Sample { + + SydApi4j api; + + @Before + public void before() { + // 通过设置debug的属性开启debug,如果不设置则默认不打开 + System.setProperty("com.sunyard.sydapi4j.debug", "true"); + api = (SydApi4j) new SydApi4j().connect("172.16.18.59", 8889, null, 1000); + } + + @After + public void after() { + try { + this.api.disconnect(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 流程演示 + */ + @Test + public void testProcess() { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] orgData = new byte[2 * 1024 * 1024]; + + // 签名 + String sign = api.attachedSign(orgData, dn); + System.out.println("签名结果: " + sign); + + // 生成数字信封 + RetWrap retWrap = api.generateDEByDn(dn, sign.getBytes()); + byte[] cipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY); + System.out.println("数字信封: " + new String(cipherKey)); + + // 解密数字信封 + RetWrap retWrap1 = api.decryptDEByDN(dn, cipherKey); + byte[] oData = (byte[]) retWrap1.get("oData"); + System.out.println("原数据: " + retWrap1.get("oData")); + + // 验签 + RetWrap verify = api.attachedVerifyAndGetAll(oData); + if (verify != null) { + System.out.println("x509公钥: " + Util.bytes2HexString(((X509) verify.get("x509")).getPubKey())); + System.out.println("签名原数据: " + Util.bytes2HexString((byte[]) (verify.get("oData")))); + } + + } + + // + // 测试通过 + // 73 + @Test + public void testSM2ConfirmSessionKeyC() { + + String userId = "8012"; + byte[] data = "12343242".getBytes(); + + // 通过用户id获取公钥 + String pk = api.SM2GetPublicKeyC(userId, SydApi.FOR_ENCRYPT); + + // 生成数字信封 + RetWrap retWrap = api.SM2GenSessionKey(pk); + printRetWrap(retWrap); + String cipherKey = (String) retWrap.get(SydApi.RET_CIPHER_KEY); + String sessionKey = (String) retWrap.get(SydApi.RET_SESSION_KEY); + String kcv = (String) retWrap.get(SydApi.RET_KCV); + + // 使用生成的对称密钥加密数据 + byte[] dataEnc = api.SYMEncryptData(SydApi.NALG_SM4, sessionKey, data); + + // 解密数字信封 + RetWrap retWrap1 = api.SM2ConfirmSessionKeyC(userId, cipherKey); + printRetWrap(retWrap1); + sessionKey = (String) retWrap1.get(SydApi.RET_SESSION_KEY); + kcv = (String) retWrap1.get(SydApi.RET_KCV); + + // 使用解密出来的对称密钥解密数据 + byte[] bytes1 = api.SYMDecryptData(SydApi.NALG_SM4, sessionKey, dataEnc); + System.out.println("解密后的数据:" + new String(bytes1)); + } + + + @Test + public void testDetachedSignAndVerify1() throws UnsupportedEncodingException { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + String orgData = "111111213323143243243"; + + + String sign = api.attachedSign(orgData.getBytes(), dn); + System.out.println("签名结果: " + sign); + System.out.println("签名结果 hex : " + Util.bytes2HexString(Util.decodeBase64(sign))); + + boolean verify = api.attachedVerify(sign); + System.out.println("验签结果: " + verify); + Assert.assertTrue(verify); + } + + + @Test + public void testDetachedSignAndVerify2() throws UnsupportedEncodingException { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + String orgData = "111111213323143243243"; + + + String sign = api.attachedSignSoft(orgData.getBytes(), dn); + System.out.println("签名结果: " + sign); + System.out.println("签名结果 hex: " + Util.bytes2HexString(Util.decodeBase64(sign))); + + boolean verify = api.attachedVerify(sign); + System.out.println("验签结果: " + verify); + Assert.assertTrue(verify); + } + + + @Test + public void testDetachedSignAndVerify3() throws UnsupportedEncodingException { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] orgData = new byte[3 * 1024]; + + String sign = api.attachedSign(orgData, dn); + System.out.println("签名结果: " + sign); + System.out.println("签名结果 hex: " + Util.bytes2HexString(Util.decodeBase64(sign))); + + boolean verify = api.attachedVerify(sign); + System.out.println("验签结果: " + verify); + Assert.assertTrue(verify); + } + + @Test + public void testDetachedSignAndVerify8() throws UnsupportedEncodingException { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] orgData = new byte[8 * 1024]; + + String sign = api.attachedSign(orgData, dn); + System.out.println("签名结果: " + sign); + System.out.println("签名结果 hex: " + Util.bytes2HexString(Util.decodeBase64(sign))); + + boolean verify = api.attachedVerify(sign); + System.out.println("验签结果: " + verify); + Assert.assertTrue(verify); + } + + // 测试通过 + // 8018 + @Test + public void testAttachedSignAndVerify() { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + String orgData = "111111213323143243243"; + String sign = api.attachedSign(orgData.getBytes(), dn); + System.out.println("签名结果: " + sign); + System.out.println("签名结果 hex : " + Util.bytes2HexString(Util.decodeBase64(sign))); + + boolean verify = api.attachedVerify(sign); + System.out.println("验签结果: " + verify); + } + + @Test + public void testAttachedSignAndVerifyGetX509() { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] orgData = "111111213323143243243".getBytes(); + String sign = api.attachedSign(orgData, dn); + System.out.println("签名结果: " + sign); + + + X509 verify = api.attachedVerifyAndGetX509(sign); + System.out.println("验签结果: " + Util.bytes2HexString(verify.getPubKey())); + } + + @Test + public void testAttachedSignAndVerifyRD() { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + String orgData = "111111213323143243243"; + String sign = api.attachedSign(orgData.getBytes(), dn); + System.out.println("签名结果: " + sign); + + byte[] verify = api.attachedVerifyRD(sign); + System.out.println("验签结果: " + Util.bytes2HexString(verify)); + } + + @Test + public void testAttachedSignAndVerifyGetAll() { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] orgData = "111111213323143243243".getBytes(); + String sign = api.attachedSign(orgData, dn); + System.out.println("签名结果: " + sign); + + RetWrap verify = api.attachedVerifyAndGetAll(sign); + if (verify != null) { + System.out.println("x509公钥: " + Util.bytes2HexString(((X509) verify.get("x509")).getPubKey())); + System.out.println("签名原数据: " + verify.get("oData")); + } + } + + @Test + public void testAttachedSignAndVerifyGetAll8k() { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] orgData = new byte[ 8 * 1024 ]; + String sign = api.attachedSign(orgData, dn); + System.out.println("签名结果: " + sign); + + RetWrap verify = api.attachedVerifyAndGetAll(sign); + if (verify != null) { + System.out.println("x509公钥: " + Util.bytes2HexString(((X509) verify.get("x509")).getPubKey())); + System.out.println("签名原数据: " + verify.get("oData")); + } + } + + + @Test + public void testAttachedSignAndVerifyGetX509C() { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] orgData = "111111213323143243243".getBytes(); + byte[] sign = api.attachedSignC(orgData, dn); + System.out.println("签名结果: " + sign); + + X509 verify = api.attachedVerifyAndGetX509(sign); + System.out.println("验签结果: " + Util.bytes2HexString(verify.getPubKey())); + } + + @Test + public void testAttachedSignAndVerifyRDC() { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + String orgData = "111111213323143243243"; + byte[] sign = api.attachedSignC(orgData.getBytes(), dn); + System.out.println("签名结果: " + sign); + + byte[] verify = api.attachedVerifyRD(sign); + System.out.println("验签结果: " + Util.bytes2HexString(verify)); + } + + @Test + public void testAttachedSignAndVerifyGetAllC() { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] orgData = "111111213323143243243".getBytes(); + byte[] sign = api.attachedSignC(orgData, dn); + System.out.println("签名结果: " + sign); + + + RetWrap verify = api.attachedVerifyAndGetAll(sign); + if (verify != null) { + System.out.println("x509公钥: " + Util.bytes2HexString(((X509) verify.get("x509")).getPubKey())); + System.out.println("签名原数据: " + verify.get("oData")); + } + } + + @Test + public void testDecryptDE54k12() throws UnsupportedEncodingException { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] oData = new byte[4 * 1024]; + + RetWrap retWrap = api.generateDEByDnHsm(dn, oData); + byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY); + byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY); + printRetWrap(retWrap); + + RetWrap retWrap1 = api.decryptDEByDNSoft(dn, new String(CipherKey)); + printRetWrap(retWrap1); + byte[] deData = (byte[]) retWrap1.get("oData"); + Assert.assertArrayEquals(deData, oData); + } + + @Test + public void testDecryptDE54k21() throws UnsupportedEncodingException { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] oData = new byte[4 * 1024]; + + RetWrap retWrap = api.generateDEByDnSoft(dn, oData); + byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY); + byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY); + printRetWrap(retWrap); + + RetWrap retWrap1 = api.decryptDEByDNHsm(dn, new String(CipherKey)); + printRetWrap(retWrap1); + byte[] deData = (byte[]) retWrap1.get("oData"); + Assert.assertArrayEquals(deData, oData); + } + + + @Test + public void testDecryptDE54k() throws UnsupportedEncodingException { + + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] oData = new byte[4 * 1024]; + + RetWrap retWrap = api.generateDEByDn(dn, oData); + byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY); + byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY); + printRetWrap(retWrap); + + RetWrap retWrap1 = api.decryptDEByDN(dn, CipherKey); + printRetWrap(retWrap1); + + } + + + @Test + public void testDecryptDE54kr() throws UnsupportedEncodingException { + String dn = "CN=YCCA@黄金交易所@Zhuangj@1,OU=Individual-2,OU=YCCA,O=CFCA OCA1,C=CN"; + byte[] oData = new byte[4 * 1024]; + + RetWrap retWrap = api.generateDEByDn(dn, oData); + byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY); + byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY); + printRetWrap(retWrap); + + RetWrap retWrap1 = api.decryptDEByDN(dn, CipherKey); + printRetWrap(retWrap1); + } + + + @Test + public void testDecryptDE520M() throws UnsupportedEncodingException { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] oData = new byte[20 * 1024 * 1024]; + + RetWrap retWrap = api.generateDEByDn(dn, oData); + byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY); + byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY); + printRetWrap(retWrap); + + RetWrap retWrap1 = api.decryptDEByDN(dn, CipherKey); + printRetWrap(retWrap1); + } + + @Test + public void testDecryptDE58k() throws UnsupportedEncodingException { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] oData = new byte[8 * 1024]; + + RetWrap retWrap = api.generateDEByDn(dn, oData); + byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY); + byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY); + printRetWrap(retWrap); + + RetWrap retWrap1 = api.decryptDEByDN(dn, CipherKey); + printRetWrap(retWrap1); + } + + @Test + public void testDecryptDE5() throws UnsupportedEncodingException { + String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1"; + byte[] oData = "00001111222233".getBytes(); + + RetWrap retWrap = api.generateDEByDn(dn, oData); + byte[] CipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY); + byte[] sessionKey = (byte[]) retWrap.get(SydApi.RET_SESSION_KEY); + printRetWrap(retWrap); + + RetWrap retWrap1 = api.decryptDEByDN(dn, CipherKey); + printRetWrap(retWrap1); + } + + + // 测试通过 + // 76 + @Test + public void testSM3Hash() { + String orgData = "FFFFFFFFFFFFFFFF"; + + byte[] hash = api.SM3Hash(orgData.getBytes()); + + System.out.println("hash:" + Util.bytes2HexString(hash)); + } + + + // =============================工具方法============================================== + + public static void printRetWrap(RetWrap rw) { + printRetWrap(rw, false); + } + + /** + * 输出 + * + * @param rw + */ + public static void printRetWrap(RetWrap rw, boolean flag) { + for (String s : rw.keySet()) { + + Object o = rw.get(s); + if (o instanceof String) { + System.out.println(s + ":" + o); + } else if (o instanceof byte[]) { + if (flag) { + System.out.println(s + ": " + Util.bytes2HexString((byte[]) o)); + } else { + System.out.println(s + ": " + new String((byte[]) o)); + } + } else { + System.out.println(s + ":" + o); + } + } + } +}