Bladeren bron

修复BUG

master
梁超 3 weken geleden
bovenliggende
commit
8d6b0fd2c0

+ 26
- 0
gly-base-core/src/main/java/cn/com/taiji/core/manager/tools/encryption/Sm2Utils.java Bestand weergeven

@@ -0,0 +1,26 @@
package cn.com.taiji.core.manager.tools.encryption;

import cn.hutool.crypto.asymmetric.SM2;
import org.bouncycastle.crypto.InvalidCipherTextException;

/**
* SM2 加解密工具类
*
* @author hou yi
* @date 2023年4月26日 下午6:05:57
*/
public class Sm2Utils {

public static byte[] decrypt(final byte[] privateKey, final byte[] cipher) throws InvalidCipherTextException {
final String privKeyStr = Utils.bytes2hex(privateKey);
final SM2 sm2d = new SM2(privKeyStr, (String) null);
return sm2d.decrypt(cipher);
}

public static byte[] encrypt(final byte[] publicKey, final byte[] clear) throws InvalidCipherTextException {
final String pubKeyStr = Utils.bytes2hex(publicKey);
final SM2 sm2e = new SM2((String) null, pubKeyStr);
return sm2e.encrypt(clear);
}

}

+ 416
- 0
gly-base-core/src/main/java/cn/com/taiji/core/manager/tools/encryption/Utils.java Bestand weergeven

@@ -0,0 +1,416 @@

package cn.com.taiji.core.manager.tools.encryption;

//import com.techrich.util.BytesBuffer;
//import com.techrich.util.Des3Cipher;
//import com.techrich.util.DesCipher;

