From c18cfda28b18b930f179e19e7dcb14f0c159e918 Mon Sep 17 00:00:00 2001 From: liulu Date: Mon, 11 Nov 2024 14:14:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=B8=BB=E5=AF=86?= =?UTF-8?q?=E9=92=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/sunyard/chsm/sdf/BCSdfApiService.java | 6 +- .../chsm/sdf/adapter/BcSdfApiAdaptor.java | 4 +- .../chsm/controller/TmkController.java | 5 +- .../chsm/service/impl/DeviceServiceImpl.java | 90 ++++++++++--------- .../com/sunyard/chsm/task/DeviceTask.java | 5 +- 5 files changed, 55 insertions(+), 55 deletions(-) diff --git a/chsm-common/src/main/java/com/sunyard/chsm/sdf/BCSdfApiService.java b/chsm-common/src/main/java/com/sunyard/chsm/sdf/BCSdfApiService.java index 7520fc1..19cb23e 100644 --- a/chsm-common/src/main/java/com/sunyard/chsm/sdf/BCSdfApiService.java +++ b/chsm-common/src/main/java/com/sunyard/chsm/sdf/BCSdfApiService.java @@ -2,8 +2,7 @@ package com.sunyard.chsm.sdf; import com.sunyard.chsm.sdf.model.EccKey; -import com.sunyard.chsm.sdf.model.EccPriKey; -import com.sunyard.chsm.sdf.model.EccPubKey; +import com.sunyard.chsm.sdf.util.LangUtils; import com.sunyard.chsm.utils.gm.BCSM2Utils; import com.sunyard.chsm.utils.gm.BCSM3Utils; import lombok.SneakyThrows; @@ -43,8 +42,7 @@ public class BCSdfApiService implements SdfApiService { byte[] x = pubKey.getQ().getXCoord().getEncoded(); byte[] y = pubKey.getQ().getYCoord().getEncoded(); byte[] d = BigIntegers.asUnsignedByteArray(32, priKey.getD()); - - return new EccKey(new EccPubKey(256, x, y), new EccPriKey(256, d)); + return new EccKey(LangUtils.merge(x, y), d); } diff --git a/chsm-common/src/main/java/com/sunyard/chsm/sdf/adapter/BcSdfApiAdaptor.java b/chsm-common/src/main/java/com/sunyard/chsm/sdf/adapter/BcSdfApiAdaptor.java index 2ecd009..27c5983 100644 --- a/chsm-common/src/main/java/com/sunyard/chsm/sdf/adapter/BcSdfApiAdaptor.java +++ b/chsm-common/src/main/java/com/sunyard/chsm/sdf/adapter/BcSdfApiAdaptor.java @@ -31,7 +31,7 @@ public class BcSdfApiAdaptor implements SdfApiAdapter { deviceInfo = new DeviceInfo(); deviceInfo.setIssuerName("BC"); deviceInfo.setDeviceName("BC-3000"); - deviceInfo.setDeviceSerial("BC00202411051037"); + deviceInfo.setDeviceSerial("BC202411051037"); deviceInfo.setDeviceVersion(1); deviceInfo.setStandardVersion(1); deviceInfo.setAsymAlgAbility(new long[]{7493065891348563935L, 3000543215027029126L}); @@ -116,7 +116,7 @@ public class BcSdfApiAdaptor implements SdfApiAdapter { byte[] encrypt = BCSM2Utils.encrypt(parameters, pucData); return Arrays.copyOfRange(encrypt, 1, encrypt.length); } catch (InvalidCipherTextException e) { - throw new RuntimeException(e); + throw new IllegalArgumentException(e); } } diff --git a/chsm-web-manage/src/main/java/com/sunyard/chsm/controller/TmkController.java b/chsm-web-manage/src/main/java/com/sunyard/chsm/controller/TmkController.java index d8b0cbe..427f987 100644 --- a/chsm-web-manage/src/main/java/com/sunyard/chsm/controller/TmkController.java +++ b/chsm-web-manage/src/main/java/com/sunyard/chsm/controller/TmkController.java @@ -1,5 +1,6 @@ package com.sunyard.chsm.controller; +import com.sunyard.chsm.model.R; import com.sunyard.chsm.service.DeviceService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.PostMapping; @@ -26,9 +27,9 @@ public class TmkController { * 初始化主密钥 */ @PostMapping("/init") - public void initTmk() { + public R initTmk() { deviceService.initTmk(); - + return R.ok(); } diff --git a/chsm-web-manage/src/main/java/com/sunyard/chsm/service/impl/DeviceServiceImpl.java b/chsm-web-manage/src/main/java/com/sunyard/chsm/service/impl/DeviceServiceImpl.java index 770449a..72cc3c3 100644 --- a/chsm-web-manage/src/main/java/com/sunyard/chsm/service/impl/DeviceServiceImpl.java +++ b/chsm-web-manage/src/main/java/com/sunyard/chsm/service/impl/DeviceServiceImpl.java @@ -12,10 +12,6 @@ import com.sunyard.chsm.mapper.TmkInfoMapper; import com.sunyard.chsm.model.entity.Device; import com.sunyard.chsm.model.entity.TmkInfo; import com.sunyard.chsm.sdf.adapter.BcSdfApiAdaptor; -import com.sunyard.chsm.sdf.adapter.SdfApiAdapter; -import com.sunyard.chsm.sdf.adapter.SdfApiAdapterFactory; -import com.sunyard.chsm.sdf.context.DeviceContext; -import com.sunyard.chsm.sdf.model.DeviceInfo; import com.sunyard.chsm.service.DeviceService; import com.sunyard.ssp.modules.sysconf.paramconf.entity.ParamConf; import com.sunyard.ssp.modules.sysconf.paramconf.mapper.ParamConfMapper; @@ -189,10 +185,10 @@ public class DeviceServiceImpl implements DeviceService { public void initTmk() { ParamConf tmkInit = paramConfMapper.selectByKey("tmk_init"); Assert.isTrue(tmkInit == null || !"true".equals(tmkInit.getValue()), "主密钥已经初始化"); - List conned = spDeviceMapper.selectConnedList(); - +// List conned = spDeviceMapper.selectConnedList(); +// LocalDateTime now = LocalDateTime.now(); - if (CollectionUtils.isEmpty(conned)) { +// if (CollectionUtils.isEmpty(conned)) { // BcSdfApiAdaptor sdfApi = new BcSdfApiAdaptor(); byte[] sk = sdfApi.generateRandom("", 16); @@ -207,46 +203,52 @@ public class DeviceServiceImpl implements DeviceService { info.setEncTmk(Hex.toHexString(encSk)); info.setPubKey(Hex.toHexString(publicKey)); tmkInfoMapper.insert(info); - return; - } - Device device = conned.iterator().next(); - - DeviceContext context = new DeviceContext(); - context.setManufacturer(device.getManufacturer()); - context.setManufacturerModel(device.getManufacturerModel()); - context.setServiceIp(device.getServiceIp()); - context.setServicePort(device.getServicePort()); - SdfApiAdapter sdfApi = SdfApiAdapterFactory.newInstance(context); - String dh = sdfApi.openDevice(); - String sh = sdfApi.openSession(dh); - DeviceInfo deviceInfo = sdfApi.getDeviceInfo(sh); - - byte[] sk = sdfApi.generateRandom(sh, 16); - byte[] publicKey = sdfApi.exportEncPublicKeyECC(sh, 1); - byte[] encSk = sdfApi.externalEncryptECC(sh, publicKey, sk); - - TmkInfo info = new TmkInfo(); - info.setId(IdWorker.getId()); - info.setCreateTime(now); - info.setDeviceSerial(deviceInfo.getDeviceSerial()); - info.setEncTmk(Hex.toHexString(encSk)); - info.setPubKey(Hex.toHexString(publicKey)); - tmkInfoMapper.insert(info); +// return; +// } +// Device device = conned.iterator().next(); +// +// DeviceContext context = new DeviceContext(); +// context.setManufacturer(device.getManufacturer()); +// context.setManufacturerModel(device.getManufacturerModel()); +// context.setServiceIp(device.getServiceIp()); +// context.setServicePort(device.getServicePort()); +// SdfApiAdapter sdfApi = SdfApiAdapterFactory.newInstance(context); +// String dh = sdfApi.openDevice(); +// String sh = sdfApi.openSession(dh); +// DeviceInfo deviceInfo = sdfApi.getDeviceInfo(sh); +// +// byte[] sk = sdfApi.generateRandom(sh, 16); +// byte[] publicKey = sdfApi.exportEncPublicKeyECC(sh, 1); +// byte[] encSk = sdfApi.externalEncryptECC(sh, publicKey, sk); +// +// TmkInfo info = new TmkInfo(); +// info.setId(IdWorker.getId()); +// info.setCreateTime(now); +// info.setDeviceSerial(deviceInfo.getDeviceSerial()); +// info.setEncTmk(Hex.toHexString(encSk)); +// info.setPubKey(Hex.toHexString(publicKey)); +// tmkInfoMapper.insert(info); // - BcSdfApiAdaptor bcApi = new BcSdfApiAdaptor(); - byte[] bcPubK = bcApi.exportEncPublicKeyECC("", 1); - byte[] bcEncSk = sdfApi.exchangeDigitEnvelopeBaseOnECC(sh, 1, bcPubK, encSk); +// BcSdfApiAdaptor bcApi = new BcSdfApiAdaptor(); +// byte[] bcPubK = bcApi.exportEncPublicKeyECC("", 1); +// byte[] bcEncSk = sdfApi.exchangeDigitEnvelopeBaseOnECC(sh, 1, bcPubK, encSk); +// +// TmkInfo bcinfo = new TmkInfo(); +// bcinfo.setId(IdWorker.getId()); +// bcinfo.setCreateTime(now); +// bcinfo.setDeviceSerial(bcApi.getDeviceInfo("").getDeviceSerial()); +// bcinfo.setEncTmk(Hex.toHexString(bcEncSk)); +// bcinfo.setPubKey(Hex.toHexString(bcPubK)); +// tmkInfoMapper.insert(bcinfo); +// sdfApi.closeSession(sh); +// sdfApi.closeDevice(dh); - TmkInfo bcinfo = new TmkInfo(); - bcinfo.setId(IdWorker.getId()); - bcinfo.setCreateTime(now); - bcinfo.setDeviceSerial(bcApi.getDeviceInfo("").getDeviceSerial()); - bcinfo.setEncTmk(Hex.toHexString(bcEncSk)); - bcinfo.setPubKey(Hex.toHexString(bcPubK)); - tmkInfoMapper.insert(bcinfo); - sdfApi.closeSession(sh); - sdfApi.closeDevice(dh); + ParamConf conf = new ParamConf(); + conf.setKey("tmk_init"); + conf.setValue("true"); + conf.setCreatTime(LocalDateTime.now()); + paramConfMapper.insert(conf); } private void checkName(String name) { diff --git a/chsm-web-manage/src/main/java/com/sunyard/chsm/task/DeviceTask.java b/chsm-web-manage/src/main/java/com/sunyard/chsm/task/DeviceTask.java index 5e2a787..ec945c3 100644 --- a/chsm-web-manage/src/main/java/com/sunyard/chsm/task/DeviceTask.java +++ b/chsm-web-manage/src/main/java/com/sunyard/chsm/task/DeviceTask.java @@ -9,7 +9,6 @@ import com.sunyard.chsm.sdf.adapter.SdfApiAdapter; import com.sunyard.chsm.sdf.adapter.SdfApiAdapterFactory; import com.sunyard.chsm.sdf.context.DeviceContext; import com.sunyard.chsm.sdf.model.DeviceInfo; -import com.sunyard.chsm.sdf.model.EccPubKey; import lombok.extern.slf4j.Slf4j; import org.bouncycastle.util.encoders.Hex; import org.springframework.beans.factory.InitializingBean; @@ -65,8 +64,8 @@ public class DeviceTask implements InitializingBean { String sh = sdfApiAdapter.openSession(dh); DeviceInfo info = sdfApiAdapter.getDeviceInfo(sh); log.info("get DeviceInfo: {}", info); - EccPubKey eccPubKey = sdfApiAdapter.exportEncPublicKeyECC(sh, 2); - log.info("exportEncPublicKeyECC: {}", Hex.toHexString(eccPubKey.getPubKeyBytes())); + byte[] eccPubKey = sdfApiAdapter.exportEncPublicKeyECC(sh, 2); + log.info("exportEncPublicKeyECC: {}", Hex.toHexString(eccPubKey)); sdfApiAdapter.closeSession(sh); sdfApiAdapter.closeDevice(dh); connected = true;