From 882e84320ca17461e91393e97ad0f71a6410b97a Mon Sep 17 00:00:00 2001 From: cheney Date: Mon, 1 Jun 2026 10:02:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=8D=8F=E8=AE=AE=EF=BC=8Cin?= =?UTF-8?q?dex=20=E6=94=B9=E4=B8=BA=204=20=E5=AD=97=E8=8A=82=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/racal/sunyard/main/SydApi4j.java | 2580 ++++++++--------- src/test/java/cmbpoc/FunctionTest.java | 28 +- 2 files changed, 1179 insertions(+), 1429 deletions(-) diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index e1cd401..1ae4b6d 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -52,7 +52,6 @@ import java.net.SocketAddress; import java.net.SocketTimeoutException; import java.nio.ByteBuffer; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.security.NoSuchProviderException; import java.security.Security; import java.security.cert.CertificateException; @@ -61,7 +60,10 @@ import java.security.cert.X509Certificate; import java.text.SimpleDateFormat; import java.time.Duration; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -79,11 +81,9 @@ public class SydApi4j implements SydApi { private static boolean debug = false; -// private static long sTime = System.currentTimeMillis(); + private static long sTime = System.currentTimeMillis(); - /** - * --------------------------主备切换字段-------------------------------------------- - */ + /** --------------------------主备切换字段--------------------------------------------*/ private static String ha_ip1; private static String ha_ip2; private static int ha_port; @@ -92,18 +92,16 @@ public class SydApi4j implements SydApi { private static final AtomicBoolean usingBackup = new AtomicBoolean(false); //连接数计数器 -// private static final AtomicInteger connectionCounter = new AtomicInteger(0); + private static final AtomicInteger connectionCounter = new AtomicInteger(0); private final Object reqLock = new Object(); - /** - * --------------------------主备切换字段-------------------------------------------- - */ + /** --------------------------主备切换字段--------------------------------------------*/ // 网络失败时重试次数 private String ip; private int port; - // private String pwd; + private String pwd; private int connectTimeout; private int readTimeout; private int retryWhenNetError = 0; @@ -135,11 +133,12 @@ public class SydApi4j implements SydApi { /** * 简单双机热备模式 * - * @param ip1:主机IP地址 - * @param ip2:备机IP地址 - * @param port:端口号 - * @param timeout:超时时间(毫秒) todo 目前主备切换只适配了安徽农信接口 - * @return:成功返回0,失败返回错误码 + * @param ip1 + * @param ip2 + * @param port + * @param timeout + * todo 目前主备切换只适配了安徽农信接口 + * @return */ public static int SYD_Set_Parameter_HA( String ip1, @@ -172,8 +171,8 @@ public class SydApi4j implements SydApi { ProtoQN proto = new ProtoQN(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -198,11 +197,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; - -// ret = (Map) ret.get("data"); + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -228,8 +223,8 @@ public class SydApi4j implements SydApi { sk = SYMUtil.genPrivateKeyMark(userId, nKeyType == 0 ? "000" : "001", "00000001"); } - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -250,10 +245,7 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((be[0] ^ be[1]) != 0) { @@ -276,8 +268,8 @@ public class SydApi4j implements SydApi { sk = SYMUtil.genPrivateKeyMark(userId, nKeyType == 0 ? "000" : "001", "00000001"); } - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -298,10 +290,7 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((be[0] ^ be[1]) != 0) { @@ -312,7 +301,7 @@ public class SydApi4j implements SydApi { String skv = ((PacketSection) ret.get("sk")).getString(); RetWrap r = new RetWrap(); // r.put("publicKey", "03420004" + pkv); - r.put("publicKey", Util.bytes2HexString(pkv)); + r.put("publicKey", Util.bytes2HexString(pkv)); r.put("privateKey", skv); return r; } @@ -347,8 +336,8 @@ public class SydApi4j implements SydApi { Proto74 proto = new Proto74(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -403,8 +392,8 @@ public class SydApi4j implements SydApi { Proto74 proto = new Proto74(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -443,8 +432,8 @@ public class SydApi4j implements SydApi { @Override public byte[] transPinFrOneKeyToAno(String srcKey, String desKey, int sca, String sPin, String primaryAccount) { ProtoCB proto = new ProtoCB(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -469,22 +458,20 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); } else { - return ((PacketSection) ret.get("dPin")).getBytes(); + byte[] pinBlock = ((PacketSection) ret.get("dPin")).getBytes(); + return pinBlock; } } } @@ -521,8 +508,8 @@ public class SydApi4j implements SydApi { byte[] sign = SYMUtil.base64SignAsDerSign(base64Sign); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -543,51 +530,59 @@ public class SydApi4j implements SydApi { byte[] v = new byte[2]; bb.get(v, 0, v.length); String a = Util.bytes2HexString(v); - return "3030".equals(a); - } - - public byte[] SYD_NakedSignAll(int rFlag, byte[] orgData, String sCertDN) { - try { - String cert = getCertByDn(sCertDN, 0); - boolean sm2Cert = CertUtil.isSM2Cert(cert); - if (!sm2Cert) {//RSA - return SYD_NakedSign(rFlag, orgData, sCertDN); - } else { - return SYD_NakedSign(orgData, sCertDN); - } - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - - } - - public boolean SYD_NakedVerifyAll(int rFlag, byte[] orgData, byte[] sign, String sCertDN) { - try { - String cert = getCertByDn(sCertDN, 0); - boolean sm2Cert = CertUtil.isSM2Cert(cert); - if (!sm2Cert) {//RSA - return SYD_NakedVerify(rFlag, orgData, sign, sCertDN); - } else { - return SYD_NakedVerify(orgData, sign, sCertDN); - } - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - - } - - public boolean SYD_NakedVerifyAll4NX(String cert, int rFlag, byte[] orgData, byte[] sign, String sCertDN) { - boolean verify = false; - //先导入证书 - importCert(cert.getBytes(), null); - if (rFlag == 4) {//SM2 国密算法 - verify = SYD_NakedVerify(orgData, sign, sCertDN); + if ("3030".equals(a)) { + return true; } else { - verify = SYD_NakedVerify(rFlag, orgData, sign, sCertDN); + return false; + } + } + + public byte[] SYD_NakedSignAll(int rFlag,byte[] orgData, String sCertDN){ + try { + String cert = getCertByDn(sCertDN,0); + boolean sm2Cert = CertUtil.isSM2Cert(cert); + if(!sm2Cert){//RSA + return SYD_NakedSign(rFlag,orgData,sCertDN); + }else { + return SYD_NakedSign(orgData,sCertDN); + } + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + + } + + public boolean SYD_NakedVerifyAll(int rFlag,byte[] orgData,byte[] sign, String sCertDN){ + try { + String cert = getCertByDn(sCertDN,0); + boolean sm2Cert = CertUtil.isSM2Cert(cert); + if(!sm2Cert){//RSA + return SYD_NakedVerify(rFlag,orgData,sign,sCertDN); + }else { + return SYD_NakedVerify(orgData,sign,sCertDN); + } + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + + } + + public boolean SYD_NakedVerifyAll4NX(String cert,int rFlag,byte[] orgData,byte[] sign, String sCertDN){ + boolean verify = false; + try { + //先导入证书 + importCert(cert.getBytes(),null); + if( rFlag == 4 ){//SM2 国密算法 + verify = SYD_NakedVerify(orgData,sign,sCertDN); + }else { + verify = SYD_NakedVerify(rFlag,orgData,sign,sCertDN); + } + //删除导入的证书 + deleteCert(sCertDN); + return verify; + } catch ( SydApiException e) { + throw e; } - //删除导入的证书 - deleteCert(sCertDN); - return verify; } @@ -596,88 +591,89 @@ public class SydApi4j implements SydApi { if (orgData == null) { throw new IllegalArgumentException("输入数据为 null"); } else { - return orgData.length < 4096 ? this.SYD_NakedSign(sCertDN, orgData, null) : this.SYD_NakedSign_OuterHash(orgData, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedSign(sCertDN, orgData, (byte[])null) : this.SYD_NakedSign_OuterHash(orgData, sCertDN); } } - public void QRCodeSign(String orgData, String sCertDN, String filepath) { - byte[] sign; + public void QRCodeSign(String orgData, String sCertDN,String filepath) { + byte[] sign = null; if (orgData == null) { throw new IllegalArgumentException("输入数据为 null"); } else { - sign = orgData.length() < 4096 ? this.SYD_NakedSign(sCertDN, orgData.getBytes(), null) : this.SYD_NakedSign_OuterHash(orgData.getBytes(), sCertDN); + sign = orgData.length() < 4096 ? this.SYD_NakedSign(sCertDN, orgData.getBytes(), (byte[])null) : this.SYD_NakedSign_OuterHash(orgData.getBytes(), sCertDN); } - QRCodeUtil.genQRCode(orgData + "+" + Util.bytes2HexString(sign), filepath); + QRCodeUtil.genQRCode(orgData+"+"+Util.bytes2HexString(sign),filepath); } - public void QRCodeSignRSA(int rFlag, String orgData, String sCertDN, String filepath) { - byte[] sign; + public void QRCodeSignRSA(int rFlag, String orgData, String sCertDN,String filepath) { + byte[] sign = null; if (orgData == null) { throw new IllegalArgumentException("输入数据为 null"); } else { - sign = orgData.length() < 4096 ? this.SYD_NakedSign(rFlag, sCertDN, orgData.getBytes(), null) : this.SYD_NakedSign_OuterHash(rFlag, orgData.getBytes(), sCertDN); + sign = orgData.length() < 4096 ? this.SYD_NakedSign(rFlag,sCertDN, orgData.getBytes(), (byte[])null) : this.SYD_NakedSign_OuterHash(rFlag,orgData.getBytes(), sCertDN); } - QRCodeUtil.genQRCode(orgData + "+" + Util.bytes2HexString(sign), filepath); + QRCodeUtil.genQRCode(orgData+"+"+Util.bytes2HexString(sign),filepath); } - public boolean QRCodeVer(String sCertDN, String filepath) { + public boolean QRCodeVer( String sCertDN,String filepath) { String s = QRCodeUtil.readQRCode(filepath); String[] split = s.split("\\+"); byte[] orgData = split[0].getBytes(); byte[] sign = Util.hexString2Bytes(split[1]); - return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN); } - public boolean QRCodeVerRSA(int rFlag, String sCertDN, String filepath) { + public boolean QRCodeVerRSA( int rFlag,String sCertDN,String filepath) { String s = QRCodeUtil.readQRCode(filepath); String[] split = s.split("\\+"); byte[] orgData = split[0].getBytes(); byte[] sign = Util.hexString2Bytes(split[1]); - return orgData.length < 4096 ? this.SYD_NakedVerify(rFlag, sCertDN, sign, orgData, null) : this.SYD_NakedVerify_OuterHash(rFlag, orgData, sign, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedVerify(rFlag,sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash(rFlag,orgData, sign, sCertDN); } - public void BarCodeSign(String orgData, String sCertDN, String filepath) { - byte[] sign; + + public void BarCodeSign(String orgData, String sCertDN,String filepath) { + byte[] sign = null; if (orgData == null) { throw new IllegalArgumentException("输入数据为 null"); } else { - sign = orgData.length() < 4096 ? this.SYD_NakedSign(sCertDN, orgData.getBytes(), null) : this.SYD_NakedSign_OuterHash(orgData.getBytes(), sCertDN); + sign = orgData.length() < 4096 ? this.SYD_NakedSign(sCertDN, orgData.getBytes(), (byte[])null) : this.SYD_NakedSign_OuterHash(orgData.getBytes(), sCertDN); } - QRCodeUtil.genBarCode(orgData + "+" + Util.bytes2HexString(sign), filepath); + QRCodeUtil.genBarCode(orgData+"+"+Util.bytes2HexString(sign),filepath); } - public void BarCodeSignRSA(int rFlag, String orgData, String sCertDN, String filepath) { - byte[] sign; + public void BarCodeSignRSA(int rFlag, String orgData, String sCertDN,String filepath) { + byte[] sign = null; if (orgData == null) { throw new IllegalArgumentException("输入数据为 null"); } else { - sign = orgData.length() < 4096 ? this.SYD_NakedSign(rFlag, sCertDN, orgData.getBytes(), null) : this.SYD_NakedSign_OuterHash(rFlag, orgData.getBytes(), sCertDN); + sign = orgData.length() < 4096 ? this.SYD_NakedSign(rFlag,sCertDN, orgData.getBytes(), (byte[])null) : this.SYD_NakedSign_OuterHash(rFlag,orgData.getBytes(), sCertDN); } - QRCodeUtil.genBarCode(orgData + "+" + Util.bytes2HexString(sign), filepath); + QRCodeUtil.genBarCode(orgData+"+"+Util.bytes2HexString(sign),filepath); } - public boolean BarCodeVer(String sCertDN, String filepath) { + public boolean BarCodeVer( String sCertDN,String filepath) { String s = QRCodeUtil.readBarCode(filepath); String[] split = s.split("\\+"); byte[] orgData = split[0].getBytes(); byte[] sign = Util.hexString2Bytes(split[1]); - return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN); } - public boolean BarCodeVerRSA(int rFlag, String sCertDN, String filepath) { + public boolean BarCodeVerRSA( int rFlag,String sCertDN,String filepath) { String s = QRCodeUtil.readBarCode(filepath); String[] split = s.split("\\+"); byte[] orgData = split[0].getBytes(); byte[] sign = Util.hexString2Bytes(split[1]); - return orgData.length < 4096 ? this.SYD_NakedVerify(rFlag, sCertDN, sign, orgData, null) : this.SYD_NakedVerify_OuterHash(rFlag, orgData, sign, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedVerify(rFlag,sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash(rFlag,orgData, sign, sCertDN); } public byte[] SYD_NakedSign(String sCertDN, byte[] orgData, byte[] hash) { @@ -688,25 +684,16 @@ public class SydApi4j implements SydApi { byte[] dn = new byte[256]; byte[] s = null; -// try { -// // sCertDN = DnUtil.verifyDn(sCertDN); -// s = sCertDN.getBytes("GBK"); -// } catch (UnsupportedEncodingException var16) { -// var16.printStackTrace(); -// } try { + // sCertDN = DnUtil.verifyDn(sCertDN); s = sCertDN.getBytes("GBK"); } catch (UnsupportedEncodingException var16) { var16.printStackTrace(); } - if (s != null) { - System.arraycopy(s, 0, dn, 0, s.length); // ✅ 只有这一个安全的调用 - } - - System.arraycopy(s != null ? s : new byte[0], 0, dn, 0, s != null ? s.length : 0); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + System.arraycopy(s, 0, dn, 0, s.length); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); packet.put("header", sn.getSn().array()); data.put("signatureDataType$option", 1); @@ -725,45 +712,44 @@ public class SydApi4j implements SydApi { Packet p = proto.parse(bb); Map ret = p.toMap(); - if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { + if (!Arrays.equals(sn.getSn().array(), ((PacketSection)ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); - } else { -// ret = (Map)ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; - PacketSection error = (PacketSection) ret.get("error"); + } + else + { + ret = (Map)ret.get("data"); + PacketSection error = (PacketSection)ret.get("error"); byte[] be = error.getBytes(); if ((be[0] ^ be[1]) != 0) { throw new SydApiException(error.getInfo(), error.autoInt()); } else { - return ((PacketSection) ret.get("signature")).getBytes(); + return ((PacketSection)ret.get("signature")).getBytes(); } } } private ByteBuffer sendAndGetResponse(ByteBuffer data) { - ByteBuffer readData; + ByteBuffer readData = null; int maxRetries = 2; int retryCount = 0; while (retryCount < maxRetries) { try { - synchronized (reqLock) { + synchronized(reqLock) { byte[] sentData = this.syncSend(data); readData = this.syncRead(sentData); return readData; } } catch (SydApiException e) { handleNetworkFailure(e, retryCount, maxRetries); // 统一处理网络故障 - retryCount++; + retryCount ++; } } throw new SydApiException("加密机连接失败,主备节点均不可用", -1); } public byte[] SYD_NakedSign_OuterHash(byte[] orgData, String sCertDN) { - // sCertDN = DnUtil.verifyDn(sCertDN); - String pk; + // sCertDN = DnUtil.verifyDn(sCertDN); + String pk = null; try { pk = this.SYD_SM2_Get_Dn_PublicKey_HA(sCertDN.getBytes("GBK"), 0); @@ -773,12 +759,12 @@ public class SydApi4j implements SydApi { } byte[] hash = this.SM3Hash(pk, orgData); - return this.SYD_NakedSign(sCertDN, null, hash); + return this.SYD_NakedSign(sCertDN, (byte[])null, hash); } - public byte[] SYD_NakedSign_OuterHash(int rFlag, byte[] orgData, String sCertDN) { + public byte[] SYD_NakedSign_OuterHash(int rFlag,byte[] orgData, String sCertDN) { byte[] hash = this.itHash(rFlag, orgData);// HASH类型。0: SHA1,1: SHA256,2: MD5 - return this.SYD_NakedSign(rFlag, sCertDN, null, hash); + return this.SYD_NakedSign(rFlag,sCertDN, (byte[])null, hash); } @@ -786,15 +772,15 @@ public class SydApi4j implements SydApi { if (orgData == null) { throw new IllegalArgumentException("输入数据为 null"); } else { - return orgData.length < 4096 ? this.SYD_NakedSign(rFlag, sCertDN, orgData, null) : this.SYD_NakedSign_OuterHash(rFlag, orgData, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedSign(rFlag,sCertDN, orgData, (byte[])null) : this.SYD_NakedSign_OuterHash(rFlag,orgData, sCertDN); } } - public byte[] SYD_NakedSign(int rFlag, String sCertDN, byte[] orgData, byte[] hash) { + public byte[] SYD_NakedSign(int rFlag, String sCertDN,byte[] orgData,byte[] hash) { Proto8003 proto = new Proto8003(); PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); byte[] dn = new byte[256]; byte[] s = null; @@ -804,26 +790,27 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } - assert s != null; System.arraycopy(s, 0, dn, 0, s.length); // 填充数据 - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); packet.put("header", sn.getSn().array()); if (rFlag == 1) { // MD5 data.put("rFlag", 'M'); - } else if (rFlag == 2) { + } + else if (rFlag == 2) { // SHA-1 data.put("rFlag", 'S'); - } else if (rFlag == 3) { + } + else if (rFlag == 3) { // SHA-256 data.put("rFlag", 'R'); - } else { - throw new SydApiException("不支持的摘要算法", 10001); + }else { + throw new SydApiException("不支持的摘要算法",10001); } data.put("signatureDataType$option", 1); data.put("DN", dn); @@ -845,10 +832,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -860,13 +844,13 @@ public class SydApi4j implements SydApi { } public boolean SYD_NakedVerify(byte[] orgData, byte[] sign, String sCertDN) { - return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN); } public boolean SYD_NakedVerify(String sCertDN, byte[] sign, byte[] orgData, byte[] hash) { Proto8004 proto = new Proto8004(); PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); sign = ASN1Util.alignSignDer(sign); byte[] dn = new byte[256]; @@ -879,9 +863,8 @@ public class SydApi4j implements SydApi { var18.printStackTrace(); } - assert s != null; System.arraycopy(s, 0, dn, 0, s.length); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); packet.put("header", sn.getSn().array()); @@ -893,7 +876,7 @@ public class SydApi4j implements SydApi { data.put("signature", sign); data.put("returnSign$option", 0); ByteBuffer bb = proto.rend(packet); - + // synchronized(this) { // byte[] b = this.syncSend(bb); // bb = this.syncRead(b); @@ -906,15 +889,12 @@ public class SydApi4j implements SydApi { // throw new SydApiException("包序号不匹配", -4); // } else { -// ret = (Map)ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; - PacketSection error = (PacketSection) ret.get("error"); + ret = (Map)ret.get("data"); + PacketSection error = (PacketSection)ret.get("error"); byte[] be = error.getBytes(); if ((be[0] ^ be[1]) != 0) { SydApiException e = new SydApiException(error.getInfo(), error.autoInt()); - println("验签失败=" + e); + println("验签失败=" + e.toString()); log.info("验签失败:{}", e.toString()); return false; } else { @@ -924,8 +904,8 @@ public class SydApi4j implements SydApi { } public boolean SYD_NakedVerify_OuterHash(byte[] orgData, byte[] sign, String sCertDN) { - // sCertDN = DnUtil.verifyDn(sCertDN); - String pk; + // sCertDN = DnUtil.verifyDn(sCertDN); + String pk = null; try { pk = this.SYD_SM2_Get_Dn_PublicKey_HA(sCertDN.getBytes("GBK"), 0); @@ -938,20 +918,20 @@ public class SydApi4j implements SydApi { return this.SYD_NakedVerify(sCertDN, sign, orgData, hash); } - public boolean SYD_NakedVerify_OuterHash(int rFlag, byte[] orgData, byte[] sign, String sCertDN) { + public boolean SYD_NakedVerify_OuterHash(int rFlag,byte[] orgData, byte[] sign, String sCertDN) { byte[] hash = this.itHash(rFlag, orgData); - return this.SYD_NakedVerify(rFlag, sCertDN, sign, orgData, hash); + return this.SYD_NakedVerify(rFlag,sCertDN, sign, orgData, hash); } - public boolean SYD_NakedVerify(int rFlag, byte[] orgData, byte[] sign, String sCertDN) { - return orgData.length < 4096 ? this.SYD_NakedVerify(rFlag, sCertDN, sign, orgData, null) : this.SYD_NakedVerify_OuterHash(rFlag, orgData, sign, sCertDN); + public boolean SYD_NakedVerify(int rFlag,byte[] orgData, byte[] sign, String sCertDN) { + return orgData.length < 4096 ? this.SYD_NakedVerify( rFlag,sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash( rFlag,orgData, sign, sCertDN); } - public boolean SYD_NakedVerify(int rFlag, String sCertDN, byte[] sign, byte[] orgData, byte[] hashData) { + public boolean SYD_NakedVerify(int rFlag, String sCertDN,byte[] sign, byte[] orgData, byte[] hashData) { Proto8004 proto = new Proto8004(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); PacketSN sn = PacketSN.gen(); byte[] dn = new byte[256]; @@ -963,11 +943,10 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } - assert s != null; System.arraycopy(s, 0, dn, 0, s.length); // 填充数据 - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); packet.put("header", sn.getSn().array()); @@ -1005,10 +984,7 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -1023,7 +999,7 @@ public class SydApi4j implements SydApi { public String sm3AndDetachedSign(byte[] orgData, String publicKey, String sCertDN) { byte[] hash = SM3Hash(publicKey, orgData); - System.out.println("hash为:" + Util.bytes2HexString(hash)); + System.out.println("hash为:"+Util.bytes2HexString(hash)); return detachedSign(DATA_HASH, hash, sCertDN); } @@ -1033,8 +1009,8 @@ public class SydApi4j implements SydApi { public RetWrap genComposeKey(String[] keys) { ProtoGY proto = new ProtoGY(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); int len = 0; for (String key : keys) { @@ -1069,16 +1045,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -1103,8 +1076,8 @@ public class SydApi4j implements SydApi { public RetWrap genComposeKey(String keyType, char keySolution, String keyClearPattern) { ProtoAA proto = new ProtoAA(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -1120,16 +1093,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -1151,7 +1121,7 @@ public class SydApi4j implements SydApi { @Override public boolean sm3AndDetachedVerify(byte[] orgData, String publicKey, String sign) { byte[] hash = SM3Hash(publicKey, orgData); - System.out.println("验签hash为:" + Util.bytes2HexString(hash)); + System.out.println("验签hash为:"+Util.bytes2HexString(hash)); return detachedVerify(DATA_HASH, hash, sign); } @@ -1172,24 +1142,23 @@ public class SydApi4j implements SydApi { @Override public String detachedSign(int dataType, byte[] orgData, String sCertDN) { Proto8005 proto = new Proto8005(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); PacketSN sn = PacketSN.gen(); byte[] dn = new byte[256]; byte[] s = null; try { - // sCertDN = verifyDn(sCertDN); + // sCertDN = verifyDn(sCertDN); s = sCertDN.getBytes("GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } - assert s != null; System.arraycopy(s, 0, dn, 0, s.length); // 填充数据 - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); packet.put("header", sn.getSn().array()); @@ -1218,10 +1187,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -1230,7 +1196,8 @@ public class SydApi4j implements SydApi { throw new SydApiException(error.getInfo(), error.autoInt()); } - return ((PacketSection) ret.get("signature")).getString(); + String signature = ((PacketSection) ret.get("signature")).getString(); + return signature; } @@ -1238,11 +1205,11 @@ public class SydApi4j implements SydApi { @Override public boolean detachedVerify(int dataType, byte[] orgData, String sign) { Proto8006 proto = new Proto8006(); - // 填充数据 - HashMap packet = new HashMap<>(); + // 填充数据 + HashMap packet = new HashMap(); PacketSN sn = PacketSN.gen(); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); packet.put("header", sn.getSn().array()); @@ -1275,51 +1242,54 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { SydApiException e = new SydApiException(error.getInfo(), error.autoInt()); - println("验签失败=" + e); + println("验签失败=" + e.toString()); return false; } return true; } - public boolean detachedVerifyAll(int rFlag, byte[] orgData, String sign) { + public boolean detachedVerifyAll(int rFlag,byte[] orgData, String sign){ try { boolean sm2Cert = CertUtil.isSignedBySM2Cert(sign); - if (!sm2Cert) {//RSA - return detachedVerifyRSA(rFlag, orgData, sign); - } else { - if (orgData.length < 4096) { - return detachedVerify(DATA_ORIGIN, orgData, sign); - } else { + if(!sm2Cert){//RSA + return detachedVerifyRSA(rFlag,orgData,sign); + }else { + if( orgData.length<4096 ){ + return detachedVerify(DATA_ORIGIN,orgData,sign); + }else { RetWrap retWrap = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); - byte[] pk = (byte[]) retWrap.get("pk"); + byte[] pk =(byte[]) retWrap.get("pk"); byte[] hash = this.SM3Hash(Util.bytes2HexString(pk), orgData); - return detachedVerify(DATA_HASH, hash, sign); + return detachedVerify(DATA_HASH,hash,sign); } } - } catch (CertificateException | IOException | NoSuchProviderException e) { + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } catch (CertificateException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (NoSuchProviderException e) { throw new RuntimeException(e); } } - public boolean detachedVerify(byte[] orgData, String sign) { + public boolean detachedVerify(byte[] orgData, String sign){ try { - if (orgData.length < 4096) { - return detachedVerify(DATA_ORIGIN, orgData, sign); - } else { + if( orgData.length<4096 ){ + return detachedVerify(DATA_ORIGIN,orgData,sign); + }else { RetWrap retWrap = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); - byte[] pk = (byte[]) retWrap.get("pk"); + byte[] pk =(byte[]) retWrap.get("pk"); byte[] hash = this.SM3Hash(Util.bytes2HexString(pk), orgData); - return detachedVerify(DATA_HASH, hash, sign); + return detachedVerify(DATA_HASH,hash,sign); } } catch (IOException e) { throw new RuntimeException(e); @@ -1327,8 +1297,8 @@ public class SydApi4j implements SydApi { } - public String getCertBySign(String sign) { - RetWrap retWrap; + public String getCertBySign(String sign){ + RetWrap retWrap = null; try { retWrap = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); } catch (IOException e) { @@ -1336,9 +1306,9 @@ public class SydApi4j implements SydApi { } String cert = (String) retWrap.get("cert"); String pemCert; - if (cert.contains("-----BEGIN CERTIFICATE-----")) { - pemCert = cert; - } else { + if(cert.contains("-----BEGIN CERTIFICATE-----")){ + pemCert =cert; + }else { String cert_begin = "-----BEGIN CERTIFICATE-----\n"; String end_cert = "\n-----END CERTIFICATE-----"; pemCert = cert_begin + cert + end_cert; @@ -1347,48 +1317,48 @@ public class SydApi4j implements SydApi { } - public HashMap getCertTime(String dn) { - String cert; + public HashMap getCertTime(String dn){ + String cert = null; try { cert = getCertByDn(dn); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } -// String pemCert; -// if(cert.contains("-----BEGIN CERTIFICATE-----")){ -// pemCert =cert; -// }else { -// String cert_begin = "-----BEGIN CERTIFICATE-----\n"; -// String end_cert = "\n-----END CERTIFICATE-----"; -// pemCert = cert_begin + cert + end_cert; -// } - X509Certificate certificate; + String pemCert; + if(cert.contains("-----BEGIN CERTIFICATE-----")){ + pemCert =cert; + }else { + String cert_begin = "-----BEGIN CERTIFICATE-----\n"; + String end_cert = "\n-----END CERTIFICATE-----"; + pemCert = cert_begin + cert + end_cert; + } + X509Certificate certificate = null; try { certificate = CertUtil.convertToX509Cert(cert); } catch (Exception e) { - throw new SydApiException(0xC101, e); + throw new SydApiException( 0xC101, e ); } SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMdd"); String afterDate = outputFormat.format(certificate.getNotAfter()); String beforeDate = outputFormat.format(certificate.getNotBefore()); - HashMap hashMap = new HashMap<>(); - hashMap.put("after", afterDate); - hashMap.put("before", beforeDate); - return hashMap; + HashMap hashMap = new HashMap(); + hashMap.put("after",afterDate); + hashMap.put("before",beforeDate); + return hashMap; } - public String importCertAndGetDN(String cert) { - RetWrap retWrap = importCert(cert.getBytes(), null); + public String importCertAndGetDN(String cert){ + RetWrap retWrap = importCert(cert.getBytes(),null); String dn = null; - int isImport = (Integer) retWrap.get("isImport"); - if (isImport == 0) { - X509Certificate certificate; + int isImport= (Integer) retWrap.get("isImport"); + if(isImport == 0) { + X509Certificate certificate = null; try { certificate = CertUtil.convertToX509Cert(cert); } catch (Exception e) { - throw new SydApiException(0xC101, e); + throw new SydApiException( 0xC101, e ); } dn = certificate.getSubjectDN().toString(); } @@ -1425,7 +1395,7 @@ public class SydApi4j implements SydApi { } String dn = null; String serialNumber = null; - X509Certificate certificate; + X509Certificate certificate = null; try { certificate = CertUtil.convertToX509Cert(cert); dn = certificate.getSubjectDN().toString(); @@ -1447,8 +1417,7 @@ public class SydApi4j implements SydApi { .map(Object::toString) .collect(Collectors.joining(",")) + "]"; String message = "支付签名证书导入失败!签名服务器地址:" + result + "| 证书对应行号:[" + bkCode + "]"; - Map map = gson.fromJson(snmpConfig, new TypeToken>() { - }.getType()); + Map map = gson.fromJson(snmpConfig, new TypeToken>() {}.getType()); try (SnmpTrapSender sender = new SnmpTrapSender(map)) { sender.sendTrap(message, 2); log.info("snmp发送告警消息:{}", message); @@ -1481,70 +1450,83 @@ public class SydApi4j implements SydApi { String[] addressParts = address.split(":"); String ip = addressParts[0]; int port = Integer.parseInt(addressParts[1]); - String dn; + String dn = null; ReentrantLock ipLock = IpLockUtils.getLockForIp(address); - ipLock.lock(); - SydApi4j sydApi4j = null; try { - //创建sydapi4j连接对象 - sydApi4j = (SydApi4j) new SydApi4j().connect(ip, port, null, timeout); - //先查询 + ipLock.lock(); + SydApi4j sydApi4j = null; + try { + //创建sydapi4j连接对象 + sydApi4j = (SydApi4j) new SydApi4j().connect(ip, port, null, timeout); + //先查询 - //导入证书 - dn = sydApi4j.importCertAndGetDN(cert); - } finally { - if (sydApi4j != null) { - sydApi4j.disconnect(); + //导入证书 + dn = sydApi4j.importCertAndGetDN(cert); + } catch (Exception e) { + throw e; + } finally { + if (sydApi4j != null) { + sydApi4j.disconnect(); + } + ipLock.unlock(); } - ipLock.unlock(); + } catch (Exception e) { + throw e; } return dn; } - public boolean attachedVerifyAll(int rFlag, String sign) { + public boolean attachedVerifyAll(int rFlag, String sign){ try { boolean sm2Cert = CertUtil.isSignedBySM2Cert(sign); - if (!sm2Cert) {//RSA - return attachedVerifyRSA(rFlag, sign); - } else { - RetWrap ret = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); - byte[] orgData = (byte[]) ret.get("orgData"); - String detsign = (String) ret.get("sign"); - if (orgData.length < 4096) { - return attachedVerify(sign); - } else { - byte[] pk = (byte[]) ret.get("pk"); - byte[] hash = this.SM3Hash(Util.bytes2HexString(pk), orgData); - return detachedVerify(DATA_HASH, hash, detsign); + if(!sm2Cert){//RSA + return attachedVerifyRSA(rFlag,sign); + }else { + RetWrap ret = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); + byte[] orgData = (byte[]) ret.get("orgData"); + String detsign = (String) ret.get("sign"); + if( orgData.length<4096 ){ + return attachedVerify(sign); + }else { + byte[] pk =(byte[]) ret.get("pk"); + byte[] hash = this.SM3Hash(Util.bytes2HexString(pk), orgData); + return detachedVerify(DATA_HASH,hash,detsign); } } - } catch (CertificateException | IOException | NoSuchProviderException e) { + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } catch (CertificateException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (NoSuchProviderException e) { throw new RuntimeException(e); } } - public boolean detachedVerifyRSA(int rFlag, byte[] orgData, String sign) { - boolean deVerify = false; - if (orgData.length < 4096) { - deVerify = detachedVerify(rFlag, DATA_ORIGIN, orgData, sign); - } else { + public boolean detachedVerifyRSA(int rFlag,byte[] orgData, String sign) { + boolean deVerify =false; + if(orgData.length<4096){ + deVerify= detachedVerify(rFlag,DATA_ORIGIN,orgData,sign); + }else { //RetWrap hashData = countHash(0, 0, orgData, null); byte[] hashData = itHash(rFlag, orgData); - deVerify = this.detachedVerify(rFlag, DATA_HASH, hashData, sign); + deVerify= this.detachedVerify(rFlag, DATA_HASH, hashData, sign); } return deVerify; } + @Override public boolean detachedVerify(int rFlag, int dataType, byte[] orgData, String sign) { Proto8006 proto = new Proto8006(); // 填充数据 - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); PacketSN sn = PacketSN.gen(); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); if (rFlag == 1) {//RSA证书 @@ -1591,15 +1573,12 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { SydApiException e = new SydApiException(error.getInfo(), error.autoInt()); - println("验签失败=" + e); + println("验签失败=" + e.toString()); return false; } @@ -1612,8 +1591,8 @@ public class SydApi4j implements SydApi { Proto8006 proto = new Proto8006(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -1642,10 +1621,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -1669,8 +1645,8 @@ public class SydApi4j implements SydApi { Proto8025 proto = new Proto8025(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -1700,13 +1676,14 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); - return 0 == (be[0] ^ be[1]); + if ((0 != (be[0] ^ be[1]))) { + + return false; + } + return true; } @Override @@ -1727,22 +1704,22 @@ public class SydApi4j implements SydApi { } - public String detachedSignAll(int rFlag, byte[] orgData, String sCertDN) { + public String detachedSignAll(int rFlag,byte[] orgData, String sCertDN){ try { // String cert = getCertByDn(sCertDN,0); // boolean sm2Cert = CertUtil.isSM2Cert(cert); - if (rFlag < 4) {//RSA - return detachedSignRSA(rFlag, orgData, sCertDN); - } else { - if (orgData.length < 4096) { - return detachedSign(DATA_ORIGIN, orgData, sCertDN); - } else { - String pk; + if(rFlag<4){//RSA + return detachedSignRSA(rFlag,orgData,sCertDN); + }else { + if (orgData.length<4096){ + return detachedSign(DATA_ORIGIN,orgData,sCertDN); + }else { + String pk = null; pk = this.SYD_SM2_Get_Dn_PublicKey_HA(sCertDN.getBytes("GBK"), 0); System.out.println(pk); byte[] hash = this.SM3Hash(pk, orgData); - System.out.println("hash为:" + Util.bytes2HexString(hash)); - return detachedSign(DATA_HASH, hash, sCertDN); + System.out.println("hash为:"+Util.bytes2HexString(hash)); + return detachedSign(DATA_HASH,hash,sCertDN); } } } catch (UnsupportedEncodingException e) { @@ -1751,41 +1728,43 @@ public class SydApi4j implements SydApi { } - public String detachedSign(byte[] orgData, String sCertDN) { + public String detachedSign(byte[] orgData, String sCertDN){ try { - if (orgData.length < 4096) { - return detachedSign(DATA_ORIGIN, orgData, sCertDN); - } else { - String pk; + if (orgData.length<4096){ + return detachedSign(DATA_ORIGIN,orgData,sCertDN); + }else { + String pk = null; pk = this.SYD_SM2_Get_Dn_PublicKey_HA(sCertDN.getBytes("GBK"), 0); System.out.println(pk); byte[] hash = this.SM3Hash(pk, orgData); - System.out.println("hash为:" + Util.bytes2HexString(hash)); - return detachedSign(DATA_HASH, hash, sCertDN); + System.out.println("hash为:"+Util.bytes2HexString(hash)); + return detachedSign(DATA_HASH,hash,sCertDN); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } - public String attachedSignAll(int rFlag, byte[] orgData, String sCertDN) { + public String attachedSignAll(int rFlag,byte[] orgData, String sCertDN){ try { - String cert = getCertByDn(sCertDN, 0); + String cert = getCertByDn(sCertDN,0); boolean sm2Cert = CertUtil.isSM2Cert(cert); - if (!sm2Cert) {//RSA - return attachedSignRSA(rFlag, orgData, sCertDN); - } else { - if (orgData.length < 4096) { + if(!sm2Cert){//RSA + return attachedSignRSA(rFlag,orgData,sCertDN); + }else { + if (orgData.length<4096){ String sign = detachedSign(DATA_ORIGIN, orgData, sCertDN); return ASN1Util.addOriginDataToPKCS7Sign(orgData, sign); - } else { - String pk; + }else { + String pk = null; pk = this.SYD_SM2_Get_Dn_PublicKey_HA(sCertDN.getBytes("GBK"), 0); byte[] hash = this.SM3Hash(pk, orgData); String sign = detachedSign(DATA_HASH, hash, sCertDN); return ASN1Util.addOriginDataToPKCS7Sign(orgData, sign); } } + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } @@ -1795,21 +1774,22 @@ public class SydApi4j implements SydApi { //人行大数据不指定摘要算法 public String detachedSignRSA(int rFlag, byte[] orgData, String sCertDN) { - String sign; - if (orgData.length < 4096) { - sign = detachedSign(rFlag, DATA_ORIGIN, orgData, sCertDN); - } else { - // RetWrap hashData = countHash(0, 0, orgData, null); + String sign =null; + if(orgData.length<4096){ + sign = detachedSign(rFlag,DATA_ORIGIN,orgData,sCertDN); + }else{ + // RetWrap hashData = countHash(0, 0, orgData, null); byte[] hash = itHash(rFlag, orgData); - sign = detachedSign(rFlag, DATA_HASH, hash, sCertDN); + sign = detachedSign(rFlag,DATA_HASH, hash, sCertDN); } return sign; } + public String detachedSign(int rFlag, int dataType, byte[] orgData, String sCertDN) { Proto8005 proto = new Proto8005(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); PacketSN sn = PacketSN.gen(); byte[] dn = new byte[256]; @@ -1821,12 +1801,11 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } - assert s != null; System.arraycopy(s, 0, dn, 0, s.length); // 填充数据 - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); packet.put("header", sn.getSn().array()); @@ -1872,10 +1851,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -1923,8 +1899,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -1947,10 +1923,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -1982,14 +1955,14 @@ public class SydApi4j implements SydApi { Proto8008 proto = new Proto8008(); // 填充数据 - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); - data.put("dataFlag", Util.slenBytes(dataFlag, 4)); + data.put("dataFlag",Util.slenBytes(dataFlag,4)); String sqlite3 = "1 = 1 "; data.put("sqlite3", sqlite3); @@ -2010,10 +1983,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -2028,7 +1998,7 @@ public class SydApi4j implements SydApi { PacketSection dataAttrFlag = (PacketSection) ret.get("dataAttrFlag"); PacketSection realCertNum = (PacketSection) ret.get("realCertNum"); try { - r.put("dataFlag", Util.smallLen(resDataFlag.getBytes())); + r.put("dataFlag",Util.smallLen(resDataFlag.getBytes()) ); r.put("certAllNum", Util.smallLen(certAllNum.getBytes())); r.put("dataAttrFlag", Util.smallLen(dataAttrFlag.getBytes())); r.put("realCertNum", Util.smallLen(realCertNum.getBytes())); @@ -2037,7 +2007,7 @@ public class SydApi4j implements SydApi { } r.put("certLenMax", Integer.parseInt(Util.bytes2HexString(Util.toArray(bb)).substring(60, 62), 16)); - List certs = new ArrayList<>(); + List certs = new ArrayList(); while (bb.limit() - bb.position() >= 128) { byte[] cert = new byte[128]; bb.get(cert); @@ -2046,7 +2016,7 @@ public class SydApi4j implements SydApi { // r.put("certDNs",certs.toArray(new String[0]) ); - r.put("certDNs", certs); + r.put("certDNs",certs); return r; } @@ -2055,39 +2025,36 @@ public class SydApi4j implements SydApi { @Override public List getAllCert() throws UnsupportedEncodingException { + ArrayList m_certs =new ArrayList(); //查出证书总数,一次次的拿出来 - RetWrap allCert = getAllCert(0); - Integer allnum = (Integer) allCert.get("certAllNum");//总数 -// List certs = (List)allCert.get("certDNs");//部分证书列表 - @SuppressWarnings("unchecked") - List certs = (List) allCert.get("certDNs"); - ArrayList m_certs = new ArrayList<>(certs); - int loop = allnum % 16;//循环次数 + RetWrap allCert =getAllCert(0); + Integer allnum =(Integer) allCert.get("certAllNum");//总数 + List certs = (List)allCert.get("certDNs");//部分证书列表 + m_certs.addAll(certs); + int loop = allnum%16;//循环次数 for (int i = 1; i < loop; i++) { - RetWrap partCert = getAllCert(16 * i); -// List partcerts = (List)partCert.get("certDNs"); - @SuppressWarnings("unchecked") - List partcerts = (List) partCert.get("certDNs"); + RetWrap partCert= getAllCert(16*i); + List partcerts = (List)partCert.get("certDNs"); m_certs.addAll(partcerts); } return m_certs; } - + // 测试通过 @Override - public RetWrap getAllCert(int dataFlag, String sqlite) throws UnsupportedEncodingException { + public RetWrap getAllCert(int dataFlag,String sqlite) throws UnsupportedEncodingException { Proto8008 proto = new Proto8008(); // 填充数据 - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); - data.put("dataFlag", Util.slenBytes(dataFlag, 4)); + data.put("dataFlag",Util.slenBytes(dataFlag,4)); data.put("sqlite3", sqlite); @@ -2107,9 +2074,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -2124,7 +2089,7 @@ public class SydApi4j implements SydApi { PacketSection dataAttrFlag = (PacketSection) ret.get("dataAttrFlag"); PacketSection realCertNum = (PacketSection) ret.get("realCertNum"); try { - r.put("dataFlag", Util.smallLen(resDataFlag.getBytes())); + r.put("dataFlag",Util.smallLen(resDataFlag.getBytes()) ); r.put("certAllNum", Util.smallLen(certAllNum.getBytes())); r.put("dataAttrFlag", Util.smallLen(dataAttrFlag.getBytes())); r.put("realCertNum", Util.smallLen(realCertNum.getBytes())); @@ -2133,7 +2098,7 @@ public class SydApi4j implements SydApi { } r.put("certLenMax", Integer.parseInt(Util.bytes2HexString(Util.toArray(bb)).substring(60, 62), 16)); - List certs = new ArrayList<>(); + List certs = new ArrayList(); while (bb.limit() - bb.position() >= 128) { byte[] cert = new byte[128]; bb.get(cert); @@ -2142,7 +2107,7 @@ public class SydApi4j implements SydApi { // r.put("certDNs",certs.toArray(new String[0]) ); - r.put("certDNs", certs); + r.put("certDNs",certs); return r; } @@ -2155,7 +2120,9 @@ public class SydApi4j implements SydApi { } /** - * + * @param certData + * @param organizationId + * @return */ @Override public RetWrap importCertAddHT(String certData, byte[] organizationId) { @@ -2173,8 +2140,8 @@ public class SydApi4j implements SydApi { public String importCert(String certData) { Proto8009 proto = new Proto8009(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -2194,10 +2161,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); @@ -2222,13 +2186,12 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } - assert s != null; System.arraycopy(s, 0, dnData, 0, s.length); // 填充数据 - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -2236,6 +2199,7 @@ public class SydApi4j implements SydApi { data.put("certDn", dnData); + // 渲染发送 ByteBuffer bb = proto.rend(packet); @@ -2253,15 +2217,12 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((be[0] ^ be[1]) != 0) { SydApiException e = new SydApiException(error.getInfo(), error.autoInt()); - println("删除失败=" + e); + println("删除失败=" + e.toString()); return false; } else { return true; @@ -2275,9 +2236,9 @@ public class SydApi4j implements SydApi { Proto8009 proto = new Proto8009(); // 填充数据 - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -2309,10 +2270,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); @@ -2486,7 +2444,7 @@ public class SydApi4j implements SydApi { } StopWatch stopWatch = new StopWatch(); stopWatch.start(); - log.info("socket收发信息异常,准备重试:{}/当前重试:{}次", e.getMessage(), retryCount + 1); + log.info("socket收发信息异常,准备重试:{}/当前重试:{}次", e.getMessage(),retryCount + 1); System.out.println(DateTime.now() + "/socket收发信息异常:" + e.getMessage() + ",准备重试, 当前重试:" + ++retryCount + "次"); boolean currentState = usingBackup.get(); @@ -2533,7 +2491,7 @@ public class SydApi4j implements SydApi { buff = new byte[bb.position()]; System.arraycopy(bb.array(), 0, buff, 0, buff.length); } - if (debug) { + if(debug){ println("\n\n发送数据"); // print("原始数据: \t"); // println(Arrays.toString(buff)); @@ -2542,13 +2500,14 @@ public class SydApi4j implements SydApi { if (null != builder && builder.isUseEBCDMode()) { // 对十个字节之后的进行 EBCD 转码 - CoderUtil.ASCIIToEBCDICFIX(buff, 10); + buff = CoderUtil.ASCIIToEBCDICFIX(buff, 10); print("EBCD 数据: \t"); println(Util.bytes2HexString(buff)); } } + testSocket(); try { @@ -2582,6 +2541,8 @@ public class SydApi4j implements SydApi { /** * 开放此接口 + * + * @param buff */ public void syncSend(byte[] buff) { @@ -2632,7 +2593,7 @@ public class SydApi4j implements SydApi { len += 2; - if (debug) { + if(debug){ print("H\t"); println(Integer.toHexString(high)); print("L\t"); @@ -2666,6 +2627,8 @@ public class SydApi4j implements SydApi { } + + if (null != builder && builder.isUseEBCDMode()) { // print("EBCD 数据: \t"); // println(Util.bytes2HexString(bb.array())); @@ -2673,7 +2636,7 @@ public class SydApi4j implements SydApi { byte[] b = bb.array(); CoderUtil.EBCDICToASCIIFIX(b, 10); } - if (debug) { + if(debug){ // print("EBCD 数据: \t"); // println(Util.bytes2HexString(bb.array())); print("HEX数据: \t"); @@ -2689,13 +2652,15 @@ public class SydApi4j implements SydApi { * 阻塞读取 * 前两个字节是长度 * + * @return * @buffer 如果出错,需要补发的数据。 */ - protected ByteBuffer syncRead(byte[] ignoredBuffer) { + protected ByteBuffer syncRead(byte[] buffer) { // println("\n\n接收数据"); try { // 正常接收 - return read(socket); + ByteBuffer bb = read(socket); + return bb; } catch (SocketTimeoutException e) { throw new SydApiException("接收数据超时", -2); @@ -2799,7 +2764,7 @@ public class SydApi4j implements SydApi { this.retryWhenNetError = retryWhenNetError; this.ip = ip; this.port = port; -// this.pwd = pwd; + this.pwd = pwd; this.connectTimeout = connectTimeout; this.readTimeout = readTimeout; @@ -2820,6 +2785,7 @@ public class SydApi4j implements SydApi { /** * @param e 原始错误 + * @throws SydApiException */ private Socket reConnect(SydApiException e) { while (this.retryWhenNetError > 0) { @@ -2852,20 +2818,22 @@ public class SydApi4j implements SydApi { if (null != socket) { try { socket.getInputStream().close(); - } catch (Exception ignored) { + } catch (Exception e1) { + } finally { } try { socket.getOutputStream().close(); - } catch (Exception ignored) { - ignored.printStackTrace(); + } catch (Exception e2) { + } finally { } try { socket.close(); - } catch (Exception ignored) { + } catch (Exception e) { + } finally { } } @@ -2886,51 +2854,67 @@ public class SydApi4j implements SydApi { private byte[] repackEncrypt(HashMap packet) { -// HashMap data = (HashMap) packet.get("data"); - @SuppressWarnings("unchecked") HashMap data = (HashMap) packet.get("data"); int v = ((byte[]) data.get("msg")).length; final ByteBuffer bbs = ByteBuffer.allocate(v + 1024); - repack("E0", 2048, packet, (packSn, packet1, ret) -> bbs.put(((PacketSection) ret.get("msg")).getBytes())); + repack("E0", 2048, packet, new PackCallBack() { + + @Override + public void circle(int packSn, HashMap packet, Map ret) { + bbs.put(((PacketSection) ret.get("msg")).getBytes()); + } + + }); return Util.toArray(bbs); } private RetWrap repackEncrypt(HashMap packet, Boolean isDe) { -// HashMap data = (HashMap) packet.get("data"); - int v = ((byte[]) packet.get("msg")).length; + HashMap data = (HashMap) packet.get("data"); + int v = ((byte[]) data.get("msg")).length; final ByteBuffer bbs = ByteBuffer.allocate(v + 1024); int packSize = 4064; // 2048/3072/4064 int packSizeDe = packSize + 16; // int fm = (int) data.get("fullMode$option"); - int fm = (Integer) packet.get("fullMode$option"); + int fm = (Integer) data.get("fullMode$option"); if (isDe && 1 == fm && v <= packSizeDe && v > packSize) { //说明传入数据正好是2048,pkcs模式下机密来的,解密时需当做唯一包处理 packSize = packSizeDe; } + RetWrap retWrap = repack("E0", packSize, packet, new PackCallBack() { + @Override + public void circle(int packSn, HashMap packet, Map ret) { + bbs.put(((PacketSection) ret.get("msg")).getBytes()); + + } + }); //return Util.toArray(bbs); - return repack("E0", packSize, packet, (packSn, packet1, ret) -> bbs.put(((PacketSection) ret.get("msg")).getBytes())); + return retWrap; } private byte[] repackHash(HashMap packet) { -// HashMap data = (HashMap) packet.get("data"); -// int v = ((byte[]) data.get("msg")).length; -// final ByteBuffer bbs = ByteBuffer.allocate(v + 1024); + HashMap data = (HashMap) packet.get("data"); + int v = ((byte[]) data.get("msg")).length; + final ByteBuffer bbs = ByteBuffer.allocate(v + 1024); final byte[][] hash = new byte[1][0]; - repack("76", 2048, packet, (packNo, packet1, ret) -> { - if (0 == packNo || 3 == packNo) { - hash[0] = ((PacketSection) ret.get("hash")).getBytes(); - } else { - byte[] filterData = ((PacketSection) ret.get("filterData")).getBytes(); - HashMap data = (HashMap) packet1.get("data"); - data.put("filterData", filterData); + repack("76", 2048, packet, new PackCallBack() { + + @Override + public void circle(int packNo, HashMap packet, Map ret) { + if (0 == packNo || 3 == packNo) { + hash[0] = ((PacketSection) ret.get("hash")).getBytes(); + } else { + byte[] filterData = ((PacketSection) ret.get("filterData")).getBytes(); + HashMap data = (HashMap) packet.get("data"); + data.put("filterData", filterData); + } } }); @@ -2939,11 +2923,11 @@ public class SydApi4j implements SydApi { /** * 分包发送接收 + * + * @param packSize */ - private RetWrap repack(String ignoredCmd, int packSize, HashMap packet, PackCallBack cb) { -// HashMap data = (HashMap) packet.get("data"); - @SuppressWarnings("unchecked") + private RetWrap repack(String cmd, int packSize, HashMap packet, PackCallBack cb) { HashMap data = (HashMap) packet.get("data"); int v = ((byte[]) data.get("msg")).length; @@ -2952,9 +2936,9 @@ public class SydApi4j implements SydApi { //>2k,即进行分块,首尾包进行解密-> 报错 //如果是PKCS模式,packSize应为2064,才能当做唯一包进行解密 -// int fm = (Integer) data.get("fullMode$option"); - int loop; - int tailLen; + int fm = (Integer) data.get("fullMode$option"); + int loop = 0; + int tailLen = 0; //解密时,PKCS,2048 dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -3156,20 +3142,20 @@ public class SydApi4j implements SydApi { // msg_all 类型改 List, 当 唯一包、尾包时还原 byte[] 类型 if (retWrap.containsKey("msg_all")) { BytesList msg_all = (BytesList) retWrap.get("msg_all"); - msg_all.append(res_msg); + msg_all.append( res_msg ); } else { - retWrap.put("msg_all", new BytesList(res_msg)); + retWrap.put("msg_all", new BytesList(res_msg) ); } if (0 == i) { if (1 == loop) { // 唯一包 - retWrap.put("msg_all", ((BytesList) retWrap.get("msg_all")).toBytes()); + retWrap.put("msg_all", ((BytesList)retWrap.get("msg_all")).toBytes() ); } } else { if (i == (loop - 1)) { // 尾包 - retWrap.put("msg_all", ((BytesList) retWrap.get("msg_all")).toBytes()); + retWrap.put("msg_all", ((BytesList)retWrap.get("msg_all")).toBytes() ); } } cb.circle(packNo, packet, ret); @@ -3188,10 +3174,10 @@ public class SydApi4j implements SydApi { } int packSize = 8000; - int readSize; + int readSize = 8000; // 获取文件长度 - int lenIndata; + int lenIndata = 0; File file = new File(inputPath); if (file.exists() && file.isFile()) { lenIndata = (int) file.length(); @@ -3223,14 +3209,8 @@ public class SydApi4j implements SydApi { File outFile = new File(outputPath); //清空文件 try { -// if (!outFile.exists()) { -// outFile.createNewFile(); -// } if (!outFile.exists()) { - boolean created = outFile.createNewFile(); - if (!created) { - throw new IOException("创建文件失败: " + outputPath); - } + outFile.createNewFile(); } FileWriter fileWriter = new FileWriter(outFile); fileWriter.write(""); @@ -3245,9 +3225,9 @@ public class SydApi4j implements SydApi { for (int i = 0; i < loop; i++) { PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); switch (nAlg) { case 0: @@ -3292,14 +3272,8 @@ public class SydApi4j implements SydApi { // 中间包 data.put("packSn$option", 2); readSize = packSize; -// byte[] msg = new byte[readSize]; -// fileInputStream.read(msg); byte[] msg = new byte[readSize]; - int bytesRead = fileInputStream.read(msg); - if (bytesRead != readSize) { - throw new IOException("读取数据不完整,期望读取: " + readSize + " 字节,实际读取: " + bytesRead + " 字节"); - } - + fileInputStream.read(msg); println("\n\t中间包"); data.put("msg", msg); } @@ -3377,8 +3351,8 @@ public class SydApi4j implements SydApi { public RetWrap SYMEnDeData(Boolean isEn, int nAlg, String seesionKey, int roundmod, int fullMode, byte[] pInData, byte[] iv, int block) { // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); // 确认算法 if (isEn) { @@ -3429,10 +3403,11 @@ public class SydApi4j implements SydApi { // Map map = repackEncrypt(packet); // Map map = repackEncrypt(packet,!isEn); + RetWrap retWrap = repackEncrypt(packet, !isEn); // System.out.println(retWrap); - return repackEncrypt(packet, !isEn); + return retWrap; } /** @@ -3442,6 +3417,7 @@ public class SydApi4j implements SydApi { * @param userId 生成密钥时的机构号。 * @param index 密钥索引 * @param data 待加密的数据 + * @return */ public byte[] SYMSM4EncDataByIndex(int type, String userId, String index, byte[] data) { @@ -3464,8 +3440,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); switch (nAlg) { case 0: @@ -3579,7 +3555,7 @@ public class SydApi4j implements SydApi { byte[] code = new byte[2]; bb.get(code); - int retCode = Integer.parseInt(new String(code)); + int retCode = Integer.valueOf(new String(code)); if (0 != retCode) { throw new SydApiException(retCode); } @@ -3595,7 +3571,7 @@ public class SydApi4j implements SydApi { lens[i] = Integer.valueOf(new String(iLenArray), 16); } - List ret = new ArrayList<>(); + List ret = new ArrayList(); // 获取 N 个长度的数据 for (int l : lens) { byte[] d = new byte[l]; @@ -3615,8 +3591,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); switch (nAlg) { case 0: @@ -3653,8 +3629,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); switch (nAlg) { case 0: @@ -3695,6 +3671,7 @@ public class SydApi4j implements SydApi { * @param userId 生成密钥时的机构号 * @param index 密钥索引 * @param pINdata 要解密的报文密文。长度必须是8 或16 的倍数 + * @return */ public byte[] SYMSM4DecDataByIndex(int type, String userId, String index, byte[] pINdata) { @@ -3720,9 +3697,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); switch (nAlg) { case 0: @@ -3772,9 +3749,9 @@ public class SydApi4j implements SydApi { Proto6A proto = new Proto6A(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("alg$option", nAlg - 0x30); if (pFlag == 1) { data.put("pFlag", 'P'); @@ -3806,9 +3783,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -3842,9 +3817,9 @@ public class SydApi4j implements SydApi { @Override - public RetWrap generateDEByDnICBC(String certSerial, String IV, byte[] oData, int roundmod) { + public RetWrap generateDEByDnICBC(String certSerial,String IV, byte[] oData,int roundmod ) { - // roundmod 轮模式,密钥加密模式:1=ECB,2=CBC + // roundmod 轮模式,密钥加密模式:1=ECB,2=CBC certSerial = verifyDn(certSerial); @@ -3886,20 +3861,20 @@ public class SydApi4j implements SydApi { // 加密数据 byte[] iv; - if (IV == null || IV.isEmpty()) { + if(IV == null || IV.isEmpty()){ String random = GenerateRandom(16); - System.out.println("random:" + random); + System.out.println("random:"+random); iv = random.getBytes(); - // Arrays.fill(iv, (byte) 0x30); - } else { + // Arrays.fill(iv, (byte) 0x30); + }else{ // if(32 != IV.getBytes().length){ // throw new SydApiException("IV长度需为32位", 0x81); // } iv = IV.getBytes(); } - /* - E0 指令复合接口 - @param isEn 指定加解密,true为加密,false为解密 + /** + * E0 指令复合接口 + * @param isEn 指定加解密,true为加密,false为解密 * @param nAlg 密钥模式,当isEn为true时,默认为SM4加密,0为DES加密,1为AES256 加密。当isEn为false时,默认为SM4解密,0为DES解密,1为AES256解密 * @param seesionKey 密钥 * @param roundmod 轮模式,密钥加密模式:1=ECB,2=CBC,3=CFB,4=OFB @@ -3914,14 +3889,14 @@ public class SydApi4j implements SydApi { RetWrap retWrap = SYMEnDeData(true, 1, sessionKeyByLMK, roundmod, 1, oData, iv, 0); byte[] enData = (byte[]) retWrap.get("msg_all"); - // System.out.println("加密结果:"+Util.bytes2HexString(enData)); + // System.out.println("加密结果:"+Util.bytes2HexString(enData)); // 组织格式 ContentInfo cms = genCMSEnvelopedData(Util.hexString2Bytes(ciperKey), enData, x509.getSerialNumber().toString(16), - dnToMap(issuser.getName()), Util.hexString2Bytes(new String(iv)) + dnToMap(issuser.getName()),Util.hexString2Bytes(new String(iv)) ); - // System.out.println("信封结果:"+Util.bytes2HexString(cms.getEncoded(DL))); + // System.out.println("信封结果:"+Util.bytes2HexString(cms.getEncoded(DL))); ret.put(SydApi.RET_CIPHER_KEY, Util.encodeBase64(cms.getEncoded(DL))); @@ -3940,27 +3915,28 @@ public class SydApi4j implements SydApi { if (oData.length <= 4 * 1024) { return generateDEByDnHsmRSA(alg, certSerial, oData); } else { - return generateDEByDnSoftRSA(certSerial, oData); + return generateDEByDnSoftRSA(alg, certSerial, oData); } } - public RetWrap generateDEByDnAll(int alg, String certSerial, byte[] oData) { - String cert; + public RetWrap generateDEByDnAll(int alg,String certSerial, byte[] oData) { + String cert = null; try { cert = getCertByDn(certSerial); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } boolean sm2Cert = CertUtil.isSM2Cert(cert); - if (sm2Cert) { - return generateDEByDn(certSerial, oData); - } else { - return generateDEByDnRSA(alg, certSerial, oData); + if(sm2Cert){ + return generateDEByDn(certSerial,oData); + }else{ + return generateDEByDnRSA(alg,certSerial,oData); } } + public RetWrap generateDEByDnHsm(String certSerial, byte[] oData) { certSerial = verifyDn(certSerial); @@ -3992,10 +3968,10 @@ public class SydApi4j implements SydApi { Proto6A proto = new Proto6A(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("alg$option", alg); data.put("certificateModel$option", model); @@ -4034,9 +4010,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -4060,9 +4034,9 @@ public class SydApi4j implements SydApi { Proto6B proto = new Proto6B(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("certificateModel$option", 0); data.put("certificateSn", certSerial); data.put("key", pCipherKey); @@ -4085,9 +4059,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -4111,7 +4083,7 @@ public class SydApi4j implements SydApi { public RetWrap decryptDEByDNHsm(String certSerial, String pCipherKey) { - // certSerial = verifyDn(certSerial); + // certSerial = verifyDn(certSerial); byte[] dnBytes = null; try { @@ -4138,9 +4110,12 @@ public class SydApi4j implements SydApi { /** * 通过 DN 查找证书 + * + * @param dn + * @return */ public String getCertByDn(String dn) throws UnsupportedEncodingException { - byte[] dnBytes; + byte[] dnBytes = null; try { String dnsort = verifyDn(dn); dnBytes = dnsort.getBytes("gbk"); @@ -4152,8 +4127,8 @@ public class SydApi4j implements SydApi { } - public String getCertByDn(String dn, int type) throws UnsupportedEncodingException { - byte[] dnBytes; + public String getCertByDn(String dn,int type) throws UnsupportedEncodingException { + byte[] dnBytes = null; try { String dnsort = verifyDn(dn); dnBytes = dnsort.getBytes("gbk"); @@ -4161,7 +4136,7 @@ public class SydApi4j implements SydApi { e.printStackTrace(); throw e; } - return getCertByDn(dnBytes, type); + return getCertByDn(dnBytes,type); } public String getCertByDn(byte[] dnBytes) { @@ -4169,7 +4144,7 @@ public class SydApi4j implements SydApi { // head PacketSN sn = PacketSN.gen(); -// HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); // data byte[] data = new byte[256]; @@ -4220,12 +4195,12 @@ public class SydApi4j implements SydApi { return new String(cert); } - public String getCertByDn(byte[] dnBytes, int type) { + public String getCertByDn(byte[] dnBytes,int type) { // 8010 指令,没有指令配置文件 // head PacketSN sn = PacketSN.gen(); -// HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); // data byte[] data = new byte[256]; @@ -4247,10 +4222,10 @@ public class SydApi4j implements SydApi { // 256 Data bb.put(data); - if (type == 0) { + if(type ==0 ){ // 签名 bb.put((byte) 0x30); - } else { + }else{ // 加密 bb.put((byte) 0x31); } @@ -4353,7 +4328,7 @@ public class SydApi4j implements SydApi { } } - public RetWrap generateDEByDnSoftRSA(String certSerial, byte[] oData) { + public RetWrap generateDEByDnSoftRSA(int alg, String certSerial, byte[] oData) { certSerial = verifyDn(certSerial); @@ -4418,7 +4393,7 @@ public class SydApi4j implements SydApi { } private static Map dnToMap(String dn) { - Map map = new HashMap<>(); + Map map = new HashMap(); String[] kvs = dn.split(","); for (String kv : kvs) { String[] ps = kv.split("="); @@ -4475,12 +4450,12 @@ public class SydApi4j implements SydApi { ASN1Set setO = (ASN1Set) seq4.getObjectAt(1).toASN1Primitive(); ASN1Sequence seqO = (ASN1Sequence) setO.getObjectAt(0).toASN1Primitive(); DERUTF8String o = (DERUTF8String) seqO.getObjectAt(1).toASN1Primitive(); - BeanUtil.setFieldValue(o, "string", issuer.get("O").getBytes(StandardCharsets.UTF_8)); + BeanUtil.setFieldValue(o, "string", issuer.get("O").getBytes("UTF-8")); ASN1Set setCN = (ASN1Set) seq4.getObjectAt(2).toASN1Primitive(); ASN1Sequence seqCn = (ASN1Sequence) setCN.getObjectAt(0).toASN1Primitive(); DERUTF8String Cn = (DERUTF8String) seqCn.getObjectAt(1).toASN1Primitive(); - BeanUtil.setFieldValue(Cn, "string", issuer.get("CN").getBytes(StandardCharsets.UTF_8)); + BeanUtil.setFieldValue(Cn, "string", issuer.get("CN").getBytes("UTF-8")); // 密文段 ASN1Sequence seq5 = (ASN1Sequence) seq.getObjectAt(2).toASN1Primitive(); @@ -4491,7 +4466,7 @@ public class SydApi4j implements SydApi { } - public static ContentInfo genCMSEnvelopedData(byte[] sessionKeyDer, byte[] enData, String sn, Map issuer, byte[] iv) throws UnsupportedEncodingException { + public static ContentInfo genCMSEnvelopedData(byte[] sessionKeyDer, byte[] enData, String sn, Map issuer,byte[] iv) throws UnsupportedEncodingException { //String tmpl = "3082014D06092A864886F70D010703A082013E3082013A0201003181F73081F40201003065305C310B300906035504061302434E3130302E060355040A0C274368696E612046696E616E6369616C2043657274696669636174696F6E20417574686F72697479311B301906035504030C1243464341205445535420534D32204F43413102051004282452300B06092A811CCF5501822D03047B307902203C3AF535307DA548DA02C0AD171D2BC24D22472B42E5E175FCAF2C5CD30B5443022100ED53E7594F35FFACEB57F05898123F78E5B2D045CC47D894CD2E7BD34D66DA1D042014BD71F8CDA9729093A7CC391E2009C5D5F007586C2EBFB55BC175A6E8BDA33E04104AB82510BFDDD4FEE79C3F107D6F7F94303B060A2A811CCF550601040201301B06072A811CCF55016804100000000000000000000000000000000080109FD3D4338DB19E672CC162AF5B9B03E9"; String tmpl = "3082014E060A2A811CCF550601040203A082013E3082013A0201003181F73081F40201003065305C310B300906035504061302434E3130302E060355040A0C274368696E612046696E616E6369616C2043657274696669636174696F6E20417574686F72697479311B301906035504030C1243464341205445535420534D32204F43413102051004282452300B06092A811CCF5501822D03047B307902203C3AF535307DA548DA02C0AD171D2BC24D22472B42E5E175FCAF2C5CD30B5443022100ED53E7594F35FFACEB57F05898123F78E5B2D045CC47D894CD2E7BD34D66DA1D042014BD71F8CDA9729093A7CC391E2009C5D5F007586C2EBFB55BC175A6E8BDA33E04104AB82510BFDDD4FEE79C3F107D6F7F94303B060A2A811CCF550601040201301B06072A811CCF55016804100000000000000000000000000000000080109FD3D4338DB19E672CC162AF5B9B03E9"; ContentInfo info = SydCmsUtil.getContentInfo(Util.hexString2Bytes(tmpl)); @@ -4514,12 +4489,12 @@ public class SydApi4j implements SydApi { ASN1Set setO = (ASN1Set) seq4.getObjectAt(1).toASN1Primitive(); ASN1Sequence seqO = (ASN1Sequence) setO.getObjectAt(0).toASN1Primitive(); DERUTF8String o = (DERUTF8String) seqO.getObjectAt(1).toASN1Primitive(); - BeanUtil.setFieldValue(o, "string", issuer.get("O").getBytes(StandardCharsets.UTF_8)); + BeanUtil.setFieldValue(o, "string", issuer.get("O").getBytes("UTF-8")); ASN1Set setCN = (ASN1Set) seq4.getObjectAt(2).toASN1Primitive(); ASN1Sequence seqCn = (ASN1Sequence) setCN.getObjectAt(0).toASN1Primitive(); DERUTF8String Cn = (DERUTF8String) seqCn.getObjectAt(1).toASN1Primitive(); - BeanUtil.setFieldValue(Cn, "string", issuer.get("CN").getBytes(StandardCharsets.UTF_8)); + BeanUtil.setFieldValue(Cn, "string", issuer.get("CN").getBytes("UTF-8")); // 密文段 ASN1Sequence seq5 = (ASN1Sequence) seq.getObjectAt(2).toASN1Primitive(); @@ -4528,7 +4503,7 @@ public class SydApi4j implements SydApi { BeanUtil.setFieldValue(data, "string", enData); //iv段 - ASN1Sequence seq6 = (ASN1Sequence) seq5.getObjectAt(1).toASN1Primitive(); + ASN1Sequence seq6 = (ASN1Sequence)seq5.getObjectAt(1).toASN1Primitive(); DEROctetString ivtag = (DEROctetString) seq6.getObjectAt(1).toASN1Primitive(); BeanUtil.setFieldValue(ivtag, "string", iv); @@ -4554,14 +4529,14 @@ public class SydApi4j implements SydApi { BeanUtil.setFieldValue(isn, "bytes", Util.hexString2Bytes(sn)); //签发者名称 -// ASN1Sequence seq4 = (ASN1Sequence) seq3.getObjectAt(0).toASN1Primitive(); + ASN1Sequence seq4 = (ASN1Sequence) seq3.getObjectAt(0).toASN1Primitive(); // ASN1Set setO = (ASN1Set) seq4.getObjectAt(1).toASN1Primitive(); // ASN1Sequence seqO = (ASN1Sequence) setO.getObjectAt(0).toASN1Primitive(); // DERUTF8String o = (DERUTF8String) seqO.getObjectAt(1).toASN1Primitive(); // BeanUtil.setFieldValue(o, "string", issuer.get("O").getBytes("UTF-8") ); -// ASN1Set setCN = (ASN1Set) seq4.getObjectAt(2).toASN1Primitive(); -// ASN1Sequence seqCn = (ASN1Sequence) setCN.getObjectAt(0).toASN1Primitive(); + ASN1Set setCN = (ASN1Set) seq4.getObjectAt(2).toASN1Primitive(); + ASN1Sequence seqCn = (ASN1Sequence) setCN.getObjectAt(0).toASN1Primitive(); // DERUTF8String Cn = (DERUTF8String) seqCn.getObjectAt(1).toASN1Primitive(); // BeanUtil.setFieldValue(Cn, "string", issuer.get("CN").getBytes("UTF-8") ); @@ -4582,7 +4557,7 @@ public class SydApi4j implements SydApi { } } - public RetWrap decryptDEByDNICBC(String certSerial, String IV, String pCipherKey, int roundmod) { + public RetWrap decryptDEByDNICBC(String certSerial, String IV,String pCipherKey,int roundmod) { certSerial = verifyDn(certSerial); @@ -4593,7 +4568,7 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } - RetWrap ret; + RetWrap ret = null; try { ret = deCMSEnvelopedData(Util.decodeBase64(pCipherKey)); @@ -4606,8 +4581,8 @@ public class SydApi4j implements SydApi { // 解密原文 byte[] iv = Util.bytes2HexString((byte[]) ret.get("iv")).getBytes(); //new byte[32]; - System.out.println("实际iv值hex:" + Util.bytes2HexString((byte[]) ret.get("iv"))); - if (null != IV && !IV.isEmpty()) { + System.out.println("实际iv值hex:"+Util.bytes2HexString((byte[]) ret.get("iv"))); + if(null != IV && !IV.isEmpty()){ iv = IV.getBytes(); } // 解密会话密钥 @@ -4632,18 +4607,18 @@ public class SydApi4j implements SydApi { } } - public RetWrap decryptDEByDNAll(int alg, String certSerial, byte[] pCipherKey) { - String cert; + public RetWrap decryptDEByDNAll(int alg,String certSerial, byte[] pCipherKey) { + String cert = null; try { cert = getCertByDn(certSerial); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } boolean sm2Cert = CertUtil.isSM2Cert(cert); - if (sm2Cert) { - return decryptDEByDN(certSerial, pCipherKey); - } else { - return decryptDEByDNRSA(alg, certSerial, pCipherKey); + if(sm2Cert){ + return decryptDEByDN(certSerial,pCipherKey); + }else{ + return decryptDEByDNRSA(alg,certSerial,pCipherKey); } } @@ -4658,7 +4633,7 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } - RetWrap ret; + RetWrap ret = null; try { ret = deCMSEnvelopedData(Util.decodeBase64(pCipherKey)); @@ -4697,7 +4672,7 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } - RetWrap ret; + RetWrap ret = null; try { ret = deCMSEnvelopedData(Util.decodeBase64(pCipherKey)); @@ -4737,10 +4712,10 @@ public class SydApi4j implements SydApi { Proto6BXF proto = new Proto6BXF(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); if (pFlag) { data.put("pFlag", "P"); } @@ -4774,10 +4749,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -4811,10 +4783,10 @@ public class SydApi4j implements SydApi { Proto6BXF proto = new Proto6BXF(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("pFlag", "R"); if (!pFlag) { data.put("pFlag", "Q"); @@ -4847,10 +4819,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -4884,9 +4853,9 @@ public class SydApi4j implements SydApi { Proto6BRSA proto = new Proto6BRSA(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("pFlag", "R"); if (!pFlag) { data.put("pFlag", "Q"); @@ -4918,10 +4887,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -4956,9 +4922,9 @@ public class SydApi4j implements SydApi { //填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("nRandomSize", nRandomSize); packet.put("data", data); @@ -4976,10 +4942,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5009,9 +4972,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("certificateModel$option", 0); data.put("certificateSn", pCertSerial); data.put("inData", pOrgData); @@ -5033,10 +4996,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5064,9 +5024,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("certificateModel$option", 0); data.put("certificateSn", pCertSerial); data.put("inData", pOrgData); @@ -5088,10 +5048,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5115,9 +5072,9 @@ public class SydApi4j implements SydApi { Proto6A proto = new Proto6A(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); try { data.put("alg$option", 3); //data.put("pFlag", "P"); @@ -5153,10 +5110,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5182,9 +5136,9 @@ public class SydApi4j implements SydApi { Proto6B proto = new Proto6B(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); try { data.put("pFlag", "P"); data.put("pSignFlag", "P"); @@ -5216,17 +5170,15 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); } RetWrap rw = new RetWrap(); rw.put("oData", ((PacketSection) ret.get("oData")).getBytes()); - return (byte[]) rw.get("oData"); + byte[] odata = (byte[]) rw.get("oData"); + return odata; // int orgCodeLen = Util.a2i(((PacketSection) ret.get("orgCodeLen")).getBytes(), 10); // if (orgCodeLen != 0) { @@ -5261,9 +5213,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("certificateModel$option", 0); data.put("certificateSn", pCertSerial); data.put("inData", pOrgData); @@ -5285,10 +5237,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5316,9 +5265,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("certificateModel$option", 0); data.put("certificateSn", pCertSerial); data.put("inData", pOrgData); @@ -5340,10 +5289,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5371,9 +5317,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("certificateModel$option", 0); data.put("certificateSn", pCertSerial); data.put("inData", pOrgData); @@ -5394,10 +5340,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5414,9 +5357,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("certificateModel$option", 0); data.put("certificateSn", pCertSerial); data.put("inData", pOrgData); @@ -5437,9 +5380,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5457,9 +5398,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("certificateModel$option", nCertType >= 0x30 ? nCertType - 0x30 : nCertType); data.put("certificateDn", certDN); packet.put("data", data); @@ -5478,10 +5419,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5495,8 +5433,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender("E2"); ProtoE2 proto = new ProtoE2(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -5513,7 +5451,7 @@ public class SydApi4j implements SydApi { //ProtocolParser parser = pset.getProtocolResParse(); - Map ret; + Map ret = null; Packet p = proto.parse(bb); ret = p.toMap(); @@ -5523,10 +5461,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5546,8 +5481,8 @@ public class SydApi4j implements SydApi { @Override public RetWrap SM3Hash(int packSn, String userId, byte[] publicKey, byte[] msg, byte[] filterData) { Proto76 proto = new Proto76(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -5563,15 +5498,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5594,8 +5527,8 @@ public class SydApi4j implements SydApi { System.out.println(getClass().getClassLoader().hashCode()); ProtoE2 proto = new ProtoE2(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); int keyLength = -1; if (keyType == 0) { @@ -5621,15 +5554,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5648,14 +5579,15 @@ public class SydApi4j implements SydApi { public boolean RSAVerify(int HashMark, int nOrgDataType, byte[] publicKey, byte[] orgData, byte[] base64Sign) { Proto7C proto = new Proto7C(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + byte[] sign = base64Sign; + 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", nOrgDataType); data.put("hashMark$option", HashMark); - data.put("signature", base64Sign); + data.put("signature", sign); data.put("hashData", orgData); data.put("infoData", orgData); data.put("publicKey", publicKey); @@ -5671,9 +5603,7 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if ("02".equals(error.getString())) { return false; @@ -5689,8 +5619,8 @@ public class SydApi4j implements SydApi { @Override public byte[] RSASignC(String privateKeyIndex, int HashMark, int nOrgDataType, byte[] orgData) { Proto7B proto = new Proto7B(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -5734,8 +5664,8 @@ public class SydApi4j implements SydApi { @Override public RetWrap genSymmetricKey(String keyType, char solution) { ProtoA0 proto = new ProtoA0(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -5750,15 +5680,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5780,8 +5708,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap genSymKey(int mode, String keyType, char solution, String zmk) { ProtoA0 proto = new ProtoA0(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -5799,15 +5727,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5840,8 +5766,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender("QQ"); ProtoQQ proto = new ProtoQQ(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -5861,10 +5787,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5888,8 +5811,8 @@ public class SydApi4j implements SydApi { public String SM2GetPublicKeyC(String privateKeyTag) { ProtoQQ proto = new ProtoQQ(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -5910,9 +5833,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5930,8 +5851,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender("QQ"); ProtoQQ proto = new ProtoQQ(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -5952,9 +5873,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5984,8 +5903,8 @@ public class SydApi4j implements SydApi { ProtoQP proto = new ProtoQP(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6006,9 +5925,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -6027,7 +5944,8 @@ public class SydApi4j implements SydApi { /** * 通过私钥获取公钥 * - * @param userId 密钥对应的索引号 + * @param userId 密钥对应的索引号 + * @param nKeyType * @return 公钥 */ @Override @@ -6035,8 +5953,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender("QQ"); ProtoQX proto = new ProtoQX(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6063,9 +5981,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -6083,8 +5999,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender(new byte[]{(byte) 0x80, 0x16}); Proto8016 proto = new Proto8016(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6126,16 +6042,17 @@ public class SydApi4j implements SydApi { throw new SydApiException(error.getInfo(), error.autoInt()); }*/ + byte[] pCertInfo = s; //byte[] pCertInfo = ((PacketSection) ret.get("certInfo")).getBytes(); //Util.bytes2HexString(pCertInfo); X509 cinfo = new X509(); - cinfo.setIssuer(Util.toCleanString(s, 0, 257)); - cinfo.setSerialNumber(Util.toCleanString(s, 257, 65)); - cinfo.setSubject(Util.toCleanString(s, 257 + 65, 257)); - cinfo.setNotBefore(Util.toCleanString(s, 257 + 65 + 257, 15)); - cinfo.setNotAfter(Util.toCleanString(s, 257 + 65 + 257 + 15, 15)); - cinfo.setPubKey(s, 257 + 65 + 257 + 15 + 15, s.length - (257 + 65 + 257 + 15 + 15)); + 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)); + cinfo.setPubKey(pCertInfo, 257 + 65 + 257 + 15 + 15, pCertInfo.length - (257 + 65 + 257 + 15 + 15)); return cinfo; } @@ -6145,6 +6062,7 @@ public class SydApi4j implements SydApi { * * @param sn sn * @param nCertType 证书类型,0:签名证书, 1:加密证书 + * @return */ @Override public String GetX509CertFileBySN(String sn, int nCertType) { @@ -6163,12 +6081,13 @@ public class SydApi4j implements SydApi { * @param flag 0: SN,1: 机构号 * @param userId 根据flag确定传入的数据 * @param nCertType 证书类型,0:签名证书, 1:加密证书 + * @return */ private String GetX509CertFileC(int flag, String userId, int nCertType) { Proto8017 proto = new Proto8017(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6206,10 +6125,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); @@ -6243,8 +6159,8 @@ public class SydApi4j implements SydApi { Proto74 proto = new Proto74(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -6301,8 +6217,8 @@ public class SydApi4j implements SydApi { byte[] sign = SYMUtil.base64Sign2DerSign(base64Sign); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6314,7 +6230,7 @@ public class SydApi4j implements SydApi { // 渲染发送 ByteBuffer bb = proto.rend(packet); -// long b = System.currentTimeMillis(); + long b = System.currentTimeMillis(); synchronized (this) { // 响应解析 @@ -6329,9 +6245,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { @@ -6354,8 +6268,8 @@ public class SydApi4j implements SydApi { Proto74 proto = new Proto74(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6419,8 +6333,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6443,9 +6357,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { @@ -6486,8 +6398,8 @@ public class SydApi4j implements SydApi { Proto74 proto = new Proto74(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6536,8 +6448,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6560,11 +6472,13 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); - return "00".equals(error.getString()); + if (!"00".equals(error.getString())) { + + return false; + } + return true; } @@ -6582,8 +6496,8 @@ public class SydApi4j implements SydApi { sign = SYMUtil.base64Sign2DerSign(Util.encodeBase64(sign)); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6596,7 +6510,7 @@ public class SydApi4j implements SydApi { // 渲染发送 ByteBuffer bb = proto.rend(packet); -// long b = System.currentTimeMillis(); + long b = System.currentTimeMillis(); synchronized (this) { // 响应解析 @@ -6611,9 +6525,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { @@ -6653,7 +6565,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } -// Packet p = proto.parse(bb); + Packet p = proto.parse(bb); DTO79.Res res = new DTO79.Res(); res = res.fullFromSoup(proto.parse(bb).resSoup()); @@ -6693,7 +6605,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } -// Packet p = proto.parse(bb); + Packet p = proto.parse(bb); DTO7A.Res res = new DTO7A.Res(); res = res.fullFromSoup(proto.parse(bb).resSoup()); @@ -6727,7 +6639,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } -// Packet p = proto.parse(bb); + Packet p = proto.parse(bb); DTO77.Res res = new DTO77.Res(); res = res.fullFromSoup(proto.parse(bb).resSoup()); @@ -6784,7 +6696,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } -// Packet p = proto.parse(bb); + Packet p = proto.parse(bb); DTO78.Res res = new DTO78.Res(); res = res.fullFromSoup(proto.parse(bb).resSoup()); @@ -6819,7 +6731,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } -// Packet p = proto.parse(bb); + Packet p = proto.parse(bb); DTO78.Res res = new DTO78.Res(); res = res.fullFromSoup(proto.parse(bb).resSoup()); @@ -6870,7 +6782,7 @@ public class SydApi4j implements SydApi { * * @param sk HEX 私钥值 * @param enData base64 值 - * @return 解密后的明文数据 + * @return */ @Override public byte[] RSADecrypt(String sk, String enData) { @@ -6880,13 +6792,14 @@ public class SydApi4j implements SydApi { } + @Override public String SM2EncryptOrder(byte[] publicKey, byte[] pOrgData) { Proto77 proto = new Proto77(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6907,9 +6820,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -6919,7 +6830,7 @@ public class SydApi4j implements SydApi { } - public byte[] SYD_SM2Encrypt(byte[] publicKey, byte[] pOrgData) { + public byte[] SYD_SM2Encrypt(byte[] publicKey, byte[] pOrgData){ ParamChecker.checkNotNull(publicKey, "publicKey"); ParamChecker.checkNotNull(pOrgData, "pOrgData"); @@ -6978,30 +6889,30 @@ public class SydApi4j implements SydApi { // 最大缓存 64K // 根据输入数据快速估算缓存大小 int buffLen = 4096; - if (1 == dataType) { - buffLen = pOrgData.limit() > 4096 ? 64 * 1024 : 5 * 1024; + if( 1 == dataType ) { + buffLen = pOrgData.limit() > 4096 ? 64*1024 : 5*1024; } - ByteBuffer bb = ByteBuffer.allocate(buffLen); + 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) { // 公钥 + if ( 2 == dataType ) { // 公钥 bb.put(ByteUtil.shortToBytes((short) publicKey.length)); bb.put(publicKey); } - if (1 == dataType) { // 数据 + if ( 1 == dataType ) { // 数据 bb.put(ByteUtil.shortToBytes((short) pOrgData.limit())); bb.put(pOrgData); - bb.put(ByteUtil.shortToBytes((short) idx)); + bb.put(ByteUtil.shortToBytes((short)idx)); } - if (3 == dataType) { // 仅数据长度 - bb.put(ByteUtil.shortToBytes((short) (63 * 1024))); - bb.put(ByteUtil.shortToBytes((short) idx)); + if ( 3 == dataType) { // 仅数据长度 + bb.put( ByteUtil.shortToBytes((short) (63*1024)) ); + bb.put( ByteUtil.shortToBytes((short)idx)); } @@ -7022,13 +6933,13 @@ public class SydApi4j implements SydApi { byte[] code = new byte[2]; bb.get(code); - int retCode = Integer.parseInt(new String(code)); + int retCode = Integer.valueOf(new String(code)); if (0 != retCode) { throw new SydApiException(retCode); } // 只有类型是 3 时有数据结果 - if (3 == dataType) { + if ( 3 == dataType ){ int isLast = bb.get(); byte[] dataLenArr = new byte[2]; bb.get(dataLenArr); @@ -7043,10 +6954,11 @@ public class SydApi4j implements SydApi { } + + /** * SM2 解密(支持大包分段) - * - * @param privateKey 私钥字节数组 + * @param privateKey 私钥字节数组 * @param pCipherData 密文数据 * @return 解密后的明文 */ @@ -7101,11 +7013,10 @@ public class SydApi4j implements SydApi { /** * 底层解密通信(与加密结构对称,魔法数改为 "7G") - * - * @param dataType 1-传入密文块,2-传入私钥,3-取明文块 - * @param privateKey 私钥(dataType=2时使用) + * @param dataType 1-传入密文块,2-传入私钥,3-取明文块 + * @param privateKey 私钥(dataType=2时使用) * @param pCipherData 密文块(dataType=1时使用) - * @param idx 偏移量索引 + * @param idx 偏移量索引 * @return MutiReturn7 包含返回码、isLast标志及数据片 */ private MutiReturn7 SYD_SM2Decrypt(int dataType, byte[] privateKey, ByteBuffer pCipherData, int idx) { @@ -7176,8 +7087,8 @@ public class SydApi4j implements SydApi { Proto77 proto = new Proto77(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -7198,10 +7109,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7215,8 +7123,8 @@ public class SydApi4j implements SydApi { Proto77 proto = new Proto77(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -7237,10 +7145,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7257,8 +7162,8 @@ public class SydApi4j implements SydApi { byte[] pOrgData = SYMUtil.base64EnData2DerEnData(enData); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -7280,9 +7185,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7296,8 +7199,8 @@ public class SydApi4j implements SydApi { Proto78 proto = new Proto78(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -7319,10 +7222,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7340,8 +7240,8 @@ public class SydApi4j implements SydApi { byte[] pOrgData = SYMUtil.base64EnData2DerEnData(enData); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -7362,10 +7262,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7391,8 +7288,8 @@ public class SydApi4j implements SydApi { byte[] pOrgData = SYMUtil.base64EnData2DerEnDataC1C2C3(enData); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -7413,9 +7310,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7436,7 +7331,7 @@ public class SydApi4j implements SydApi { * @param cdn 证书DN * @param userId 用户ID * @param msg 原始数据,二进制的消息块 - * @return SM3哈希值 + * @return */ private byte[] SM3Hash(String publicKey, String csn, String cdn, String userId, byte[] msg) { @@ -7627,7 +7522,10 @@ public class SydApi4j implements SydApi { // 最后一块正好为空的情况 - boolean hard = msg.length == 0 || 3 == block; + boolean hard = false; + if (msg.length == 0 || 3 == block) { + hard = true; + } byte packtype; int i = 0; @@ -7779,9 +7677,9 @@ public class SydApi4j implements SydApi { Proto72 proto = new Proto72(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("publicKey", Util.hexString2Bytes(publicKey)); data.put("lmk", "X"); packet.put("data", data); @@ -7803,9 +7701,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7831,9 +7727,9 @@ public class SydApi4j implements SydApi { Proto72 proto = new Proto72(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("pFlag", "R"); data.put("publicKey", Util.hexString2Bytes(publicKey)); data.put("lmk", "L"); @@ -7856,9 +7752,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7884,7 +7778,8 @@ public class SydApi4j implements SydApi { */ @Override public RetWrap genDigitalEnvelope(String publicKey) { - return SM2GenSessionKey(publicKey); + RetWrap retWrap = SM2GenSessionKey(publicKey); + return retWrap; } /** @@ -7907,11 +7802,12 @@ public class SydApi4j implements SydApi { * @param inPutDataStr 报文明文 * @param msgId 设备认证内容 * @return 格式:sessionByPk(base64)+\r\n+头(base64)+\r\n+密文(base64) + * @throws UnsupportedEncodingException */ @Override public String genDigitalEnvelopeWithDM(String keyValue, String inPutDataStr, String msgId) throws UnsupportedEncodingException { String separator = "\r\n"; //分隔符 - byte[] inPutData; + byte[] inPutData = null; if (null != builder) { inPutData = inPutDataStr.getBytes(builder.getEncode()); } else { @@ -7947,7 +7843,7 @@ public class SydApi4j implements SydApi { @Override public String genDigitalEnvelopeWithData(String keyValue, String inPutDataStr) throws UnsupportedEncodingException { String separator = "\r\n"; //分隔符 - byte[] inPutData; + byte[] inPutData = null; if (null != builder) { inPutData = inPutDataStr.getBytes(builder.getEncode()); } else { @@ -7968,9 +7864,11 @@ public class SydApi4j implements SydApi { RetWrap retWrap1 = SYMEnDeData(true, 1, sessionByLMK, 1, 1, inPutData, null, 0); byte[] ret = (byte[]) retWrap1.get("msg_all"); - return Util.encodeBase64(Util.hexString2Bytes(sessionByPk_HEX)) + - separator + - Util.encodeBase64(ret); + StringBuilder sb = new StringBuilder(); + sb.append(Util.encodeBase64(Util.hexString2Bytes(sessionByPk_HEX))); + sb.append(separator); + sb.append(Util.encodeBase64(ret)); + return sb.toString(); } @@ -7999,7 +7897,7 @@ public class SydApi4j implements SydApi { if (null != builder) { return new String((byte[]) retWrap.get("msg_all"), builder.getEncode()); } - return new String((byte[]) retWrap.get("msg_all"), StandardCharsets.UTF_8); + return new String((byte[]) retWrap.get("msg_all"), "UTF-8"); } @Override @@ -8007,6 +7905,8 @@ public class SydApi4j implements SydApi { return "X12345678123456781234567812345678"; } + ; + @Override public RetWrap SM2ConfirmSessionKeyC(String userId, String cipherKey) { @@ -8020,9 +7920,9 @@ public class SydApi4j implements SydApi { Proto73 proto = new Proto73(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); //传全9的情况,私钥需要自己传 //data.put("privateKeyTag", "999999999999999999999999999"); @@ -8050,9 +7950,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -8207,13 +8105,14 @@ public class SydApi4j implements SydApi { * @param msg 消息数据 * @param iv iv * @param block 消息块号 + * @return */ // @Override public RetWrap SM4EncDecDataCBC(Boolean isEn, int keyType, String key, int inType, int outType, byte[] msg, byte[] iv, int block) { // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); // 确认算法 if (isEn) { @@ -8243,7 +8142,9 @@ public class SydApi4j implements SydApi { packet.put("data", data); - return repackEncrypt(packet, !isEn); + RetWrap retWrap = repackEncrypt(packet, !isEn); + + return retWrap; } /** @@ -8263,8 +8164,8 @@ public class SydApi4j implements SydApi { public byte[] SM4EncryptDataCBC(int keyType, String key, int inType, int outType, byte[] msg) { // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); byte[] iv = new byte[32]; Arrays.fill(iv, (byte) 0x30); @@ -8300,7 +8201,7 @@ public class SydApi4j implements SydApi { */ // 测试通过 // @Override - private boolean _SM4DecryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) { + private boolean _SM4DecryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) throws UnsupportedEncodingException { byte[] msg = FileUtil.getDataFromFile(inFilePath); @@ -8424,9 +8325,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("alg$option", 0x9); @@ -8461,8 +8362,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); byte[] iv = new byte[0 == nAlg ? 16 : 32]; Arrays.fill(iv, (byte) 0x30); @@ -8510,9 +8411,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); switch (nAlg) { case 0: @@ -8547,8 +8448,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender("7E"); Proto7E proto = new Proto7E(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8571,9 +8472,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -8605,7 +8504,7 @@ public class SydApi4j implements SydApi { } - interface PackCallBack { + static interface PackCallBack { void circle(int packSn, HashMap packet, Map ret); } @@ -8615,8 +8514,8 @@ public class SydApi4j implements SydApi { Proto8024 proto = new Proto8024(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8651,9 +8550,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -8700,8 +8597,8 @@ public class SydApi4j implements SydApi { Proto8026 proto = new Proto8026(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8732,9 +8629,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -8762,8 +8657,8 @@ public class SydApi4j implements SydApi { ProtoQ1 proto = new ProtoQ1(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8807,9 +8702,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -8836,8 +8729,8 @@ public class SydApi4j implements SydApi { ProtoQ2 proto = new ProtoQ2(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8882,10 +8775,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; - + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -8909,8 +8799,8 @@ public class SydApi4j implements SydApi { ProtoMAXF proto = new ProtoMAXF(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8923,11 +8813,9 @@ public class SydApi4j implements SydApi { data.put("TAK", TAK); - StringBuilder infoDataBuilder = new StringBuilder(infoData); - while (infoDataBuilder.length() % 16 != 0) { - infoDataBuilder.append("00"); + while (infoData.length() % 16 != 0) { + infoData += "00"; } - infoData = infoDataBuilder.toString(); data.put("MACData", infoData); // 渲染发送 @@ -8946,9 +8834,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -8976,8 +8862,8 @@ public class SydApi4j implements SydApi { ProtoMU proto = new ProtoMU(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9009,9 +8895,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9051,7 +8935,7 @@ public class SydApi4j implements SydApi { // 获取协议 synchronized (this) { -// int packNo = 0; + int packNo = 0; for (int i = 0; i < loop; i++) { if (0 == i) { @@ -9119,7 +9003,7 @@ public class SydApi4j implements SydApi { } else { // 中间包 -// packNo = 2; + packNo = 2; byte[] msg = new byte[packSize]; System.arraycopy(data, i * packSize, msg, 0, packSize); println("\n\t中间包"); @@ -9149,8 +9033,8 @@ public class SydApi4j implements SydApi { ProtoMA proto = new ProtoMA(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9179,9 +9063,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9203,6 +9085,7 @@ public class SydApi4j implements SydApi { * @param fullMode 填充类型,0-弱填充 1 强填充 * @param infoData MAC数据 * @param mac MAC值 hex 格式 + * @return */ public void MACCheck(int alg, int keyType, String AK, int fullMode, byte[] infoData, String mac) { ProtoMC proto = new ProtoMC(); @@ -9219,8 +9102,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9250,9 +9133,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9260,6 +9141,7 @@ public class SydApi4j implements SydApi { throw new SydApiException(error.getInfo(), error.autoInt()); } + return; } /** @@ -9277,8 +9159,8 @@ public class SydApi4j implements SydApi { ProtoYA proto = new ProtoYA(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9328,9 +9210,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9344,14 +9224,16 @@ public class SydApi4j implements SydApi { @Override public byte[] SYD_Encrypt_Data(int alg, String DEK, byte[] infoData, byte[] vector, int disperseAlg, byte[] factor) { String ss = SYD_Disperse(DEK, 0, 1, factor, disperseAlg, 0); - return Encrypt_Data(alg, 1, 2, ss, infoData, vector); + byte[] bytes = Encrypt_Data(alg, 1, 2, ss, infoData, vector); + return bytes; } @Override public byte[] SYD_Decrypt_Data(int alg, String DEK, byte[] infoData, byte[] vector, int disperseAlg, byte[] factor) { String ss = SYD_Disperse(DEK, 0, 1, factor, disperseAlg, 0); - return Decrypt_Data(alg, 1, 2, ss, infoData, vector); + byte[] bytes = Decrypt_Data(alg, 1, 2, ss, infoData, vector); + return bytes; } @@ -9374,7 +9256,8 @@ public class SydApi4j implements SydApi { ss = ss.substring(1, 17); } - return MAC_Data(ss, infoData, IV); + byte[] bytes = MAC_Data(ss, infoData, IV); + return bytes; } @@ -9383,7 +9266,6 @@ public class SydApi4j implements SydApi { try { InputStream is = SydApi.class.getClassLoader().getResourceAsStream("racal.sunyard.main.version"); - assert is != null; byte[] buff = new byte[is.available()]; is.read(buff); return new String(buff).trim(); @@ -9409,14 +9291,14 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); - String keyTpye; + String keyTpye = ""; if (0 == Alg) { data.put("alg$option", 0); @@ -9460,10 +9342,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; - + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9490,14 +9369,14 @@ public class SydApi4j implements SydApi { Proto4D43 proto = new Proto4D43(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); - String keyTpye; + String keyTpye = ""; if (0 == Alg) { data.put("alg$option", 0); @@ -9543,10 +9422,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; - + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9567,8 +9443,8 @@ public class SydApi4j implements SydApi { public RetWrap getStatus() { ProtoHI proto = new ProtoHI(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9589,9 +9465,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection cpu = (PacketSection) ret.get("cpu"); PacketSection mem = (PacketSection) ret.get("mem"); PacketSection disk = (PacketSection) ret.get("disk"); @@ -9618,8 +9492,8 @@ public class SydApi4j implements SydApi { public RetWrap genAndPrint(String keyType, String solution) { ProtoA2 proto = new ProtoA2(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9646,9 +9520,7 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9678,8 +9550,8 @@ public class SydApi4j implements SydApi { public void setPrintFormat(String format) { ProtoPA proto = new ProtoPA(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9703,9 +9575,7 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9715,6 +9585,7 @@ public class SydApi4j implements SydApi { throw new SydApiException(error.getInfo(), error.autoInt()); } + return; } @Override @@ -9722,8 +9593,8 @@ public class SydApi4j implements SydApi { ProtoNE proto = new ProtoNE(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9753,10 +9624,7 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; - + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9793,8 +9661,8 @@ public class SydApi4j implements SydApi { ProtoA8 proto = new ProtoA8(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9822,9 +9690,7 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9853,8 +9719,8 @@ public class SydApi4j implements SydApi { ProtoA6 proto = new ProtoA6(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9882,9 +9748,7 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9923,8 +9787,8 @@ public class SydApi4j implements SydApi { ProtoMA proto = new ProtoMA(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9953,10 +9817,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9992,8 +9853,8 @@ public class SydApi4j implements SydApi { ProtoB0 proto = new ProtoB0(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -10018,10 +9879,7 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -10054,8 +9912,8 @@ public class SydApi4j implements SydApi { public RetWrap changZMK2LMK(String zmki, String zmk) { ProtoBY proto = new ProtoBY(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -10074,16 +9932,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10127,8 +9982,8 @@ public class SydApi4j implements SydApi { public RetWrap getVersion(String num) { ProtoHV proto = new ProtoHV(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -10152,9 +10007,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection name = (PacketSection) ret.get("name"); PacketSection model = (PacketSection) ret.get("model"); PacketSection equipNo = (PacketSection) ret.get("equipNo"); @@ -10184,8 +10037,8 @@ public class SydApi4j implements SydApi { */ public RetWrap calcHash(String key, int hashType, String contextInfo, byte[] infoData) { Proto82 proto = new Proto82(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -10201,15 +10054,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10241,9 +10092,9 @@ public class SydApi4j implements SydApi { Proto68 proto = new Proto68(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("DEK", DEK.getBytes()); @@ -10272,9 +10123,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10299,9 +10148,9 @@ public class SydApi4j implements SydApi { Proto69 proto = new Proto69(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("alg$option", 1); data.put("fillKey$option", 0); @@ -10330,9 +10179,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10357,9 +10204,9 @@ public class SydApi4j implements SydApi { Proto62 proto = new Proto62(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("keyType", keyType); data.put("key", key); @@ -10385,9 +10232,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10412,9 +10257,9 @@ public class SydApi4j implements SydApi { Proto63 proto = new Proto63(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("keyType", keyType); data.put("key", key); @@ -10441,12 +10286,12 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; - + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); - return "00".equals(error.getString()); + if (!"00".equals(error.getString())) { + return false; + } + return true; } /** @@ -10464,9 +10309,9 @@ public class SydApi4j implements SydApi { Proto64 proto = new Proto64(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("keyType", keyType); data.put("key", key); @@ -10493,10 +10338,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; - + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10523,9 +10365,9 @@ public class SydApi4j implements SydApi { Proto65 proto = new Proto65(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("keyType", keyType); data.put("srcKey", srcKey); @@ -10551,9 +10393,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10577,9 +10417,9 @@ public class SydApi4j implements SydApi { Proto66 proto = new Proto66(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("key", key); data.put("AlgorithmKey", "P"); @@ -10604,9 +10444,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10629,9 +10467,9 @@ public class SydApi4j implements SydApi { Proto67 proto = new Proto67(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("srcKey", srcKey); data.put("aimKey", aimKey); @@ -10656,14 +10494,12 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); } -// RetWrap rw = new RetWrap(); + RetWrap rw = new RetWrap(); return ((PacketSection) ret.get("info")).getBytes(); } @@ -10679,9 +10515,9 @@ public class SydApi4j implements SydApi { Proto60 proto = new Proto60(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); // 渲染发送 耗时长 @@ -10699,9 +10535,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10726,9 +10560,9 @@ public class SydApi4j implements SydApi { Proto71 proto = new Proto71(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("publicKey", pk); data.put("key", key); @@ -10750,9 +10584,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10775,8 +10607,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender("E2"); ProtoE4 proto = new ProtoE4(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -10797,7 +10629,7 @@ public class SydApi4j implements SydApi { //ProtocolParser parser = pset.getProtocolResParse(); - Map ret; + Map ret = null; Packet p = proto.parse(bb); ret = p.toMap(); @@ -10807,9 +10639,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10829,7 +10659,7 @@ public class SydApi4j implements SydApi { */ public byte[] itHash(int digestAlg, byte[] orgData) { //同步加密机与裸签摘要算法枚举值 - if (!(digestAlg == 1 || digestAlg == 2 || digestAlg == 3)) { + if(!(digestAlg==1||digestAlg==2||digestAlg==3)){ throw new IllegalArgumentException("不支持的算法:" + digestAlg); } int hashType = 0; @@ -10884,8 +10714,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap countHash(int hashType, int packSn, byte[] msg, byte[] filterData) { Proto7D proto = new Proto7D(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -10900,15 +10730,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10927,15 +10755,16 @@ public class SydApi4j implements SydApi { /** * 10.1装载对称密钥至用户存储区(=) -> pass * - * @param index 索引地址,指示用户密钥存储区的27位地址。 - * @param key 被加密的密钥 + * @param index 索引地址,指示用户密钥存储区的27位地址。 + * @param key 被加密的密钥 + * @param checkValue 校验值 * @return 是否成功 */ // 测试通过 - public Boolean loadKey(String index, String key) { + public Boolean loadKey(String index, String key, String checkValue) { ProtoLB proto = new ProtoLB(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -10966,15 +10795,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10993,8 +10820,8 @@ public class SydApi4j implements SydApi { @Override public RetWrap readKey(String index) { ProtoLF proto = new ProtoLF(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11008,15 +10835,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -11040,12 +10865,13 @@ public class SydApi4j implements SydApi { * @param index 索引地址,指示用户密钥存储区的27位地址 * @param key 被加密的密钥 * @param checkValue 校验值 + * @return */ // 测试通过 public Boolean updateKey(String index, String key, String checkValue) { ProtoLG proto = new ProtoLG(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11082,16 +10908,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -11118,8 +10941,8 @@ public class SydApi4j implements SydApi { // 测试通过 public byte[] switchEncryY(String srcType, int sca, String desKeyType, int dca, int roundModel, String sDEK, String dDEK, byte[] infoData, String vector) { ProtoY0 proto = new ProtoY0(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11142,15 +10965,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -11164,6 +10985,10 @@ public class SydApi4j implements SydApi { /** * 自动 4k 分包 * + * @param keyType + * @param mode + * @param key + * @param msg * @return MAC 值 */ public String genMAB(int mode, int keyType, String key, byte[] msg) { @@ -11178,8 +11003,8 @@ public class SydApi4j implements SydApi { return new String((byte[]) retWrap.get("MAB")); } - int loop; - int tailLen; + int loop = 0; + int tailLen = 0; loop = msg.length / blockSize; tailLen = msg.length % blockSize; @@ -11190,7 +11015,7 @@ public class SydApi4j implements SydApi { // loop 至少是 2 // 分包数据 - byte[] data; + byte[] data = null; // 首包 // 必然有 @@ -11235,8 +11060,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap genMAB(int packSn, int keyType, int mode, String key, byte[] iv, byte[] msg) { ProtoMS proto = new ProtoMS(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11257,15 +11082,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -11286,8 +11109,8 @@ public class SydApi4j implements SydApi { // 测试通过 public String genPIN(String account) { ProtoJA proto = new ProtoJA(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11303,16 +11126,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -11329,13 +11149,14 @@ public class SydApi4j implements SydApi { * @param account 账号,账号中去除校验位的最右 12 位 * @param encryptionAlg 加密算法 0:DES, 1:3DES(默认), 2:SM4 * @param pinBlock PIN 块,LMK 对(02-03)下加密的导出 PINBLOCK。(PINBLOCK格式默认为 01 格式) + * @return */ public Boolean decryPINBAndPrint(char fileType, String account, int encryptionAlg, String pinBlock) { ProtoPE proto = new ProtoPE(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11367,10 +11188,7 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11388,6 +11206,8 @@ public class SydApi4j implements SydApi { * @param zpk2 目的 ZPK,将要加密 PIN 块的 ZPK;LMK 对(06-07)下加密 * @param sca 源算法 SCA,1:3DES, 2:SM4 * @param dca 目的算法DCA, 1:3DES, 2:SM4 + * @param spt + * @param dpt * @param pin 源 PIN 块,源 ZPK 下加密的源 PIN 块 * @param account 账号中去除校验位的最右 12 位 * @return 目的 ZPK 下加密的目的 PIN 块 @@ -11399,8 +11219,8 @@ public class SydApi4j implements SydApi { ProtoCC proto = new ProtoCC(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11433,10 +11253,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11465,8 +11282,8 @@ public class SydApi4j implements SydApi { ProtoJE proto = new ProtoJE(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11496,10 +11313,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11526,8 +11340,8 @@ public class SydApi4j implements SydApi { ProtoJG proto = new ProtoJG(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11557,9 +11371,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11588,8 +11400,8 @@ public class SydApi4j implements SydApi { ProtoCA proto = new ProtoCA(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11622,9 +11434,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11652,8 +11462,8 @@ public class SydApi4j implements SydApi { ProtoJC proto = new ProtoJC(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11683,9 +11493,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11710,8 +11518,8 @@ public class SydApi4j implements SydApi { ProtoBA proto = new ProtoBA(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11736,9 +11544,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11764,8 +11570,8 @@ public class SydApi4j implements SydApi { ProtoNG proto = new ProtoNG(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11790,9 +11596,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11819,8 +11623,8 @@ public class SydApi4j implements SydApi { */ public String transPinFrOneKeyToAno(String srcKeyType, String desKeyType, String srcKey, String desKey, int sca, int dca, String sPin, String primaryAccount) { ProtoCB proto = new ProtoCB(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11844,15 +11648,14 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); + ret = (Map) ret.get("data"); + PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); } else { @@ -11873,8 +11676,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap genTAK(String tmk) { ProtoHA proto = new ProtoHA(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11892,15 +11695,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -11937,8 +11738,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap transTAKFromZMKToLMK(String zmk, String tak) { ProtoMF proto = new ProtoMF(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11957,15 +11758,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -11994,8 +11793,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap transTAKFromLMKToZMK(String zmk, String tak) { ProtoMG proto = new ProtoMG(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12014,15 +11813,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12051,8 +11848,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap transTAKFromLMKToTMK(String tmk, String tak) { ProtoAG proto = new ProtoAG(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12071,7 +11868,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -12107,8 +11904,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap genKeyByTMKOrTPKOrPVK(String tmk) { ProtoHC proto = new ProtoHC(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12127,7 +11924,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -12171,8 +11968,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranKeyByTMKToAno(String tmkNow, String tmkStorage) { ProtoAE proto = new ProtoAE(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12191,7 +11988,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -12226,8 +12023,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranKeyByTMKFromZMKToLMK(String zmk, String tmk) { ProtoFC proto = new ProtoFC(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12247,7 +12044,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -12282,8 +12079,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranKeyByTMKFromLMKToZMK(String zmk, String tmk) { ProtoFE proto = new ProtoFE(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12303,15 +12100,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12341,8 +12136,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap genZEKOrZAK(int key, String zmk) { ProtoFI proto = new ProtoFI(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12362,15 +12157,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12424,8 +12217,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranZEAKFromZMKToLMK(int key, String zmk, String zekOrzak) { ProtoFK proto = new ProtoFK(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12446,15 +12239,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12493,8 +12284,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranZEAKFromLMKToZMK(int key, String zmk, String zekOrzak) { ProtoFM proto = new ProtoFM(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12515,7 +12306,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -12559,8 +12350,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap genZPK(String zmk) { ProtoIA proto = new ProtoIA(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12579,15 +12370,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12625,8 +12414,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranZPKFromZMKToLMK(String zmk, String zpk) { ProtoFA proto = new ProtoFA(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12646,7 +12435,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -12682,8 +12471,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranZPKFromLMKToZMK(String zmk, String zpk) { ProtoGC proto = new ProtoGC(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12703,15 +12492,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12739,8 +12526,8 @@ public class SydApi4j implements SydApi { public RetWrap genZMKAndPrint() { ProtoOC proto = new ProtoOC(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -12769,9 +12556,7 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -12804,8 +12589,8 @@ public class SydApi4j implements SydApi { public RetWrap composeKey(String keyType, String[] keys) { ProtoA4 proto = new ProtoA4(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); int len = 0; for (String key : keys) { @@ -12838,15 +12623,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12875,8 +12658,8 @@ public class SydApi4j implements SydApi { */ public RetWrap roleVerify(String roleInfo) { Proto61 proto = new Proto61(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12889,15 +12672,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12919,9 +12700,9 @@ public class SydApi4j implements SydApi { Proto73 proto = new Proto73(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); //String sk = "00010000C530EFBC1971D6526BDAB640370EE9197BFB0D78F88417C1ED49981FFEF226980000000000000000000000000000000000000000000000000000000000000000"; //data.put("privateKey", Util.hexString2Bytes(sk)); @@ -12946,9 +12727,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12979,9 +12758,9 @@ public class SydApi4j implements SydApi { ProtoAB proto = new ProtoAB(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("keyType", "002"); data.put("key", key.getBytes()); @@ -13005,9 +12784,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -13058,22 +12835,24 @@ public class SydApi4j implements SydApi { @Override public String SYMEncrytByKeyIndex(String index, String intPutData) { RetWrap retWrap = this.SYMEnDeData(true, 1, new String((byte[]) this.readKey(index).get("key")), 1, 1, intPutData.getBytes(), null, 0); - return Util.bytes2HexString((byte[]) retWrap.get("msg_all")); + String msg_all_en = Util.bytes2HexString((byte[]) retWrap.get("msg_all")); + return msg_all_en; } @Override public String SYMDecrytByKeyIndex(String index, String intPutData) { RetWrap retWrap = this.SYMEnDeData(false, 1, new String((byte[]) this.readKey(index).get("key")), 1, 1, Util.hexString2Bytes(intPutData), null, 0); - return new String(Util.hexString2Bytes(Util.bytes2HexString((byte[]) retWrap.get("msg_all")))); + String msg_all_de = new String(Util.hexString2Bytes(Util.bytes2HexString((byte[]) retWrap.get("msg_all")))); + return msg_all_de; } // 测试通过 @Override public boolean setDevCert(String equipType, String equipNo, byte[] equipSk, byte[] equipPk, byte[] manuPk, byte[] authSign) { Proto90 proto = new Proto90(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13095,9 +12874,7 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if ("00".equals(error.getString())) { return true; @@ -13111,8 +12888,8 @@ public class SydApi4j implements SydApi { @Override public boolean changeDevCert(byte[] equipSk, byte[] equipPk, byte[] authSign) { Proto91 proto = new Proto91(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13131,9 +12908,7 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if ("00".equals(error.getString())) { return true; @@ -13156,8 +12931,8 @@ public class SydApi4j implements SydApi { @Override public RetWrap getDeviceCertiInfo() { Proto92 proto = new Proto92(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13178,9 +12953,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); RetWrap retWrap = new RetWrap(); if ("00".equals(error.getString())) { @@ -13203,8 +12976,8 @@ public class SydApi4j implements SydApi { @Override public String deviceSignC(byte[] orgData) { Proto97 proto = new Proto97(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13236,8 +13009,8 @@ public class SydApi4j implements SydApi { public boolean setToken(byte[] token) { Proto98 proto = new Proto98(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13254,9 +13027,7 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if ("00".equals(error.getString())) { return true; @@ -13270,8 +13041,8 @@ public class SydApi4j implements SydApi { @Override public byte[] calcAuthHead(byte[] certiContent) { Proto99 proto = new Proto99(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13293,9 +13064,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); // PacketSection equipType = (PacketSection) ret.get( "equipType" ); // PacketSection equipNo = (PacketSection) ret.get( "equipNo" ); // RetWrap retWrap = new RetWrap(); @@ -13330,7 +13099,7 @@ public class SydApi4j implements SydApi { } private RetWrap attachedVerifySoftGetAll(byte[] sign) { - RetWrap ret; + RetWrap ret = null; try { ret = ASN1Util.takeOutOriginDataFromPKCS7Sign(new String(sign)); @@ -13359,7 +13128,7 @@ public class SydApi4j implements SydApi { } private boolean attachedVerifySoft(String sign) { - RetWrap ret; + RetWrap ret = null; try { ret = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); } catch (IOException e) { @@ -13375,7 +13144,7 @@ public class SydApi4j implements SydApi { } public boolean attachedVerifyRSA(int rFlag, String sign) { - RetWrap ret; + RetWrap ret = null; boolean verifyRSA = false; try { ret = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); @@ -13389,11 +13158,11 @@ public class SydApi4j implements SydApi { // byte[] hash = this.SM3Hash( Util.bytes2HexString( pk ), orgData ); String sign2 = (String) ret.get("sign"); if (orgData.length < 4096) { - verifyRSA = this.detachedVerify(rFlag, 1, orgData, sign2); - } else { - // RetWrap hashData = this.countHash(0, 0, orgData, null); + verifyRSA= this.detachedVerify(rFlag, 1, orgData, sign2); + }else { + // RetWrap hashData = this.countHash(0, 0, orgData, null); byte[] hashData = itHash(rFlag, orgData); - verifyRSA = this.detachedVerify(rFlag, 0, hashData, sign2); + verifyRSA= this.detachedVerify(rFlag, 0, hashData, sign2); } return verifyRSA; } @@ -13410,50 +13179,50 @@ public class SydApi4j implements SydApi { @Override public String attachedSign(byte[] orgData, String sCertDN) { try { - if (orgData.length > 4096) { - return attachedSignSoft(orgData, sCertDN); + if ( orgData.length > 4096 ) { + return attachedSignSoft( orgData, sCertDN ); } else { - return attachedSignHsm(orgData, sCertDN); + return attachedSignHsm( orgData, sCertDN ); } - } catch (UnsupportedEncodingException e) { + }catch (UnsupportedEncodingException e) { throw new IllegalArgumentException("DN is not in GBK"); } } @Override - public String attachedSignRSA(int rFlag, byte[] orgData, String sCertDN) { + public String attachedSignRSA(int rFlag,byte[] orgData, String sCertDN) { //return attachedSignRSA( rFlag, orgData, sCertDN ); try { - if (orgData.length > 4096) { - return attachedSignRSASoft(rFlag, orgData, sCertDN); + if ( orgData.length > 4096 ) { + return attachedSignRSASoft( rFlag,orgData, sCertDN ); } else { - return attachedSignRSAHsm(rFlag, orgData, sCertDN); + return attachedSignRSAHsm( rFlag,orgData, sCertDN ); } - } catch (UnsupportedEncodingException e) { + }catch (UnsupportedEncodingException e) { throw new IllegalArgumentException("DN is not in GBK"); } } - public String attachedSignRSASoft(int rFlag, byte[] orgData, String sCertDN) throws UnsupportedEncodingException { - sCertDN = verifyDn(sCertDN); + public String attachedSignRSASoft(int rFlag,byte[] orgData, String sCertDN) throws UnsupportedEncodingException { + sCertDN = verifyDn( sCertDN ); try { // RetWrap hashData = countHash(0, 0, orgData, null); // byte[] hash = (byte[]) hashData.get("hash"); byte[] hash = itHash(rFlag, orgData); - String sign = this.detachedSign(rFlag, 0, hash, sCertDN); - return ASN1Util.addOriginDataToPKCS7Sign(orgData, sign); + String sign = this.detachedSign(rFlag,0, hash, sCertDN); + return ASN1Util.addOriginDataToPKCS7Sign( orgData, sign ); } catch (IOException e) { e.printStackTrace(); throw new IllegalArgumentException("解析 pkcs7 签名格式时错误"); } } - public String attachedSignRSAHsm(int rFlag, byte[] orgData, String sCertDN) { - sCertDN = verifyDn(sCertDN); + public String attachedSignRSAHsm(int rFlag,byte[] orgData, String sCertDN) { + sCertDN = verifyDn( sCertDN ); Proto8018 proto = new Proto8018(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); PacketSN sn = PacketSN.gen(); byte[] dn = new byte[256]; @@ -13464,12 +13233,11 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } - assert s != null; System.arraycopy(s, 0, dn, 0, s.length); // 填充数据 - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); packet.put("header", sn.getSn().array()); @@ -13501,9 +13269,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -13515,11 +13281,13 @@ public class SydApi4j implements SydApi { } + + public String attachedSignHsm(byte[] orgData, String sCertDN) { - sCertDN = verifyDn(sCertDN); + sCertDN = verifyDn( sCertDN ); Proto8018 proto = new Proto8018(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); PacketSN sn = PacketSN.gen(); byte[] dn = new byte[256]; @@ -13530,12 +13298,11 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } - assert s != null; System.arraycopy(s, 0, dn, 0, s.length); // 填充数据 - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); packet.put("data", data); packet.put("header", sn.getSn().array()); data.put("DN", dn); @@ -13557,9 +13324,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -13571,15 +13336,15 @@ public class SydApi4j implements SydApi { } public boolean attachedVerifyHsm(String sign) { - return attachedVerify(sign.getBytes()); + return attachedVerify( sign.getBytes() ); } @Override public boolean attachedVerify(String sign) { - if (sign.length() > 4096) { - return attachedVerifySoft(sign); + if ( sign.length() > 4096 ) { + return attachedVerifySoft( sign ); } else { - return attachedVerify(sign.getBytes()); + return attachedVerify( sign.getBytes() ); } } @@ -13588,8 +13353,8 @@ public class SydApi4j implements SydApi { Proto8019 proto = new Proto8019(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); PacketSN sn = PacketSN.gen(); packet.put("data", data); @@ -13615,14 +13380,12 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { SydApiException e = new SydApiException(error.getInfo(), error.autoInt()); - println("验签失败=" + e); + println("验签失败=" + e.toString()); return false; } @@ -13632,7 +13395,7 @@ public class SydApi4j implements SydApi { @Override public byte[] attachedVerifyRD(String sign) { - return attachedVerifyRD(sign.getBytes()); + return attachedVerifyRD( sign.getBytes() ); } @Override @@ -13652,11 +13415,11 @@ public class SydApi4j implements SydApi { * @return 返回CERT_INFO结构的证书数据 */ public X509 attachedVerifyAndGetX509(String sign) { - return attachedVerifyAndGetX509(sign.getBytes()); + return attachedVerifyAndGetX509( sign.getBytes() ); } public X509 attachedVerifyAndGetX509(byte[] sign) { - RetWrap retWrap = attachedVerifyAndGetAll(sign); + RetWrap retWrap = attachedVerifyAndGetAll( sign); if (retWrap == null) return null; @@ -13664,17 +13427,17 @@ public class SydApi4j implements SydApi { } public RetWrap attachedVerifyAndGetAll(String sign) { - return attachedVerifyAndGetAll(sign.getBytes()); + return attachedVerifyAndGetAll( sign.getBytes() ); } // 大数据兼容处理 public RetWrap attachedVerifyAndGetAll(byte[] sign) { - if (sign.length > 4096) { + if( sign.length > 4096 ) { - return attachedVerifySoftGetAll(sign); + return attachedVerifySoftGetAll( sign ); } else { - return attachedVerifyAndGetAllHsm(sign); + return attachedVerifyAndGetAllHsm( sign ); } } @@ -13683,8 +13446,8 @@ public class SydApi4j implements SydApi { Proto8019 proto = new Proto8019(); // 填充数据 - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13708,16 +13471,13 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { return null; } - PacketSection info = (PacketSection) ret.get("info"); byte[] pCertInfo = info.getBytes(); @@ -13732,7 +13492,7 @@ public class SydApi4j implements SydApi { RetWrap retWrap = new RetWrap(); retWrap.put("x509", x509); - retWrap.put("oData", oDataB); + retWrap.put("oData", oDataB ); return retWrap; } @@ -13753,15 +13513,15 @@ public class SydApi4j implements SydApi { * @param inputData 输入数据 * @return byte[] 数据 * @author wp - * @Description:(A0) 对称加密接口 + * @Description:(A0)对称加密接口 * @date 2021/5/31 */ // 测试通过 @Override public byte[] SYMEncryt(int storageType, String key, int algFlag, byte[] iv, int divNum, List divData, int[] divAlgFlag, List divIv, byte[] inputData) { ProtoS0 proto = new ProtoS0(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13781,7 +13541,7 @@ public class SydApi4j implements SydApi { // throw new SydApiException("输入的数据格式错误", 21); // } String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8); + gId = gId.substring(gId.length() - 8, gId.length()); data.put("keyIndex", "0000000000000000000" + gId); } //算法类型支持算法:3DES、AES、SM1、SM4 @@ -13825,9 +13585,7 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { @@ -13859,7 +13617,7 @@ public class SydApi4j implements SydApi { } if (0 == storageType) { String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8); + gId = gId.substring(gId.length() - 8, gId.length()); // 密钥索引 bb.put(("0000000000000000000" + gId).getBytes()); } @@ -13959,8 +13717,8 @@ public class SydApi4j implements SydApi { @Override public byte[] SYMDecryt(int storageType, String key, int algFlag, byte[] iv, int divNum, List divData, int[] divAlgFlag, List divIv, byte[] inputData) { ProtoS1 proto = new ProtoS1(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13980,7 +13738,7 @@ public class SydApi4j implements SydApi { // throw new SydApiException("输入的数据格式错误", 21); // } String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8); + gId = gId.substring(gId.length() - 8, gId.length()); data.put("keyIndex", "0000000000000000000" + gId); } //算法类型支持算法:3DES、AES、SM1、SM4 @@ -14024,9 +13782,8 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); + PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -14056,8 +13813,8 @@ public class SydApi4j implements SydApi { @Override public byte[] genMAC(int storageType, String key, int algFlag, byte[] iv, int divNum, List divData, int[] divAlgFlag, List divIv, byte[] inputData, int macDataLen) { ProtoS2 proto = new ProtoS2(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14077,7 +13834,7 @@ public class SydApi4j implements SydApi { // throw new SydApiException("输入的数据格式错误", 21); // } String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8); + gId = gId.substring(gId.length() - 8, gId.length()); data.put("keyIndex", "0000000000000000000" + gId); } //算法类型 @@ -14124,9 +13881,8 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); + PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -14157,7 +13913,7 @@ public class SydApi4j implements SydApi { } if (0 == storageType) { String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8); + gId = gId.substring(gId.length() - 8, gId.length()); // 密钥索引 bb.put(("0000000000000000000" + gId).getBytes()); } @@ -14253,8 +14009,8 @@ public class SydApi4j implements SydApi { @Override public byte[] genMacAndNextIv(String keyIndex, byte[] iv, int divNum, List divData, int[] divAlgFlag, List divIv, byte[] inputData) { ProtoS3 proto = new ProtoS3(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14313,9 +14069,8 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); + PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -14427,8 +14182,8 @@ public class SydApi4j implements SydApi { @Override public RetWrap genSYMkey(int storageType, int algFlag, int keyLength, String keyIndex) { ProtoS4 proto = new ProtoS4(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14453,9 +14208,7 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -14482,8 +14235,8 @@ public class SydApi4j implements SydApi { @Override public byte[] signData(int storageType, int algFlag, String key, byte[] inputData) { ProtoS5 proto = new ProtoS5(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14505,7 +14258,7 @@ public class SydApi4j implements SydApi { // throw new SydApiException("输入的数据格式错误", 21); // } String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8); + gId = gId.substring(gId.length() - 8, gId.length()); data.put("keyIndex", "0000000000000000000" + gId); } @@ -14517,15 +14270,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -14550,8 +14301,8 @@ public class SydApi4j implements SydApi { @Override public Boolean verifySignData(int storageType, int algFlag, String key, byte[] orgData, byte[] signData) { ProtoS6 proto = new ProtoS6(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14573,7 +14324,7 @@ public class SydApi4j implements SydApi { // throw new SydApiException("输入的数据格式错误", 21); // } String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8); + gId = gId.substring(gId.length() - 8, gId.length()); data.put("keyIndex", "0000000000000000000" + gId); } data.put("orgData", orgData); @@ -14585,7 +14336,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -14615,8 +14366,8 @@ public class SydApi4j implements SydApi { @Override public byte[] ASYMEnData(int storageType, int algFlag, String key, byte[] inputData) { ProtoS7 proto = new ProtoS7(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14650,7 +14401,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -14680,8 +14431,8 @@ public class SydApi4j implements SydApi { @Override public byte[] ASYMDeData(int storageType, int algFlag, String key, byte[] cipherText) { ProtoS8 proto = new ProtoS8(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14715,7 +14466,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -14777,18 +14528,18 @@ public class SydApi4j implements SydApi { if (protectedKeyDivNum != 0) { // 分散因子 - for (byte[] protectedKeyDivDatum : protectedKeyDivData) { - bb.put(protectedKeyDivDatum); + for (int i = 0; i < protectedKeyDivData.size(); i++) { + bb.put(protectedKeyDivData.get(i)); } // 分散算法标识 - for (int j : protectedKeyDivAlgFlag) { - bb.put(String.format("%02d", MyUtil.transAlg(j)).getBytes()); + for (int i = 0; i < protectedKeyDivAlgFlag.length; i++) { + bb.put(String.format("%02d", MyUtil.transAlg(protectedKeyDivAlgFlag[i])).getBytes()); } // 分散算法的初始向量 - for (byte[] bytes : protectedKeyDivIv) { - bb.put(bytes); + for (int i = 0; i < protectedKeyDivIv.size(); i++) { + bb.put(protectedKeyDivIv.get(i)); } } @@ -14863,8 +14614,8 @@ public class SydApi4j implements SydApi { private RetWrap _exportKeyAndCalc(String protectedKeyIndex, int algFlag, byte[] iv, int protectedKeyDivNum, List protectedKeyDivData, int[] protectedKeyDivAlgFlag, List protectedKeyDivIv, int exportKeyNum, List structList) { ProtoS9 proto = new ProtoS9(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14912,15 +14663,14 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); + ret = (Map) ret.get("data"); + PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); } else { @@ -14950,8 +14700,8 @@ public class SydApi4j implements SydApi { public byte[] consultKey(String keyIndex, int divNum, List divData, int[] divAlgFlag, List divIv, byte[] clientRandom, byte[] serverRandom) { ProtoSA proto = new ProtoSA(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14998,15 +14748,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -15029,8 +14777,8 @@ public class SydApi4j implements SydApi { @Override public byte[] pHash(String key, String seed, int outLen) { ProtoSB proto = new ProtoSB(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -15044,15 +14792,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -15089,8 +14835,8 @@ public class SydApi4j implements SydApi { String encryptKeyIndex, int encryptAlgFlag, byte[] encryptIv, int encryptKeyDivNum, List encryptKeyDivData, int[] encryptKeyDivAlgFlag, List encryptKeyDivIv, byte[] inputData) { ProtoSC proto = new ProtoSC(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -15178,9 +14924,8 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); + PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -15205,8 +14950,8 @@ public class SydApi4j implements SydApi { @Override public byte[] ASYMDecryptAndEn(int storageType, String key, int decryptAlgFlag, byte[] inputData, int prefixIgnoreCount, int suffixIgnoreCount) { ProtoSD proto = new ProtoSD(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -15240,9 +14985,7 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -15266,8 +15009,8 @@ public class SydApi4j implements SydApi { @Override public byte[] SYMDecryptAndEn(String decryptKey, int decryptAlgFlag, byte[] decryptIv, byte[] inputData) { ProtoSE proto = new ProtoSE(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -15288,10 +15031,7 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -15320,8 +15060,8 @@ public class SydApi4j implements SydApi { public byte[] decryptByMainAndSYMEn(String encryptKeyIndex, int encryptAlgFlag, byte[] encryptIv, int encryptKeyDivNum, List encryptKeyDivData, int[] encryptKeyDivAlgFlag, List encryptKeyDivIv, byte[] inputData) { ProtoSF proto = new ProtoSF(); - HashMap packet = new HashMap<>(); - HashMap data = new HashMap<>(); + HashMap packet = new HashMap(); + HashMap data = new HashMap(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -15360,16 +15100,13 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret; + Map ret = null; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { -// ret = (Map) ret.get("data"); - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; + ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -15386,9 +15123,9 @@ public class SydApi4j implements SydApi { //填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap<>(); + HashMap packet = new HashMap(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap<>(); + HashMap data = new HashMap(); data.put("nRandomSize", nRandomSize); packet.put("data", data); @@ -15406,9 +15143,8 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - @SuppressWarnings("unchecked") - Map dataMap = (Map) ret.get("data"); - ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); + ret = (Map) ret.get("data"); + PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); } 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); }