import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
* VFJ 工具类
*
* @author hou yi
* @date 2023年4月26日 下午7:09:45
*/
public class Utils {
public static byte[] getRandom(final int length) {
final Random random = new Random();
final byte[] result = new byte[length];
for (int i = 0; i < length; ++i) {
result[i] = (byte) random.nextInt(256);
}
return result;
}

public static byte high(final byte chr) {
byte temp = 0;
for (int i = 7; i >= 4; --i) {
if ((1L << i & (long) chr) != 0x0L) {
temp |= (byte) (1L << i - 4);
}
}
return temp;
}

public static int bytes2int(final byte[] bytes, final int offset) {
final int length1 = bytes[offset + 3] << 24 & 0xFF000000;
final int length2 = bytes[offset + 2] << 16 & 0xFF0000;
final int length3 = bytes[offset + 1] << 8 & 0xFF00;
final int length4 = bytes[offset + 0] << 0 & 0xFF;
return length1 + length2 + length3 + length4;
}

public static short bytes2short(final byte[] bytes, final int offset) {
final int length1 = bytes[offset + 1] << 8 & 0xFF00;
final int length2 = bytes[offset + 0] << 0 & 0xFF;
return (short) (length1 + length2);
}

public static byte[] int2bytes(final int value) {
final byte[] bytes = { (byte) (value & 0xFF), (byte) ((value & 0xFF00) >> 8), (byte) ((value & 0xFF0000) >> 16),
(byte) ((value & 0xFF000000) >> 24) };
return bytes;
}

public static byte[] short2bytes(final short value) {
final byte[] bytes = { (byte) (value & 0xFF), (byte) ((value & 0xFF00) >> 8) };
return bytes;
}

public static String bytes2hex(final byte[] bytes, int len) {
if (len > bytes.length || len <= 0) {
len = bytes.length;
}
final StringBuffer sb = new StringBuffer();
for (int i = 0; i < len; ++i) {
String temp = Integer.toHexString(bytes[i]);
switch (temp.length()) {
case 0:
case 1: {
temp = "0" + temp;
break;
}
default: {
temp = temp.substring(temp.length() - 2);
break;
}
}
sb.append(temp);
}
return sb.toString().toUpperCase();
}

public static String bytes2hex(final byte[] bytes) {
return bytes2hex(bytes, 0);
}

public static int toInt(final char a) {
if (a >= '0' && a <= '9') {
return a - '0';
}
if (a >= 'A' && a <= 'F') {
return a - '7';
}
if (a >= 'a' && a <= 'f') {
return a - 'W';
}
return 0;
}

public static byte[] hex2bytes(String hex) {
if (hex.length() % 2 != 0) {
hex = "0" + hex;
}
final int len = hex.length() / 2;
final byte[] val = new byte[len];
for (int i = 0; i < len; ++i) {
val[i] = (byte) (toInt(hex.charAt(2 * i)) * 16 + toInt(hex.charAt(2 * i + 1)));
}
return val;
}

public static String bcd2string(final byte[] bcddata, final int offset, final int len) {
final int strlen = len * 2;
final byte[] temp = new byte[strlen];
int i = 0;
int j = 0;
while (i < len) {
temp[j++] = high(bcddata[offset + i]);
temp[j++] = (byte) (bcddata[offset + i] & 0xF);
++i;
}
for (i = 0; i < strlen; ++i) {
if (temp[i] >= 10) {
temp[i] = (byte) (65 + temp[i] - 10);
} else {
final byte[] array = temp;
final int n = i;
array[n] += 48;
}
}
return new String(temp);
}

public static String bcd2string(final byte[] bcddata) {
return bcd2string(bcddata, 0, bcddata.length);
}

static byte hxv(final byte chr) {
if (chr > 96) {
return (byte) (chr - 97 + 10);
}
if (chr > 64) {
return (byte) (chr - 65 + 10);
}
if (chr == 0) {
return 0;
}
return (byte) (chr - 48);
}

public static byte[] string2bcd(final String str, final int len) {
byte[] temp = new byte[len * 2];
for (int i = 0; i < len * 2; ++i) {
temp[i] = 48;
}
StringBuffer sb = new StringBuffer(new String(temp));
sb = sb.replace(len * 2 - str.length(), sb.length(), str);
temp = new byte[len];
int lpi = 0;
int lpj = 0;
while (lpi < len) {
final byte high = (byte) (hxv((byte) sb.charAt(lpj++)) << 4);
final byte low = hxv((byte) sb.charAt(lpj++));
temp[lpi] = (byte) (high | low);
++lpi;
}
return temp;
}

public static byte[] int2bcd(final int data, final int len) {
final String buf = String.valueOf(data);
return string2bcd(buf, len);
}

public static byte[] long2bcd(final long data, final int len) {
final String buf = String.valueOf(data);
return string2bcd(buf, len);
}

public static int ntohl(final byte[] bytes) {
return ntohl(bytes, 0);
}

public static int ntohl(final byte[] bytes, final int offset) {
final int length1 = bytes[offset + 0] << 24 & 0xFF000000;
final int length2 = bytes[offset + 1] << 16 & 0xFF0000;
final int length3 = bytes[offset + 2] << 8 & 0xFF00;
final int length4 = bytes[offset + 3] << 0 & 0xFF;
return length1 + length2 + length3 + length4;
}

public static byte[] htonl(final int value) {
final byte[] bytes = { (byte) ((value & 0xFF000000) >> 24), (byte) ((value & 0xFF0000) >> 16),
(byte) ((value & 0xFF00) >> 8), (byte) (value & 0xFF) };
return bytes;
}

public static short ntohs(final byte[] bytes, final int offset) {
final int length = (bytes[offset + 0] << 8 & 0xFF00) + (bytes[offset + 1] << 0 & 0xFF);
return (short) length;
}

public static byte[] htons(final short value) {
final byte[] bytes = { (byte) ((value & 0xFF00) >> 8), (byte) (value & 0xFF) };
return bytes;
}

public static byte[] xor(final byte[] b1, final byte[] b2) {
final int len = (b1.length <= b2.length) ? b1.length : b2.length;
final byte[] temp = new byte[len];
for (int i = 0; i < len; ++i) {
temp[i] = (byte) (b1[i] ^ b2[i]);
}
return temp;
}

public static String trunkString(int length, final String orig) {
if (length <= 0) {
length = 256;
}
final int len = orig.length();
if (len < 2 * length + 3) {
return orig;
}
final StringBuffer result = new StringBuffer();
return result.append(orig.substring(0, length)).append("...").append(orig.substring(orig.length() - length))
.toString();
}

public static String adjustString(final String value, final char fill, final int length, final boolean head) {
if (value == null || length < 0) {
return null;
}
final int srcLen = value.getBytes().length;
if (srcLen >= length) {
return new String(value.getBytes(), 0, length);
}
final StringBuffer sb = new StringBuffer().append(fill);
int len = length - srcLen;
while (len != 1) {
len /= 2;
sb.append(sb);
}
sb.append(sb).setLength(length - srcLen);
if (head) {
return sb.append(value).toString();
}
return sb.insert(0, value).toString();
}

public static String centerString(final String value, final int len) {
final int length = value.getBytes().length;
if (length >= len) {
return value.substring(0, len);
}
final int subLen = (len - length) / 2;
return padString(padString(value, length + subLen, -1), len, 1);
}

public static String padString(final String str, final int len, final int direct) {
if (str == null) {
return "";
}
if (str.length() >= len) {
return str;
}
final int fill = len - str.length();
int precount = 0;
int postcount = 0;
if (direct < 0) {
precount = fill;
} else if (direct > 0) {
postcount = fill;
} else {
precount = ((fill % 2 > 0) ? (fill / 2 + 1) : (fill / 2));
postcount = fill - precount;
}
final StringBuffer sb = new StringBuffer();
for (int i = 0; i < precount; ++i) {
sb.append(" ");
}
sb.append(str);
for (int i = 0; i < postcount; ++i) {
sb.append(" ");
}
return sb.toString();
}

// public static byte[] encryptTDes(final byte[] key, final byte[] plainText) {
// final Des3Cipher cipher = new Des3Cipher(key);
// final BytesBuffer bb = new BytesBuffer(plainText);
// while (bb.length() % 8 != 0) {
// bb.append((byte) 0);
// }
// final BytesBuffer result = new BytesBuffer();
// for (int i = 0; i < bb.length() / 8; i += 8) {
// final byte[] p = bb.getValueN(i, 8);
// final byte[] c = new byte[8];
// cipher.encrypt(p, c);
// result.append(c);
// }
// return result.getValue();
// }

// public static byte[] decryptTDes(final byte[] key, final byte[] ciperText) {
// final Des3Cipher cipher = new Des3Cipher(key);
// final BytesBuffer bb = new BytesBuffer(ciperText);
// final BytesBuffer result = new BytesBuffer();
// for (int i = 0; i < bb.length() / 8; i += 8) {
// final byte[] p = bb.getValueN(i, 8);
// final byte[] c = new byte[8];
// cipher.decrypt(p, c);
// result.append(c);
// }
// return result.getValue();
// }

// public static byte[] encryptPboc(final byte[] key, final byte[] plainText) {
// final Des3Cipher cipher = new Des3Cipher(key);
// final BytesBuffer bb = new BytesBuffer(plainText);
// if (bb.length() % 8 != 0) {
// bb.append((byte) (-128));
// }
// while (bb.length() % 8 != 0) {
// bb.append((byte) 0);
// }
// final BytesBuffer result = new BytesBuffer();
// for (int i = 0; i < bb.length() / 8; i += 8) {
// final byte[] p = bb.getValueN(i, 8);
// final byte[] c = new byte[8];
// cipher.encrypt(p, c);
// result.append(c);
// }
// return result.getValue();
// }

// public static byte[] decryptPboc(final byte[] key, final byte[] ciperText) {
// return decryptTDes(key, ciperText);
// }

// public static byte[] DiversifyKey(final byte[] key, final byte[] plainText) {
// final byte[] plain = new byte[8];
// System.arraycopy(plainText, 0, plain, 0, 8);
// final byte[] leftKey = encryptPboc(key, plain);
// for (int i = 0; i < 8; ++i) {
// plain[i] ^= -1;
// }
// final byte[] rightKey = encryptPboc(key, plain);
// final BytesBuffer bb = new BytesBuffer(leftKey);
// return bb.append(rightKey).getValue();
// }

// public static byte[] PBOCMac(final byte[] message, final byte[] key, final byte[] initData) {
// final BytesBuffer keyBuffer = new BytesBuffer(key);
// final BytesBuffer bb = new BytesBuffer(message);
// if (bb.length() % 8 != 0) {
// bb.append((byte) (-128));
// }
// while (bb.length() % 8 != 0) {
// bb.append((byte) 0);
// }
// final DesCipher des = new DesCipher(keyBuffer.getValueN(0, 8));
// final byte[] cipherData = new byte[8];
// System.arraycopy(initData, 0, cipherData, 0, 8);
// for (int i = 0; i < bb.length(); i += 8) {
// final byte[] data = bb.getValueN(i, 8);
// for (int j = 0; j < 8; ++j) {
// final byte[] array = cipherData;
// final int n = j;
// array[n] ^= data[j];
// }
// des.encrypt(cipherData, cipherData);
// }
// des.setKey(keyBuffer.getValueN(8, 8));
// des.decrypt(cipherData, cipherData);
// des.setKey(keyBuffer.getValueN(0, 8));
// des.encrypt(cipherData, cipherData);
// return new byte[] { cipherData[0], cipherData[1], cipherData[2], cipherData[3] };
// }

public static String getSystemDate() {
final Date date = new Date();
final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(date);
}

public static String getSystemTime() {
final Date date = new Date();
final SimpleDateFormat format = new SimpleDateFormat("HHmmss");
return format.format(date);
}

public static String getSystemDateTime() {
final Date date = new Date();
final SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
return format.format(date);
}

public static byte[] read(final InputStream in, final int size) throws IOException, InterruptedIOException {
final byte[] buf = new byte[size];
int rc;
for (int remain = size, received = 0; remain > 0; remain -= rc, received += rc) {
rc = in.read(buf, received, remain);
if (rc == -1) {
throw new IOException("\u7f51\u7edc\u8fde\u63a5\u53ef\u80fd\u51fa\u73b0\u95ee\u9898");
}
}
return buf;
}
}

