Parcourir la source

Merge branch 'develop' into dev/ltkg20200525

gaodm il y a 5 ans
Parent
commit
c5e5a2c6b5

+ 59 - 0
prec-service/src/main/java/com/diagbot/facade/PrecEncryptFacade.java

@@ -0,0 +1,59 @@
+package com.diagbot.facade;
+
+import com.diagbot.util.AESCipher;
+import com.diagbot.vo.PrecMapVO;
+import org.springframework.stereotype.Component;
+
+import java.text.ParseException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author wangfeng
+ * @Description:
+ * @date 2020-05-11 15:08
+ */
+@Component
+public class PrecEncryptFacade {
+    private static final String IV_STRING = "z76rxggpnykxeyb1";
+
+
+    public PrecMapVO getEncryption(PrecMapVO precMapVO) {
+        //patientInfo  patientName
+        Map<String,String> map = precMapVO.getMap();
+        Map<String,String> mapNew =new HashMap<String, String>();
+        try {
+            for(String key : map.keySet()){
+                String value = map.get(key);
+                String aesEncrypValue = AESCipher.aesEncryptString(value,IV_STRING);
+                mapNew.put(key,aesEncrypValue);
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        PrecMapVO data = new PrecMapVO();
+        data.setMap(mapNew);
+        return data;
+    }
+
+    public PrecMapVO getDecode(PrecMapVO precVO) {
+        //patientInfo  patientName
+        Map<String,String> map = precVO.getMap();
+        Map<String,String> mapNew =new HashMap<String, String>();
+        try {
+            for(String key : map.keySet()){
+                String value = map.get(key);
+                String aesEncrypValue = AESCipher.aesDecryptString(value,IV_STRING);
+                mapNew.put(key,aesEncrypValue);
+
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        PrecMapVO data = new PrecMapVO();
+        data.setMap(mapNew);
+        return data;
+    }
+}

+ 222 - 0
prec-service/src/main/java/com/diagbot/util/AESCipher.java

@@ -0,0 +1,222 @@
+package com.diagbot.util;
+
+import sun.misc.BASE64Decoder;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.text.ParseException;
+
+/**
+ * AES加密(指定一个初始化向量:IV_STRING,的加密方式)
+ *
+ * @author Administrator
+ */
+public class AESCipher {
+
+    private static final String IV_STRING = "z76rxggpnykxeyb1";
+
+    /**
+     * 加密
+     *
+     * @param content 加密内容
+     * @param key     加密秘钥
+     * @return
+     */
+    public static String aesEncryptString(String content, String key)
+            throws InvalidKeyException, NoSuchAlgorithmException,
+            NoSuchPaddingException, InvalidAlgorithmParameterException,
+            IllegalBlockSizeException, BadPaddingException,
+            UnsupportedEncodingException {
+        byte[] contentBytes = content.getBytes("UTF-8");
+        byte[] keyBytes = key.getBytes("UTF-8");
+        //加密,得到byte数组
+        byte[] encryptedBytes = aesEncryptBytes(contentBytes, keyBytes);
+        //将加密结果byte数组转换成16进制字符串
+        String hex = byte2HexStr(encryptedBytes);
+
+        return hex;
+    }
+
+    /**
+     * 解密
+     *
+     * @param content 解密内容
+     * @param key     解密秘钥
+     * @return
+     */
+    public static String aesDecryptString(String content, String key)
+            throws Exception {
+        byte[] decryptFrom = parseHexStr2Byte(content);
+        byte[] keyBytes = key.getBytes("UTF-8");
+        //解密,得到byte数组
+        byte[] decryptedBytes = aesDecryptBytes(decryptFrom, keyBytes);
+        //将解密结果byte数组转换成字符串
+        return new String(decryptedBytes, "UTF-8");
+    }
+
+    /**
+     * 将byte数组转换成16进制字符串
+     *
+     * @param b
+     * @return
+     */
+    public static String byte2HexStr(byte[] b) {
+        String stmp = "";
+        StringBuilder sb = new StringBuilder("");
+        for (int n = 0; n < b.length; n++) {
+            stmp = Integer.toHexString(b[n] & 0xFF);
+            sb.append(stmp.length() == 1 ? "0" + stmp : stmp);
+        }
+        return sb.toString().toUpperCase().trim();
+    }
+
+    public static String ebotongDecrypto(String str) {
+        BASE64Decoder base64decoder = new BASE64Decoder();
+        try {
+            byte[] encodeByte = base64decoder.decodeBuffer(str);
+            return new String(encodeByte);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return str;
+    }
+
+    /**
+     * 将16进制字符串转换成byte数组
+     *
+     * @param hexStr
+     * @return
+     */
+    public static byte[] parseHexStr2Byte(String hexStr) {
+        if (hexStr.length() < 1) {
+            return null;
+        }
+        byte[] result = new byte[hexStr.length() / 2];
+        for (int i = 0; i < hexStr.length() / 2; i++) {
+            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
+            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
+                    16);
+            result[i] = ((byte) (high * 16 + low));
+        }
+        return result;
+    }
+
+    /**
+     * 加密,得到byte数组
+     *
+     * @param contentBytes 加密内容的byte数组
+     * @param keyBytes     加密秘钥的byte数组
+     * @return
+     */
+    public static byte[] aesEncryptBytes(byte[] contentBytes, byte[] keyBytes)
+            throws NoSuchAlgorithmException, NoSuchPaddingException,
+            InvalidKeyException, InvalidAlgorithmParameterException,
+            IllegalBlockSizeException, BadPaddingException,
+            UnsupportedEncodingException {
+        return cipherOperation(contentBytes, keyBytes, Cipher.ENCRYPT_MODE);
+    }
+
+    /**
+     * 解密,得到byte数组
+     *
+     * @param contentBytes 解密内容的byte数组
+     * @param keyBytes     解密秘钥的byte数组
+     * @return
+     */
+    public static byte[] aesDecryptBytes(byte[] contentBytes, byte[] keyBytes)
+            throws NoSuchAlgorithmException, NoSuchPaddingException,
+            InvalidKeyException, InvalidAlgorithmParameterException,
+            IllegalBlockSizeException, BadPaddingException,
+            UnsupportedEncodingException {
+        return cipherOperation(contentBytes, keyBytes, Cipher.DECRYPT_MODE);
+    }
+
+    /**
+     * 加密/解密(指定一个初始化向量:IV_STRING)
+     *
+     * @param contentBytes 内容的byte数组
+     * @param keyBytes     秘钥的byte数组
+     * @param mode         加密:Cipher.ENCRYPT_MODE / 解密:Cipher.DECRYPT_MODE
+     * @return
+     */
+    private static byte[] cipherOperation(byte[] contentBytes, byte[] keyBytes,
+                                          int mode) throws UnsupportedEncodingException,
+            NoSuchAlgorithmException, NoSuchPaddingException,
+            InvalidKeyException, InvalidAlgorithmParameterException,
+            IllegalBlockSizeException, BadPaddingException {
+        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
+
+        byte[] initParam = IV_STRING.getBytes("UTF-8");
+        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
+
+        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
+        cipher.init(mode, secretKey, ivParameterSpec);
+
+        return cipher.doFinal(contentBytes);
+    }
+
+    public static void main(String[] args) throws ParseException {
+        //        String time = "2018-03-29 13:09:43";
+        //        String serial = "000000ECC4BE0564";
+        //        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        //        Date date = formatter.parse(time);
+        //        long times = date.getTime();
+        //        String subSerial = serial.substring(serial.length() - 8, serial.length());
+        //        String stimes = String.valueOf(times);
+        //        String subtimes = stimes.substring(stimes.length() - 8, stimes.length());
+        //        String keys = subSerial + subtimes;
+        //        System.out.println("【subSerial】="+subSerial);
+        //        System.out.println("【stimes】="+stimes);
+        //        System.out.println("【times】="+times);
+        //        System.out.println("【subtimes】="+subtimes);
+        //        System.out.println("【keys】=" + keys);
+        //        String money = String.valueOf(100);
+        //        System.out.println("【money】=" + money);
+        //
+        //        String en = null;
+        //        try
+        //        {
+        //            en = aesEncryptString(money, keys);
+        //        }
+        //        catch (Exception e)
+        //        {
+        //            e.printStackTrace();
+        //        }
+        //        String de = null;
+        //        try
+        //        {
+        //            de = aesDecryptString(en, keys);
+        //        }
+        //        catch (Exception e)
+        //        {
+        //            e.printStackTrace();
+        //        }
+        //        System.out.println("加密前金额:"+de);
+        //        System.out.println("加密后金额:"+en);
+        String content = "test";
+        String en = "";
+        String de = "";
+        try {
+            en = aesEncryptString(content, IV_STRING);
+            System.out.println(en);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        try {
+            de = aesDecryptString(en, IV_STRING);
+            System.out.println(de);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 20 - 0
prec-service/src/main/java/com/diagbot/vo/PrecMapVO.java

@@ -0,0 +1,20 @@
+package com.diagbot.vo;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.Date;
+import java.util.Map;
+
+/**
+ * @author wangfeng
+ * @Description:
+ * @date 2020-05-11 14:47
+ */
+@Setter
+@Getter
+public class PrecMapVO {
+    //请求数据
+    private  Map<String,String> map;
+}

+ 46 - 0
prec-service/src/main/java/com/diagbot/web/PrecEncryptController.java

@@ -0,0 +1,46 @@
+package com.diagbot.web;
+
+import com.diagbot.annotation.SysLogger;
+import com.diagbot.dto.RespDTO;
+import com.diagbot.facade.PrecEncryptFacade;
+import com.diagbot.vo.PrecMapVO;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.validation.Valid;
+
+/**
+ * @author wangfeng
+ * @Description:
+ * @date 2020-05-11 14:44
+ */
+@RestController
+@RequestMapping("/prec")
+@Api(value = "预问诊请求API[by:wangfeng]", tags = { "预问诊请求API" })
+@SuppressWarnings("unchecked")
+public class PrecEncryptController {
+
+    @Autowired
+    private PrecEncryptFacade precEncryptFacade;
+
+    @ApiOperation(value = "请求参数加密:[by:wangfeng]",
+            notes = "Map<String ,String> map")
+    @PostMapping("/getEncryption")
+    @SysLogger("getEncryption")
+    public RespDTO<PrecMapVO> getEncryption(@RequestBody PrecMapVO precMapVO) {
+        return RespDTO.onSuc(precEncryptFacade.getEncryption(precMapVO));
+    }
+
+    @ApiOperation(value = "请求参数解密 :[by:wangfeng]",
+            notes = "Map<String ,String> map")
+    @PostMapping("/getDecode")
+    @SysLogger("getDecode")
+    public RespDTO<PrecMapVO> getDecode(@RequestBody @Valid PrecMapVO precVO) {
+        return RespDTO.onSuc(precEncryptFacade.getDecode(precVO));
+    }
+}