整理短链接的四接口,依然使用 java 实现。
This commit is contained in:
parent
fdbcf5d43b
commit
01c29d3d79
@ -778,18 +778,24 @@ public class SydApiBaseFunction implements SydApi {
|
||||
|
||||
@Override
|
||||
public String SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData) {
|
||||
return SYD_SM4Mac_ShortData(iKeyIndex, pcData, 1024);
|
||||
}
|
||||
|
||||
public String 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 > 1040 ) {
|
||||
throw new SydApiException("输入内容超长",19);
|
||||
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);
|
||||
|
||||
@ -838,12 +844,17 @@ public class SydApiBaseFunction implements SydApi {
|
||||
|
||||
@Override
|
||||
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, String pcMac) {
|
||||
return SYD_SM4Mac_ShortData(iKeyIndex, pcData, 1024, pcMac);
|
||||
}
|
||||
|
||||
|
||||
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, int limitDataLen, String pcMac) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4Mac_ShortData方法接收参数:{}", iKeyIndex, Arrays.toString(pcData), pcMac);
|
||||
}
|
||||
|
||||
if ( null == pcData || pcData.length > 1040 ) {
|
||||
if ( null == pcData || pcData.length > limitDataLen ) {
|
||||
throw new SydApiException("输入内容超长",19);
|
||||
}
|
||||
|
||||
@ -861,83 +872,43 @@ public class SydApiBaseFunction implements SydApi {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 非依赖 C 实现,仅长连接依赖 C
|
||||
* @param iEncKeyIndex:输入。加密所需要的密钥索引值
|
||||
* @param iMacKeyIndex:输入。计算mac所需要的密钥索引值
|
||||
* @param pcData: 输入。计算mac所需要的数据。
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public DataAndMac SYD_SM4_EncryptAndMac_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData) {
|
||||
if ( null == pcData || pcData.length > 1024 ) {
|
||||
throw new SydApiException(19);
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4_EncryptAndMac_ShortData:k1={} k2={}", iEncKeyIndex, iMacKeyIndex);
|
||||
}
|
||||
|
||||
if ( ! ConnectHandlerManager.isInited() ) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
int enLen = BytesUtil.calculatePaddedLength(pcData.length);
|
||||
byte[] encData = new byte[enLen];
|
||||
byte[] mac = new byte[16];
|
||||
IntByReference encDataLen = new IntByReference(encData.length);
|
||||
|
||||
int i = SydLibrary.INSTANCE.SYD_SM4_Encrypt_Mac_ShortData(
|
||||
ConnectHandlerManager.getHandler(),
|
||||
iEncKeyIndex,
|
||||
iMacKeyIndex,
|
||||
pcData,
|
||||
pcData.length,
|
||||
encData,
|
||||
encDataLen,
|
||||
mac
|
||||
);
|
||||
if ( 0 == i ){
|
||||
return new DataAndMac(encData, Util.bytes2HexString(mac));
|
||||
} else {
|
||||
throw new SydApiException(i);
|
||||
}
|
||||
|
||||
// byte[] enData = SYD_SM4_ShortData(iEncKeyIndex, pcData, Consts.ECB_ENC);
|
||||
// String mac = SYD_SM4Mac_ShortData(iMacKeyIndex, enData);
|
||||
// return new DataAndMac(enData, mac);
|
||||
// 参数检查在函数内部
|
||||
byte[] enData = SYD_SM4_ShortData(iEncKeyIndex, pcData, Consts.ECB_ENC);
|
||||
// mac 长度放宽到 1024,因为密文填充
|
||||
String 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, String mac) {
|
||||
// boolean ret = SYD_SM4Mac_ShortData(iMacKeyIndex, pcData, 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);
|
||||
|
||||
if ( ! ConnectHandlerManager.isInited() ) {
|
||||
throw new SydApiException(-1);
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4_CheckMacAndDecrypt_ShortData:k1={} k2={}", iEncKeyIndex, iMacKeyIndex);
|
||||
}
|
||||
|
||||
byte[] decryptedData = new byte[pcData.length];
|
||||
IntByReference decryptedDataLen = new IntByReference(decryptedData.length);
|
||||
|
||||
int i = SydLibrary.INSTANCE.SYD_SM4_Decrypt_Mac_ShortData(
|
||||
ConnectHandlerManager.getHandler(),
|
||||
iEncKeyIndex,
|
||||
iMacKeyIndex,
|
||||
pcData,
|
||||
pcData.length,
|
||||
Util.hexString2Bytes(mac),
|
||||
decryptedData,
|
||||
decryptedDataLen
|
||||
);
|
||||
|
||||
byte[] data = new byte[decryptedDataLen.getValue()];
|
||||
System.arraycopy(decryptedData, 0, data, 0, data.length);
|
||||
|
||||
if ( 0 == i ){
|
||||
return new DataAndMacCheck(data, true);
|
||||
} if (0x01000010 == i) {
|
||||
return new DataAndMacCheck(data, false);
|
||||
} else {
|
||||
throw new SydApiException(i);
|
||||
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
|
||||
@ -947,15 +918,18 @@ public class SydApiBaseFunction implements SydApi {
|
||||
log.debug("SYD_SM4_EncryptAndMac_BatchData");
|
||||
}
|
||||
|
||||
if (pcData.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
if (null == this.hsms || null == iMacKeyIndex || null == pcData) {
|
||||
throw new SydApiException("输入内容为空",0x01000001);
|
||||
}
|
||||
|
||||
if (null == this.hsms || null == iMacKeyIndex || null == pcData) {
|
||||
throw new SydApiException("输入内容为空",15);
|
||||
}
|
||||
|
||||
if ( iEncKeyIndex.length != iMacKeyIndex.length || iEncKeyIndex.length != pcData.size() ) {
|
||||
throw new SydApiException("密钥长度和数据长度不对应", 15);
|
||||
throw new SydApiException("密钥长度和数据长度不对应", 0x01000001);
|
||||
}
|
||||
|
||||
if (pcData.isEmpty()) {
|
||||
log.info("SYD_SM4_EncryptAndMac_BatchData pcData isEmpty");
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
List<byte[]> enDatas = SYD_SM4_BatchData(iEncKeyIndex, pcData, Consts.ECB_ENC);
|
||||
@ -976,14 +950,15 @@ public class SydApiBaseFunction implements SydApi {
|
||||
}
|
||||
|
||||
if (pcData.isEmpty()) {
|
||||
log.info("SYD_SM4_CheckMacAndDecrypt_BatchData pcData isEmpty");
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
if (null == this.hsms || null == iMacKeyIndex || null == pcData) {
|
||||
throw new SydApiException("输入内容为空",15);
|
||||
if (null == this.hsms || null == iMacKeyIndex ) {
|
||||
throw new SydApiException("输入内容为空", 0x01000001);
|
||||
}
|
||||
if ( iEncKeyIndex.length != iMacKeyIndex.length || iEncKeyIndex.length != pcData.size() ) {
|
||||
throw new SydApiException("密钥长度和数据长度不对应", 15);
|
||||
throw new SydApiException("密钥长度和数据长度不对应", 0x01000001);
|
||||
}
|
||||
|
||||
String[] macs = new String[pcData.size()];
|
||||
|
||||
Loading…
Reference in New Issue
Block a user