+ 21
- 0
gly-base-core/src/main/java/cn/com/taiji/core/model/comm/protocol/inss/vfj/VfjRegistryRequest.java Bestand weergeven

@@ -0,0 +1,21 @@
package cn.com.taiji.core.model.comm.protocol.inss.vfj;

import cn.com.taiji.core.model.comm.protocol.AbstractSignTypeRequest;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

@Getter
@Setter
@Accessors(chain = true)
public class VfjRegistryRequest extends AbstractSignTypeRequest<VfjRegistryResponse> {
public VfjRegistryRequest() {
super(VfjServiceCmd.REGISTRY);
}

@JsonProperty("Signature")
private String signature;
@JsonProperty("ClientCert")
private String clientCert;
}

+ 15
- 0
gly-base-core/src/main/java/cn/com/taiji/core/model/comm/protocol/inss/vfj/VfjRegistryResponse.java Bestand weergeven

@@ -0,0 +1,15 @@
package cn.com.taiji.core.model.comm.protocol.inss.vfj;

import cn.com.taiji.core.model.comm.protocol.AbstractSignTypeResponse;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

@Getter
@Setter
@Accessors(chain = true)
public class VfjRegistryResponse extends AbstractSignTypeResponse {
@JsonProperty("Envelop")
private String envelop;
}

