瀏覽代碼

接口改用DES加密

qng 4 年之前
父節點
當前提交
14c9bd52c4

+ 5 - 4
eccs-biz/src/main/java/com/emato/biz/service/impl/SalesDetaiServicelImpl.java

@@ -8,6 +8,7 @@ import com.emato.biz.mapper.mall.SalesDetailMapper;
 import com.emato.biz.service.mall.ISalesDetaiServicel;
 import com.emato.common.core.Result;
 import com.emato.common.utils.DateUtils;
+import com.emato.common.utils.oms.request.DesUtils;
 import com.emato.common.utils.sign.Md5Utils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -59,13 +60,13 @@ public class SalesDetaiServicelImpl implements ISalesDetaiServicel {
             //调用接口限制间隔时间
             Date msgDate = mallMngChangeMapper.getPullDataTime((String)weChatMapMsg.put("tranDirection",msg.get("tranDirection")));
             long diff=(new Date().getTime()-msgDate.getTime())/1000/60;
-            if(diff>2){
-                //if(true){
+            //if(diff>2){
+                if(true){
                 //记录调用参数接口日志
                 pullQueryData(weChatMapMsg);
                 List<NewSystemFormatEntiy> list = mallMngChangeMapper.getSalesDetaiData(weChatMapMsg);
-                String md5List = Md5Utils.encryption(JSON.toJSONString(list));
-                return Result.success(md5List);
+                String desList = DesUtils.encode(JSON.toJSONString(list));
+                return Result.success(desList);
             }else{
                 return Result.error("1003","请求过于频繁,请稍后再试");
             }

+ 101 - 0
eccs-common/src/main/java/com/emato/common/utils/oms/request/DesUtils.java

@@ -0,0 +1,101 @@
+package com.emato.common.utils.oms.request;
+
+import javax.crypto.*;
+import javax.crypto.spec.DESKeySpec;
+import java.security.SecureRandom;
+
+public class DesUtils {
+
+    private static final String DES = "DES";
+    private static final String KEY = "4YztMHI7PsT4rLZN2435";
+
+    private static byte[] encrypt(byte[] src, byte[] key) throws Exception {
+        SecureRandom sr = new SecureRandom();
+        DESKeySpec dks = new DESKeySpec(key);
+        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
+        SecretKey secretKey = keyFactory.generateSecret(dks);
+        Cipher cipher = Cipher.getInstance(DES);
+        cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr);
+        return cipher.doFinal(src);
+    }
+
+    private static byte[] decrypt(byte[] src, byte[] key) throws Exception {
+        SecureRandom sr = new SecureRandom();
+        DESKeySpec dks = new DESKeySpec(key);
+        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
+        SecretKey secretKey = keyFactory.generateSecret(dks);
+        Cipher cipher = Cipher.getInstance(DES);
+        cipher.init(Cipher.DECRYPT_MODE, secretKey, sr);
+        return cipher.doFinal(src);
+    }
+
+    private static String byte2hex(byte[] b) {
+        String hs = "";
+        String temp = "";
+        for (int n = 0; n < b.length; n++) {
+            temp = (java.lang.Integer.toHexString(b[n] & 0XFF));
+            if (temp.length() == 1)
+                hs = hs + "0" + temp;
+            else
+                hs = hs + temp;
+        }
+        return hs.toUpperCase();
+
+    }
+
+    private static byte[] hex2byte(byte[] b) {
+        if ((b.length % 2) != 0)
+            throw new IllegalArgumentException("length not even");
+        byte[] b2 = new byte[b.length / 2];
+        for (int n = 0; n < b.length; n += 2) {
+            String item = new String(b, n, 2);
+            b2[n / 2] = (byte) Integer.parseInt(item, 16);
+        }
+        return b2;
+    }
+
+    private static String decode(String src, String key) {
+        String decryptStr = "";
+        try {
+            byte[] decrypt = decrypt(hex2byte(src.getBytes()), key.getBytes());
+            decryptStr = new String(decrypt);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return decryptStr;
+    }
+
+    private static String encode(String src, String key){
+        byte[] bytes = null;
+        String encryptStr = "";
+        try {
+            bytes = encrypt(src.getBytes(), key.getBytes());
+        } catch (Exception ex) {
+            ex.printStackTrace();
+        }
+        if (bytes != null)
+            encryptStr = byte2hex(bytes);
+        return encryptStr;
+    }
+
+    /**
+     * 解密
+     */
+    public static String decode(String src) {
+        return decode(src, KEY);
+    }
+
+    /**
+     * 加密
+     */
+    public static String encode(String src) {
+        return encode(src, KEY);
+    }
+//    public static void main(String[] args) {
+//        String ss = "uu123@#$";
+//        String encodeSS = encode(ss);
+//        System.out.println(encodeSS);
+//        String decodeSS = decode(encodeSS);
+//        System.out.println(decodeSS);
+//    }
+}