package com.sunyard.sge.database; import com.sun.jna.ptr.IntByReference; import com.sunyard.SydApiException; import com.sunyard.proto.Util; import com.sunyard.sge.bytes.BytesUtil; import com.sunyard.sge.database.pool.HsmLinkInfo; import com.sunyard.sge.log.LogFactory; import com.sunyard.sge.plus.ConnectHandlerManager; import com.sunyard.sge.plus.SydLibrary; import com.sunyard.sge.pool.ObjectInPool; import com.sunyard.util.SYMUtil; import org.apache.logging.log4j.Logger; import racal.sunyard.main.SydApi4j; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * 上海黄金交易所 - 数据库加密方案 */ public class SydApiBaseFunction implements SydApi { private SydApi4j[] hsms = null; private HsmLinkInfo[] linkInfos = null; private ObjectInPool oip = null; Logger log = LogFactory.getLogger(); private boolean isShortLinkMode = false; public SydApiBaseFunction() { isShortLinkMode = true; } public SydApiBaseFunction(SydApi4j[] hsms, HsmLinkInfo[] linkInfos) { this.hsms = hsms; this.linkInfos = linkInfos; } public SydApi4j[] getHsms() { return hsms; } public HsmLinkInfo[] getLinkInfos() { return linkInfos; } public void setOip(ObjectInPool oip) { this.oip = oip; } private void updateLastUseTime(){ if ( null != this.oip ) { this.oip.updateLastUseTime(); } } @Override public SydApi SYD_Short_Connect_Ex(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int allDealTimeOut) { Logger log = LogFactory.getLogger(); if (LogFactory.iLogLevel == 3) { log.debug("SYD_Connect_Ex方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList), Arrays.toString(iPortList), iConnectTimeOut, allDealTimeOut); } if (pcIpList.length != iPortList.length) { throw new IllegalArgumentException("ip与端口数量不一致"); } SydApi4j[] apis = new SydApi4j[pcIpList.length]; int iDealTimeOut = allDealTimeOut / pcIpList.length; boolean connected = false; for (int i = 0; i < pcIpList.length; i++) { try { apis[i] = (SydApi4j) new SydApi4j().connect(pcIpList[i], iPortList[i], null, iConnectTimeOut, iDealTimeOut); connected = true; } catch (Exception e) { // 忽略错误 log.error("网络连接错误 {} {} - 忽略", pcIpList[i], iPortList[i]); } } if (!connected) { throw new SydApiException("连接错误", -1); } hsms = apis; isShortLinkMode = true; return this; } public void SYD_Disconnect() { if (null == hsms) { return; } try { for (int i = 0; i < hsms.length; i++) { SydApi4j api = hsms[i]; if (null != api) { try { api.disconnect(); } catch (Exception e) { } } } } catch (Exception e) { } hsms = null; } @Override public void SYD_Disconnect_Ex() { if (isShortLinkMode) { SYD_Disconnect(); } // 配合链接池,不再需要释放。 } @Override public String SYD_SM3_Hash_ShortData(byte[] pcData) { if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM3_Hash_ShortData方法接收参数:pcData:{}", Arrays.toString(pcData)); } if (null == this.hsms || this.hsms.length < 1) { throw new SydApiException(-1); } if (null == pcData) { throw new SydApiException("输入内容为 null",19); } if ( pcData.length > 1024 ) { throw new SydApiException("输入内容为空",19); } SydApiException le = new SydApiException(-1); updateLastUseTime(); for (SydApi4j api : this.hsms) { if (null == api) { continue; } if (isShortLinkMode) { try { byte[] hash = api.SYD_SM3_LongData(pcData, null, 0); log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash)); return Util.bytes2HexString(hash); } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } else { synchronized (api) { try { byte[] hash = api.SYD_SM3_LongData(pcData, null, 0); log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash)); return Util.bytes2HexString(hash); } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } } } if ( ! this.isShortLinkMode ) { this.oip.setError(); } throw le; } @Override public LongDataReturn SYD_SM3_Hash_LongData(byte[] pcData, int iPkgNum, String pcProcData) { Logger log = LogFactory.getLogger(); if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM3_Hash_LongData 方法接收参数:pcData:{},iPkgNum:{},pcProcData:{}", Arrays.toString(pcData), iPkgNum, pcProcData); } if (null == this.hsms || this.hsms.length < 1) { throw new SydApiException(-1); } byte[] procData = null; if (2 == iPkgNum || 3 == iPkgNum) { procData = Util.hexString2Bytes(pcProcData); log.debug("Util.hexString2Bytes方法返回值:{}", Arrays.toString(procData)); } SydApiException le = new SydApiException(-1); updateLastUseTime(); for (SydApi4j api : this.hsms) { if (null == api) { continue; } if (isShortLinkMode) { try { byte[] hash = api.SYD_SM3_LongData(pcData, procData, iPkgNum); log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash)); if (0 == iPkgNum || 3 == iPkgNum) { return new LongDataReturn(null, Util.bytes2HexString(hash)); } else { return new LongDataReturn(Util.bytes2HexString(hash), null); } } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } else { synchronized (api) { try { byte[] hash = api.SYD_SM3_LongData(pcData, procData, iPkgNum); log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash)); if (0 == iPkgNum || 3 == iPkgNum) { return new LongDataReturn(null, Util.bytes2HexString(hash)); } else { return new LongDataReturn(Util.bytes2HexString(hash), null); } } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } } } if ( ! this.isShortLinkMode ) { this.oip.setError(); } throw le; } @Override public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) { Logger log = LogFactory.getLogger(); if ( iKeyIndex < 0 ) { throw new SydApiException(0x01000001); } if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData), iFlag); } if (iFlag == Consts.ECB_DEC && null == pcData) { throw new SydApiException(0x01000001); } if (null == this.hsms || this.hsms.length < 1) { throw new SydApiException(-1); } if (Consts.ECB_ENC == iFlag) { if ( 0 == pcData.length || pcData.length > 1024 ) { throw new SydApiException("输入内容长度错误",0x01000001); } } else { if ( 0 == pcData.length || pcData.length % 16 != 0 || pcData.length > 1040 ) { throw new SydApiException("输入密文数据长度错误", 0x01000014); } } updateLastUseTime(); String keyIndex = String.format("%08d", iKeyIndex); SydApiException le = new SydApiException(-1); for (SydApi4j api : this.hsms) { if (null == api) { continue; } if (isShortLinkMode) { try { if (Consts.ECB_ENC == iFlag) { return api.SYMEncryptData( com.sunyard.SydApi.NALG_SM4, "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex), 1, pcData); } else { return api.SYMDecryptData( com.sunyard.SydApi.NALG_SM4, "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex), 1, pcData); } } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } else { synchronized (api) { try { if (Consts.ECB_ENC == iFlag) { return api.SYMEncryptData( com.sunyard.SydApi.NALG_SM4, "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex), 1, pcData); } else { return api.SYMDecryptData( com.sunyard.SydApi.NALG_SM4, "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex), 1, pcData); } } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } } } if ( ! this.isShortLinkMode ) { this.oip.setError(); } throw le; } @Override public List SYD_SM4_BatchData(int[] iKeyIndex, List pcData, int iFlag) { Logger log = LogFactory.getLogger(); if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4_BatchData方法接收参数:iKeyIndex:{},pcData元素个数为:{},iFlag:{}", iKeyIndex, pcData.size(), iFlag); } if ( null == pcData ) { throw new SydApiException("输入参数错误", 0x01000001); } if (pcData.isEmpty()) { return new ArrayList<>(); } if (iFlag == Consts.ECB_DEC) { for (byte[] pcdata : pcData) { if ( pcdata.length == 0 || pcdata.length % 16 != 0 || pcdata.length > 1040) { throw new SydApiException(0x01000015); } } } else { for (byte[] pcdata : pcData) { if ( pcdata.length > 1024) { throw new SydApiException(19); } } } if (null == this.hsms || this.hsms.length < 1) { throw new SydApiException(-1); } String[] keys = new String[iKeyIndex.length]; updateLastUseTime(); for (int i = 0; i < iKeyIndex.length; i++) { if ( iKeyIndex[i] < 0 ) { throw new SydApiException(0x01000001); } keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i])); } SydApiException le = new SydApiException(-1); for (SydApi4j api : this.hsms) { if (null == api) { continue; } if (isShortLinkMode) { try { if (Consts.ECB_ENC == iFlag) { return api.SYMEncryptDecryptBatchData(8, 1, null, keys, 1, pcData); } else { return api.SYMEncryptDecryptBatchData(9, 1, null, keys, 1, pcData); } } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } else { synchronized (api) { try { if (Consts.ECB_ENC == iFlag) { return api.SYMEncryptDecryptBatchData(8, 1, null, keys, 1, pcData); } else { return api.SYMEncryptDecryptBatchData(9, 1, null, keys, 1, pcData); } } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } } } if ( ! this.isShortLinkMode ) { this.oip.setError(); } throw le; } @Override public byte[] SYD_SM4_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, int iFlag) { Logger log = LogFactory.getLogger(); if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, iFlag); } if ( iKeyIndex < 0 ) { throw new SydApiException(0x01000001); } if (iFlag == Consts.ECB_DEC && (null == pcData || pcData.length == 0 || pcData.length % 16 != 0)) { throw new SydApiException(0x01000015); } if (null == this.hsms || this.hsms.length < 1) { throw new SydApiException(-1); } updateLastUseTime(); String keyIndex = String.format("%08d", iKeyIndex); SydApiException le = new SydApiException(-1); for (SydApi4j api : this.hsms) { if (null == api) { continue; } if (isShortLinkMode) { try { if (Consts.ECB_ENC == iFlag) { return api.SYD_SM4_LongData( 8, 1, null, "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex), 1, pcData, iPkgNum); } else { return api.SYD_SM4_LongData( 9, 1, null, "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex), 1, pcData, iPkgNum); } } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } else { synchronized (api) { try { if (Consts.ECB_ENC == iFlag) { return api.SYD_SM4_LongData( 8, 1, null, "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex), 1, pcData, iPkgNum); } else { return api.SYD_SM4_LongData( 9, 1, null, "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex), 1, pcData, iPkgNum); } } catch (SydApiException e) { if (null==e){ } le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } } } if ( ! this.isShortLinkMode ) { this.oip.setError(); } throw le; } @Override public List SYD_SM4Mac_BatchData(int[] iKeyIndex, List pcData) { Logger log = LogFactory.getLogger(); if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4Mac_BatchData:iKeyIndex:{},pcData元素个数为:{}", iKeyIndex, pcData.size()); } if (pcData.isEmpty()) { return new ArrayList<>(); } if (null == this.hsms || this.hsms.length < 1) { throw new SydApiException(-1); } String[] keys = new String[iKeyIndex.length]; updateLastUseTime(); for (int i = 0; i < iKeyIndex.length; i++) { if ( iKeyIndex[i] < 0 ) { throw new SydApiException(0x01000001); } keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i])); } SydApiException le = new SydApiException(-1); for (SydApi4j api : this.hsms) { if (null == api) { continue; } if (isShortLinkMode) { try { List list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData); List ret = new ArrayList<>(list.size()); for (int i = 0; i < list.size(); i++) { byte[] mac = new byte[16]; byte[] buff = list.get(i); System.arraycopy(buff, buff.length - 16, mac, 0, 16); ret.add( mac ); } return ret; } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } else { synchronized (api) { try { List list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData); List ret = new ArrayList<>(list.size()); for (int i = 0; i < list.size(); i++) { byte[] mac = new byte[16]; byte[] buff = list.get(i); System.arraycopy(buff, buff.length - 16, mac, 0, 16); ret.add( mac ); } return ret; } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } } } if ( ! this.isShortLinkMode ) { this.oip.setError(); } throw le; } @Override public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List pcData, List pcMac) { Logger log = LogFactory.getLogger(); if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4Mac_BatchData 方法接收参数:iKeyIndex:{}, pcData元素个数为:{},pcMac:{}", iKeyIndex, pcData.size(), pcMac.size()); } if (pcData.isEmpty()) { return new boolean[0]; } if (null == this.hsms || this.hsms.length < 1) { throw new SydApiException(-1); } String[] keys = new String[iKeyIndex.length]; updateLastUseTime(); for (int i = 0; i < iKeyIndex.length; i++) { keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i])); } SydApiException le = new SydApiException(-1); for (SydApi4j api : this.hsms) { if (null == api) { continue; } if (isShortLinkMode) { try { List list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData); boolean[] ret = new boolean[list.size()]; for (int i = 0; i < list.size(); i++) { byte[] mac = new byte[16]; byte[] buff = list.get(i); System.arraycopy(buff, buff.length - 16, mac, 0, 16); ret[i] = Arrays.equals(pcMac.get(i), mac); } return ret; } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } else { synchronized (api) { try { List list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData); boolean[] ret = new boolean[list.size()]; for (int i = 0; i < list.size(); i++) { byte[] mac = new byte[16]; byte[] buff = list.get(i); System.arraycopy(buff, buff.length - 16, mac, 0, 16); ret[i] = Arrays.equals(pcMac.get(i), mac); } return ret; } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } } } if ( ! this.isShortLinkMode ) { this.oip.setError(); } throw le; } @Override public LongDataReturn SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData) { Logger log = LogFactory.getLogger(); if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4Mac_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},pcProcData:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, pcProcData); } if (null == this.hsms || this.hsms.length < 1) { throw new SydApiException(-1); } String keyIndex = String.format("%08d", iKeyIndex); byte[] iv = null; // 仅一包 和 第一包 忽略 pcProcData 参数 if (Consts.PkgNumAll != iPkgNum && Consts.PkgNumHead != iPkgNum) { iv = pcProcData.getBytes(); } SydApiException le = new SydApiException(-1); updateLastUseTime(); for (SydApi4j api : this.hsms) { if (null == api) { continue; } if (isShortLinkMode) { try { byte[] ret = api.SYD_SM4_LongData( 8, 2, iv, "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex), 1, pcData, iPkgNum); byte[] mac = new byte[16]; System.arraycopy(ret, ret.length - 16, mac, 0, 16); // 仅一包 和 尾包 返回最终结果 if (Consts.PkgNumAll == iPkgNum || Consts.PkgNumTail == iPkgNum) { return new LongDataReturn<>(null, Util.bytes2HexString(mac)); } else { // 返回中间值 return new LongDataReturn<>(Util.bytes2HexString(mac), null); } } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } else { synchronized (api) { try { byte[] ret = api.SYD_SM4_LongData( 8, 2, iv, "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex), 1, pcData, iPkgNum); byte[] mac = new byte[16]; System.arraycopy(ret, ret.length - 16, mac, 0, 16); // 仅一包 和 尾包 返回最终结果 if (Consts.PkgNumAll == iPkgNum || Consts.PkgNumTail == iPkgNum) { return new LongDataReturn<>(null, Util.bytes2HexString(mac)); } else { // 返回中间值 return new LongDataReturn<>(Util.bytes2HexString(mac), null); } } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } } } if ( ! this.isShortLinkMode ) { this.oip.setError(); } throw le; } @Override public LongDataReturn SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData, String pcMac) { Logger log = LogFactory.getLogger(); if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4Mac_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},pcProcData:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, pcProcData, pcMac); } updateLastUseTime(); LongDataReturn ret = SYD_SM4Mac_LongData(iKeyIndex, pcData, iPkgNum, pcProcData); // 仅一包 和 尾包 返回最终结果 if (Consts.PkgNumAll == iPkgNum || Consts.PkgNumTail == iPkgNum) { return new LongDataReturn<>(null, pcMac.equals(ret.getData())); } else { // 返回中间值 return new LongDataReturn<>(ret.getProcData(), false); } } @Override public byte[] SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData) { return SYD_SM4Mac_ShortData(iKeyIndex, pcData, 1024); } public byte[] SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, int limitDataLen) { Logger log = LogFactory.getLogger(); if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4Mac_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData)); } if ( null == pcData || pcData.length > limitDataLen ) { throw new SydApiException("输入内容长度错误", 0x01000001); } if (null == this.hsms || this.hsms.length < 1) { throw new SydApiException(-1); } String keyIndex = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex)); log.debug("\"SK\" + SYMUtil.genPrivateKeyMark方法返回值:{}", keyIndex); SydApiException le = new SydApiException(-1); updateLastUseTime(); for (SydApi4j api : this.hsms) { if (null == api) { continue; } if (isShortLinkMode) { try { return api.SYD_SM4Mac_ShortData(keyIndex, pcData); } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } else { synchronized (api){ try { return api.SYD_SM4Mac_ShortData(keyIndex, pcData); } catch (SydApiException e) { le = e; if (e.getRetCode() < 0) { log.error("HA 重试"); continue; } throw e; } } } } if ( ! this.isShortLinkMode ) { this.oip.setError(); } throw le; } @Override public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, byte[] pcMac) { return SYD_SM4Mac_ShortData(iKeyIndex, pcData, 1024, pcMac); } public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, int limitDataLen, byte[] pcMac) { Logger log = LogFactory.getLogger(); if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4Mac_ShortData 方法接收参数:{}", iKeyIndex, Util.bytes2HexString(pcData), Util.bytes2HexString(pcMac)); } if ( null == pcData || pcData.length > limitDataLen ) { throw new SydApiException("输入内容超长",19); } if (null == this.hsms || this.hsms.length < 1) { throw new SydApiException(-1); } if (null == pcMac) { throw new IllegalArgumentException(); } updateLastUseTime(); byte[] mac = this.SYD_SM4Mac_ShortData(iKeyIndex, pcData, limitDataLen); log.debug(this + ".SYD_SM4Mac_ShortData方法返回值:{}", mac); return pcMac.equals(mac); } /** * 非依赖 C 实现,仅长连接依赖 C * @param iEncKeyIndex:输入。加密所需要的密钥索引值 * @param iMacKeyIndex:输入。计算mac所需要的密钥索引值 * @param pcData: 输入。计算mac所需要的数据。 * @return */ @Override public DataAndMac SYD_SM4_EncryptAndMac_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData) { Logger log = LogFactory.getLogger(); if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4_EncryptAndMac_ShortData:k1={} k2={}", iEncKeyIndex, iMacKeyIndex); } // 参数检查在函数内部 byte[] enData = SYD_SM4_ShortData(iEncKeyIndex, pcData, Consts.ECB_ENC); // mac 长度放宽到 1024,因为密文填充 byte[] mac = SYD_SM4Mac_ShortData(iMacKeyIndex, enData, 1040); return new DataAndMac(enData, mac); } @Override public DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData, byte[] mac) { Logger log = LogFactory.getLogger(); if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4_CheckMacAndDecrypt_ShortData:k1={} k2={}", iEncKeyIndex, iMacKeyIndex); } boolean ret = SYD_SM4Mac_ShortData(iMacKeyIndex, pcData, 1040, mac); byte[] data = null; try { data = SYD_SM4_ShortData(iEncKeyIndex, pcData, Consts.ECB_DEC); }catch (Exception e){ return new DataAndMacCheck(null, ret); } return new DataAndMacCheck(data, ret); } @Override public List SYD_SM4_EncryptAndMac_BatchData(int[] iEncKeyIndex, int[] iMacKeyIndex, List pcData) { if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4_EncryptAndMac_BatchData"); } if (null == this.hsms || null == iMacKeyIndex || null == pcData) { throw new SydApiException("输入内容为空",0x01000001); } if ( iEncKeyIndex.length != iMacKeyIndex.length || iEncKeyIndex.length != pcData.size() ) { throw new SydApiException("密钥长度和数据长度不对应", 0x01000001); } if (pcData.isEmpty()) { log.info("SYD_SM4_EncryptAndMac_BatchData pcData isEmpty"); return new ArrayList<>(); } List enDatas = SYD_SM4_BatchData(iEncKeyIndex, pcData, Consts.ECB_ENC); List macs = SYD_SM4Mac_BatchData(iMacKeyIndex, enDatas); List ret = new ArrayList(); for ( int i=0 ; i< enDatas.size() ; i++ ) { DataAndMac dam = new DataAndMac(enDatas.get(i), macs.get(i)); ret.add(dam); } return ret; } @Override public List SYD_SM4_CheckMacAndDecrypt_BatchData(int[] iEncKeyIndex, int[] iMacKeyIndex, List pcData) { if (LogFactory.iLogLevel == 3) { log.debug("SYD_SM4_CheckMacAndDecrypt_BatchData"); } if (pcData.isEmpty()) { log.info("SYD_SM4_CheckMacAndDecrypt_BatchData pcData isEmpty"); return new ArrayList<>(); } if (null == this.hsms || null == iMacKeyIndex ) { throw new SydApiException("输入内容为空", 0x01000001); } if ( iEncKeyIndex.length != iMacKeyIndex.length || iEncKeyIndex.length != pcData.size() ) { throw new SydApiException("密钥长度和数据长度不对应", 0x01000001); } List macs = new ArrayList<>(pcData.size()); Listdatas = new ArrayList<>(pcData.size()); for ( int i=0 ; i enData = SYD_SM4_BatchData(iEncKeyIndex, datas, Consts.ECB_DEC); Listret = new ArrayList<>(enData.size()); for ( int i=0 ; i list) { if (null == list || list.isEmpty()) { return new int[0]; } int[] ia = new int[list.size()]; for (int i = 0; i < ia.length; i++) { ia[i] = list.get(i); } return ia; } protected static boolean[] toBoolArray(List list) { if (null == list || list.isEmpty()) { return new boolean[0]; } boolean[] ia = new boolean[list.size()]; for (int i = 0; i < ia.length; i++) { ia[i] = list.get(i); } return ia; } }