From 8a282df6c09ac7bd7d7f3a7fc00dcf6100e4ea1a Mon Sep 17 00:00:00 2001 From: zhangyuan Date: Fri, 29 Jul 2022 10:55:55 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90=E5=AF=86=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/visit/utils/DhDeviceUtil.java | 16 +++++++++++----- .../power/modules/visit/utils/RSAUtils.java | 18 +++++++++++++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/epmet-plugins-module/pli-power-base/pli-power-base-server/src/main/java/com/epmet/plugin/power/modules/visit/utils/DhDeviceUtil.java b/epmet-plugins-module/pli-power-base/pli-power-base-server/src/main/java/com/epmet/plugin/power/modules/visit/utils/DhDeviceUtil.java index 06c265f..d12936f 100644 --- a/epmet-plugins-module/pli-power-base/pli-power-base-server/src/main/java/com/epmet/plugin/power/modules/visit/utils/DhDeviceUtil.java +++ b/epmet-plugins-module/pli-power-base/pli-power-base-server/src/main/java/com/epmet/plugin/power/modules/visit/utils/DhDeviceUtil.java @@ -67,7 +67,7 @@ public class DhDeviceUtil { * @author zhy * @date 2022/5/20 16:40 */ - public String getAccessToken() { + public String getPublicKey() { String key = RedisKeys.getDhPublicKey(); Object obj = redisUtils.get(key); if (null != obj) { @@ -404,7 +404,13 @@ public class DhDeviceUtil { cardFormDTO.setCardStatus("ACTIVE"); cardFormDTO.setStartDate(LocalDate.now().toString()); cardFormDTO.setEndDate(LocalDate.now().plusDays(1L).toString()); - cardFormDTO.setCardPassword("0"); + String pwd; + try { + pwd = RSAUtils.getPwdByRsa(getPublicKey(), "123456"); + } catch (Exception e) { + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "密码加密失败"); + } + cardFormDTO.setCardPassword(pwd); cardFormDTO.setSubSystems("1"); Map paramsMap = new HashMap<>(4); @@ -497,7 +503,7 @@ public class DhDeviceUtil { * @date 2022/7/28 16:09 */ private String getDhUrlParams() { - DhToken token = getLoginPass(getAccessToken()); + DhToken token = getLoginPass(getPublicKey()); return "?userId=" + token.getId() + "&username=" + token.getLoginName() + "&token=" + token.getToken(); } @@ -553,7 +559,7 @@ public class DhDeviceUtil { * @author yinzuomei * @date 2020/2/27 16:09 */ - public static byte[] toByteArray(InputStream input) throws IOException { + private byte[] toByteArray(InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 4]; int n = 0; @@ -571,7 +577,7 @@ public class DhDeviceUtil { * @author zhy * @date 2022/7/29 9:47 */ - public static String generateNiceString(int len) { + private String generateNiceString(int len) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { sb.append(Integer.toHexString(new Random().nextInt(16))); diff --git a/epmet-plugins-module/pli-power-base/pli-power-base-server/src/main/java/com/epmet/plugin/power/modules/visit/utils/RSAUtils.java b/epmet-plugins-module/pli-power-base/pli-power-base-server/src/main/java/com/epmet/plugin/power/modules/visit/utils/RSAUtils.java index 9d33dfa..866b401 100644 --- a/epmet-plugins-module/pli-power-base/pli-power-base-server/src/main/java/com/epmet/plugin/power/modules/visit/utils/RSAUtils.java +++ b/epmet-plugins-module/pli-power-base/pli-power-base-server/src/main/java/com/epmet/plugin/power/modules/visit/utils/RSAUtils.java @@ -18,6 +18,7 @@ import java.util.Map; import javax.crypto.Cipher; +import cn.hutool.core.codec.Base64; import org.bouncycastle.jce.provider.BouncyCastleProvider; /** @@ -305,5 +306,20 @@ public class RSAUtils{ keyMap.put(PRIVATE_KEY, privateKey); return keyMap; } - + + /** + * 密码加密 + * + * @param + * @return java.lang.String + * @author zhy + * @date 2022/7/29 10:46 + */ + public static String getPwdByRsa(String publicKey, String str) throws Exception { + byte[] decoded = Base64.decode(publicKey); + RSAPublicKey pubKey =(RSAPublicKey)KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));// RSA加密 + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, pubKey); + return Base64.encode(cipher.doFinal(str.getBytes("UTF-8"))); + } }