+ 1
- 1
gly-base-core/src/main/java/cn/com/taiji/core/model/comm/protocol/inss/vfj/VfjServiceCmd.java Bestand weergeven

@@ -6,7 +6,7 @@ import cn.com.taiji.core.model.comm.protocol.SignServiceType;
import cn.com.taiji.core.model.comm.protocol.inss.InssServiceType;

public enum VfjServiceCmd implements SignServiceCommand {
REGISTRY("客户端注册", "Registry", null),
REGISTRY("客户端注册", "Registry", VfjRegistryRequest.class),
STARTUP("客户端启动交易", "StartUp", VfjStartUpRequest.class),
BATCHAPPLY("批次管理类交易", "BatchApply", VfjBatchApplyRequest.class), //
APPLY4RECOVERY("回收申请", "Apply4Recovery", VfjApply4RecoveryRequest.class),

+ 4
- 0
zhywpt-service-inss/src/main/java/cn/com/taiji/inss/manager/handler/VfjServiceHandler.java Bestand weergeven

@@ -90,11 +90,15 @@ public class VfjServiceHandler extends AbstractInssServiceHandler<VfjServiceCmd>
private VfjUpgradeDoneManager vfjUpgradeDoneManager;
@Autowired
private VfjExtAuth4IssManager vfjExtAuth4IssManager;
@Autowired
private VfjRegistryManager vfjRegistryManager;

@Override
protected <T extends AbstractSignTypeRequest<?>> AbstractSignTypeResponse handleInternal(T request, SignJsonRequest jsonReq, AbstractHttpRequestInfo reqInfo) throws ServiceHandleException {
VfjServiceCmd cmd = VfjServiceCmd.fromIfCode(jsonReq.getIfCode());
switch (cmd) {
case REGISTRY:
return vfjRegistryManager.serviceHandle((VfjRegistryRequest) request);
case BATCHAPPLY:
return vfjBatchApplyManager.serviceHandle((VfjBatchApplyRequest) request);
case APPLY4RECOVERY:

+ 1
- 0
zhywpt-service-inss/src/main/java/cn/com/taiji/inss/manager/inst/InstOnlineActiveMananger.java Bestand weergeven

@@ -476,6 +476,7 @@ public class InstOnlineActiveMananger extends AbstractCallBackManager {
obuInfo.setObuNetId(vo.getObuId().substring(0, 4));
obuInfo.setObuStatusChangeTime(LocalDateTime.now());
obuInfo.setUpdateTime(LocalDateTime.now());
obuInfo.setCardId(vo.getCardId());
// 货车2020-01-01 前时间置为 2020-01-01 2030-01-01 该时间(2020)后按实际 时间记录
if (vehicleInfo.getType() != null
&& VehicleType.isWeightCar(vehicleInfo.getType())

+ 1
- 1
zhywpt-service-inss/src/main/java/cn/com/taiji/inss/manager/vfj/AbstractHandleManager.java Bestand weergeven

@@ -87,7 +87,7 @@ public abstract class AbstractHandleManager extends AbstractManager {
}
}

private String getTermTrace() {
protected String getTermTrace() {
long termTrace = redisWrapManager.increment(VFJ_TERM_TRACE, 1);
if (termTrace > 99999999) redisWrapManager.set(VFJ_TERM_TRACE, "1", 1, TimeUnit.DAYS);
String termTraceStr = "00000000" + termTrace;

+ 0
- 1
zhywpt-service-inss/src/main/java/cn/com/taiji/inss/manager/vfj/VfjInit4LoadManager.java Bestand weergeven

@@ -5,7 +5,6 @@ import cn.com.taiji.core.entity.log.InssInterfaceLog;
import cn.com.taiji.core.manager.comm.LogManager;
import cn.com.taiji.core.model.comm.protocol.inss.vfj.VfjInit4LoadRequest;
import cn.com.taiji.core.model.comm.protocol.inss.vfj.VfjInit4LoadResponse;
import cn.com.taiji.core.model.comm.protocol.inss.vfj.VfjServiceCmd;
import cn.com.taiji.core.repo.jpa.log.InssInterfaceLogRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

+ 53
- 0
zhywpt-service-inss/src/main/java/cn/com/taiji/inss/manager/vfj/VfjRegistryManager.java Bestand weergeven

@@ -0,0 +1,53 @@
package cn.com.taiji.inss.manager.vfj;

import cn.com.taiji.common.manager.net.http.ServiceHandleException;
import cn.com.taiji.common.pub.EncodeTool;
import cn.com.taiji.core.manager.tools.encryption.SM3Utils;
import cn.com.taiji.core.manager.tools.encryption.Sm2Utils;
import cn.com.taiji.core.model.comm.protocol.inss.vfj.VfjRegistryRequest;
import cn.com.taiji.core.model.comm.protocol.inss.vfj.VfjRegistryResponse;
import cn.com.taiji.core.model.comm.protocol.valid.GlyServiceError;
import cn.com.taiji.inss.config.VfjConfigProperties;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import org.bouncycastle.util.encoders.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class VfjRegistryManager extends AbstractHandleManager {
private static final String VFJ_SESSION_ID_KEY = "vfj_session_id";

@Autowired
private VfjConfigProperties vfjConfig;

public VfjRegistryResponse serviceHandle(VfjRegistryRequest request) throws ServiceHandleException {
try {
String srcData = vfjConfig.getNetNodeId() + vfjConfig.getTerminalId() + getTermTrace();
// 客户端私钥
byte[] priKeysChain = Base64.decode(vfjConfig.getClientPrivateKey().getBytes());
// 客户端公钥
byte[] cliPubKeysChain = Base64.decode(vfjConfig.getClientPublicKey().getBytes());
// 服务端公钥
byte[] srvPubKeysChain = Base64.decode(vfjConfig.getServerPublicKey().getBytes());
byte[] cipher = Sm2Utils.encrypt(srvPubKeysChain, SM3Utils.hash(srcData.getBytes()));
byte[] cipherCert = Sm2Utils.encrypt(srvPubKeysChain, cliPubKeysChain);
String sign = new String(Base64.encode(cipher));
request.setSignature(sign);
request.setClientCert(new String(Base64.encode(cipherCert)));
VfjRegistryResponse response = excute(request, VfjRegistryResponse.class);
if (!hasText(response.getEnvelop()))
throw GlyServiceError.BUSINESS_VALIDATE_ERR.toHandleException("接口错误:VFJ会话ID加密信封返回为空");
SM2 sm2 = new SM2(vfjConfig.getClientPrivateKey(), vfjConfig.getServerPublicKey());
byte[] sessionIdBytes = sm2.decrypt(EncodeTool.decodeBase64(response.getEnvelop()), KeyType.PrivateKey);
String sessionId = new String(sessionIdBytes);
redisWrapManager.set(VFJ_SESSION_ID_KEY, sessionId, 86400, TimeUnit.SECONDS);
return response;
} catch (Exception e) {
throw GlyServiceError.BUSINESS_VALIDATE_ERR.toHandleException(e.getMessage());
}

}
}

Laden…
Annuleren
Opslaan