From 1ffbaa7b82f99443d933b0f7d0add0b35d2db01c Mon Sep 17 00:00:00 2001
From: wangrenhuang <wangren_huang@163.com>
Date: 星期三, 02 三月 2022 14:26:53 +0800
Subject: [PATCH] PTEncryptionUti新增解密方法

---
 src/main/java/com/hx/encryption/PTConstant.java       |   34 +++++++++++++++++
 src/main/java/com/hx/util/DateUtil.java               |   14 +++++++
 src/main/java/com/hx/encryption/PTEncryptionUtil.java |   70 ++++++++++++++++++++++++++---------
 3 files changed, 100 insertions(+), 18 deletions(-)

diff --git a/src/main/java/com/hx/encryption/PTConstant.java b/src/main/java/com/hx/encryption/PTConstant.java
new file mode 100644
index 0000000..9394fd2
--- /dev/null
+++ b/src/main/java/com/hx/encryption/PTConstant.java
@@ -0,0 +1,34 @@
+package com.hx.encryption;
+
+/**
+ *  鎺ュ彛瑙e瘑杩斿洖缁撴灉閰嶇疆
+ * @author wangrenhuang
+ * @Data 2020-06-09
+ */
+public class PTConstant {
+
+	public static String PT_SIGN_EEMPLY = "绛惧悕涓虹┖锛�";
+	public static String PT_TIMELS_EMPLY = "鏃堕棿涓虹┖锛�";
+	public static String PT_SIGN_ERROR = "绛惧悕閿欒";
+	public static String PT_ORER_TIME = "鎺ュ彛瓒呮椂";
+
+
+	private boolean success;
+	private String errCode;
+	
+	
+	/*************************************************************************/
+
+	public boolean isSuccess() {
+		return success;
+	}
+	public void setSuccess(boolean success) {
+		this.success = success;
+	}
+	public String getErrCode() {
+		return errCode;
+	}
+	public void setErrCode(String errCode) {
+		this.errCode = errCode;
+	}
+}
diff --git a/src/main/java/com/hx/encryption/PTEncryptionUtil.java b/src/main/java/com/hx/encryption/PTEncryptionUtil.java
index 1239213..8d15e1f 100644
--- a/src/main/java/com/hx/encryption/PTEncryptionUtil.java
+++ b/src/main/java/com/hx/encryption/PTEncryptionUtil.java
@@ -1,6 +1,8 @@
 package com.hx.encryption;
 
+import com.hx.util.DateUtil;
 import com.hx.util.MD5Util;
+import com.hx.util.StringUtils;
 
 import java.util.Calendar;
 import java.util.Date;
@@ -12,6 +14,7 @@
  */
 public class PTEncryptionUtil {
 
+
     /*
      * 鏃ユ湡鍔犲瘑
      * date  瑕佸姞瀵嗙殑鏃ユ湡
@@ -19,24 +22,55 @@
     public static String  encryption(Date date){
         Calendar cal=Calendar.getInstance();
         cal.setTime(date);
-        int year = cal.get(Calendar.YEAR);//鑾峰彇骞翠唤
-        int month=cal.get(Calendar.MONTH);//鑾峰彇鏈堜唤
-        int day=cal.get(Calendar.DATE);//鑾峰彇鏃�
-        int hour=cal.get(Calendar.HOUR_OF_DAY);//灏忔椂
-        int minute=cal.get(Calendar.MINUTE);//鍒�
-        int second=cal.get(Calendar.SECOND);//绉�
+        StringBuffer data = new StringBuffer();
+        data.append(cal.get(Calendar.DATE));
+        data.append(cal.get(Calendar.MONTH));
+        data.append(cal.get(Calendar.YEAR));
+        data.append(cal.get(Calendar.SECOND));
+        data.append(cal.get(Calendar.MINUTE));
+        data.append(cal.get(Calendar.HOUR_OF_DAY));
 
-        String yearStr = com.hx.mp.util.MD5Util.MD5Encode(year+"","");
-        String monthStr = com.hx.mp.util.MD5Util.MD5Encode(month+"","");
-        String dayStr = com.hx.mp.util.MD5Util.MD5Encode(day+"","");
-        String hourStr = com.hx.mp.util.MD5Util.MD5Encode(hour+"","");
-        String minuteStr = com.hx.mp.util.MD5Util.MD5Encode(minute+"","");
-        String secondStr = com.hx.mp.util.MD5Util.MD5Encode(second+"","");
-
-        StringBuffer result = new StringBuffer();
-        result.append(yearStr).append("-").append(monthStr).append("-").append(dayStr).append("-")
-                .append(hourStr).append("-").append(minuteStr).append("-").append(secondStr);
-
-        return result.toString();
+        return com.hx.mp.util.MD5Util.MD5Encode(data.toString(),"");
     }
+
+    /**
+     *
+     * @param data 绛惧悕
+     * @param date 鏃堕棿
+     * @param time 鏈夋晥鏃堕棿 绉�
+     * @return
+     */
+    public static PTConstant decode(String data,Date date,Integer time){
+        PTConstant ptConstant = new PTConstant();
+        ptConstant.setSuccess(true);
+        if (StringUtils.isEmpty(data)){
+            ptConstant.setSuccess(false);
+            ptConstant.setErrCode(PTConstant.PT_SIGN_EEMPLY);
+            return ptConstant;
+        }
+        if (null == date){
+            ptConstant.setSuccess(false);
+            ptConstant.setErrCode(PTConstant.PT_TIMELS_EMPLY);
+            return ptConstant;
+        }
+
+        if(!encryption(date).equals(data)){
+            ptConstant.setSuccess(false);
+            ptConstant.setErrCode(PTConstant.PT_SIGN_ERROR);
+            return ptConstant;
+        }
+        if(time == null){
+            ptConstant.setSuccess(true);
+            return ptConstant;
+        }
+        //鏈夋晥鏃堕棿鍒ゆ柇
+        if (DateUtil.calLastedTime(date) > time){
+            ptConstant.setSuccess(false);
+            ptConstant.setErrCode(PTConstant.PT_ORER_TIME);
+            return ptConstant;
+        }
+
+        return ptConstant;
+    }
+
 }
diff --git a/src/main/java/com/hx/util/DateUtil.java b/src/main/java/com/hx/util/DateUtil.java
index 9669b94..258a55e 100644
--- a/src/main/java/com/hx/util/DateUtil.java
+++ b/src/main/java/com/hx/util/DateUtil.java
@@ -202,6 +202,20 @@
         return Format_14.format(date);
     }
 
+
+    /**
+     * 褰撳墠鏃堕棿涔嬪墠鐨勬椂闂翠笌褰撳墠鏃堕棿鐩稿樊澶氬皯绉�
+     * @param startDate 褰撳墠鏃堕棿涔嬪墠鐨勬椂闂�
+     * @return
+     */
+    public static int calLastedTime(Date startDate) {
+        long nowDate = new Date().getTime();
+        long startDateTime = startDate.getTime();
+        int diffSeconds = (int) ((nowDate - startDateTime) / 1000);
+        return diffSeconds;
+    }
+
+
     /**
      * 杞崲鎴恲yyyMMddHHmmssSSS鏍煎紡鐨勬棩鏈熷瓧绗︿覆
      *

--
Gitblit v1.8.0