Bläddra i källkod

二维码加密和解密

dengyj 4 år sedan
förälder
incheckning
e132f3adc9

+ 1 - 1
src/main/java/com/steerinfo/baseinfo/meterbasegroupspot/controller/MeterBaseGroupSpotController.java

@@ -70,7 +70,7 @@ public class MeterBaseGroupSpotController extends BaseRESTfulController {
     @PostMapping(value = "/")
     public RESTfulResult add(@ModelAttribute MeterBaseGroupSpot model){
         HashMap<String,Object> map = new HashMap<>();
-        map.put("groupId",model.getSpotId());
+        map.put("scale",model.getSpotId());
         map.put("spotId",model.getSpotId());
         map.put("valueFlag","1");
         if (meterBaseGroupSpotService.getByParams(map) != null){

+ 33 - 0
src/main/java/com/steerinfo/meterwork/meterworkcaractualfirst/controller/MeterWorkCarActualFirstController.java

@@ -21,6 +21,7 @@ import com.steerinfo.meterwork.meterworkprelink.model.MeterWorkPreLink;
 import com.steerinfo.meterwork.meterworkrailwayactfirst.model.MeterWorkRailwayActFirst;
 import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
 import com.steerinfo.util.PublicMethod;
+import com.steerinfo.util.StringUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiImplicitParams;
@@ -883,6 +884,38 @@ public class MeterWorkCarActualFirstController extends BaseRESTfulController {
         }
     }
 
+    @ApiOperation(value = "终端补打二维码加密")
+    @PostMapping(value = "/encryptionForWfTest", produces = "application/json;charset=UTF-8")
+    public RESTfulResult encryptionForWfTest(@RequestBody HashMap param) throws Exception {
+        try {
+            String carNo = param.get("carNo") == null ? "": param.get("carNo").toString();
+            if(StringUtils.isEmpty(carNo)) {
+                return failed("车号为空");
+            }
+            String enStr = meterWorkCarActualFirstService.encryptionTest(carNo);
+            return success(enStr);
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            throw new MarkerMetException(500, "操作异常!!");
+        }
+    }
+
+    @ApiOperation(value = "终端补打二维码解密")
+    @PostMapping(value = "/DecryptionForWfTest", produces = "application/json;charset=UTF-8")
+    public RESTfulResult DecryptionForWfTest(@RequestBody HashMap param) throws Exception {
+        try {
+            String message = param.get("message") == null ? "": param.get("message").toString();
+            if(StringUtils.isEmpty(message)) {
+                return failed("密文为空");
+            }
+            RESTfulResult rm  = meterWorkCarActualFirstService.decryptionTest(message);
+            return rm;
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            throw new MarkerMetException(500, "操作异常!!");
+        }
+    }
+
     @ApiOperation(value = "非内倒车辆使用期限皮重洁净")
     @PostMapping(value = "/doAddNetByTermWf", produces = "application/json;charset=UTF-8")
     public RESTfulResult doAddNetByTermWf(@RequestBody CommonCombination<MeterWorkCarActualFirst, MeterWorkCarActual, MeterMonitorNote, MeterWorkPreLink> com) {

+ 2 - 0
src/main/java/com/steerinfo/meterwork/meterworkcaractualfirst/service/IMeterWorkCarActualFirstService.java

@@ -96,6 +96,8 @@ public interface IMeterWorkCarActualFirstService extends IBaseService<MeterWorkC
     RESTfulResult doAddNetWf(MeterWorkCarActualFirst secondDb, MeterWorkCarActual netDb,  MeterMonitorNote note,MeterWorkPreLink link, String isLock, String saveTare) throws Exception;
 
     String encryption(String str) throws Exception;
+    String encryptionTest(String str) throws Exception;
+    RESTfulResult decryptionTest(String str) throws Exception;
 
     RESTfulResult doAddNetByTermWf(MeterWorkCarActualFirst one, MeterWorkCarActual two, MeterMonitorNote three);
 

+ 68 - 0
src/main/java/com/steerinfo/meterwork/meterworkcaractualfirst/service/impl/MeterWorkCarActualFirstServiceImpl.java

@@ -56,6 +56,7 @@ import com.steerinfo.meterwork.meterworkrailwayactual.model.MeterWorkRailwayActu
 import com.steerinfo.pretrack.pretrackscale.mapper.PreTrackScaleMapper;
 import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
 import com.steerinfo.util.*;
+import org.apache.http.util.TextUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -63,6 +64,7 @@ import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.interceptor.TransactionAspectSupport;
 import org.springframework.util.Assert;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.DigestUtils;
 
 import javax.annotation.Resource;
 import java.lang.reflect.Field;
@@ -2080,6 +2082,72 @@ public class MeterWorkCarActualFirstServiceImpl extends BaseServiceImpl<MeterWor
         return deStr;
     }
 
+    @Override
+    public String encryptionTest(String carNo) throws Exception {
+//        String deStr = AESUtil.encrypt(str, "abcdefghijklmnop", "0000000000000000");
+//        return deStr;
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
+        String afl = sdf.format(new Date());
+        String message = carNo + "jisco2022" + afl;
+        String key = "jiscosec";
+        String deStr = DESUtil.toHexString(DESUtil.encrypt(message, key));
+        //byte[] deStr = DESUtil.encrypt(message, key);
+//        return deStr.toString();s
+//        return new String(deStr);
+        return deStr;
+    }
+
+    @Override
+    public RESTfulResult decryptionTest(String str) {
+        RESTfulResult rm = new RESTfulResult();
+        rm.setFailed();
+        try {
+            String key = "jiscosec";
+            String destr;
+            try{
+                destr = DESUtil.decrypt(str, key);
+            }catch(Exception e) {
+                rm.setCode("9");
+                rm.setMessage("密文错误");
+                return rm;
+            }
+            String carNo = destr.substring(0, 7);
+            String time = destr.substring(16);
+
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+            SimpleDateFormat sdf01 = new SimpleDateFormat("yyyyMMddHHmm");
+            Date date = sdf01.parse(time);
+            Calendar beforeTime = Calendar.getInstance();
+            beforeTime.add(Calendar.MINUTE, -5);// 5分钟之前的时间
+            Date startTime = sdf.parse(sdf.format(beforeTime.getTime()));
+            Date endTime = sdf.parse(sdf.format(new Date()));
+            if(!(date.compareTo(startTime) > 0 && date.compareTo(endTime) < 0)) {
+                rm.setMessage("二维码已过期");
+                rm.setCode("1");
+                return rm;
+            }
+
+                String carnumRegex = "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领 A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领 A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9 挂学警港澳]{1})";
+            if (!TextUtils.isEmpty(carNo)) {
+                if(carNo.matches(carnumRegex)) {
+                    rm.setSucceed();
+                    rm.setCode("0");
+                    rm.setMessage(carNo);
+                }else {
+                    rm.setMessage("车号不正确");
+                    rm.setCode("2");
+                }
+            }else {
+                rm.setMessage("车号为空");
+                rm.setCode("2");
+            }
+        }catch(Exception e) {
+            e.printStackTrace();
+            rm.setMessage("操作异常>>>" + e.getMessage());
+        }
+        return rm;
+    }
+
     //两条轨道衡计量实绩的替换
     private MeterWorkRailwayActFirst change(MeterWorkRailwayActFirst one, MeterWorkRailwayActFirst two) {
         //1、为有值的数据,2为0的值  进行替换

+ 56 - 0
src/main/java/com/steerinfo/util/DESUtil.java

@@ -0,0 +1,56 @@
+package com.steerinfo.util;
+
+import javax.crypto.Cipher;
+import javax.crypto.SecretKey;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.DESKeySpec;
+import javax.crypto.spec.IvParameterSpec;
+
+public class DESUtil {
+    //解密
+    public static String decrypt(String message, String key) throws Exception{
+        byte[] bytesrc = convertHexString(message);
+        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
+        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("GB2312"));
+        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
+        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
+        IvParameterSpec iv = new IvParameterSpec(key.getBytes("GB2312"));
+        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
+        byte[] retByte = cipher.doFinal(bytesrc);
+
+        return new String(retByte, "GB2312");
+    }
+
+    //加密
+    public static byte[] encrypt(String message, String key) throws Exception{
+        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
+        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("GB2312"));
+        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
+        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
+        IvParameterSpec iv = new IvParameterSpec(key.getBytes("GB2312"));
+        cipher.init(Cipher.ENCRYPT_MODE,secretKey,iv);
+        return cipher.doFinal(message.getBytes("GB2312"));
+    }
+
+    public static byte[] convertHexString(String ss) {
+        byte[] digest =new byte[ss.length()/2];
+        for (int i = 0; i < digest.length; i++) {
+            String byteString = ss.substring(2*i, 2*i+2);
+            int byteValue = Integer.parseInt(byteString, 16);
+            digest[i] = (byte) byteValue;
+        }
+        return digest;
+    }
+
+    public static String toHexString(byte b[]) {
+        StringBuffer hexString = new StringBuffer();
+        for (int i = 0; i < b.length; i++) {
+            String painText = Integer.toHexString(0xff & b[i]);
+            if (painText.length() < 2) {
+                painText = '0' + painText;
+            }
+            hexString.append(painText);
+        }
+        return hexString.toString();
+    }
+}