【License】java簡單實現license認證-DES


思路

  1. 在config文件中配置一個變量,license。
  2. license中包括該license允許的截止日期,使用DES算法進行加密
  3. 在用戶登錄時,讀取該license,並對license進行解密,用截止日期與當前日期進行對比,如果小於當前日期,則允許用戶登錄
  4. 否則,則提示用戶license過期

代碼

DES.java

  1 package com.fastech.utils;
  2 
  3 import java.security.*;
  4 import javax.crypto.*;
  5 
  6 /**
  7  * DES加解密算法
  8  */
  9 public class DES {
 10      private static String strDefaultKey = "加密的公鑰";
 11         private Cipher encryptCipher = null;
 12         private Cipher decryptCipher = null;
 13 
 14 
 15         /**
 16          * 默認構造方法,使用默認密鑰
 17          * @throws Exception
 18          */
 19         public DES() throws Exception {
 20             this(strDefaultKey);
 21         }
 22 
 23 
 24         /**
 25          * 指定密鑰構造方法
 26          * @param strKey 指定的密鑰
 27          * @throws Exception
 28          */
 29         public DES(String strKey) throws Exception {
 30             Security.addProvider(new com.sun.crypto.provider.SunJCE());
 31             Key key = getKey(strKey.getBytes());
 32             encryptCipher = Cipher.getInstance("DES");
 33             encryptCipher.init(Cipher.ENCRYPT_MODE, key);
 34             decryptCipher = Cipher.getInstance("DES");
 35             decryptCipher.init(Cipher.DECRYPT_MODE, key);
 36         }
 37 
 38 
 39         /**
 40          * 加密字符串
 41          * @param strIn 需加密的字符串
 42          * @return 加密后的字符串
 43          * @throws Exception
 44          */
 45         public String encrypt(String strIn) throws Exception {
 46             return byteArr2HexStr(encrypt(strIn.getBytes()));
 47         }
 48 
 49 
 50         /**
 51          * 加密字節數組
 52          * @param arrB 需加密的字節數組
 53          * @return 加密后的字節數組
 54          * @throws Exception
 55          */
 56         public byte[] encrypt(byte[] arrB) throws Exception {
 57             return encryptCipher.doFinal(arrB);
 58         }
 59 
 60 
 61 
 62         /**
 63          * 解密字符串
 64          * @param strIn 需解密的字符串
 65          * @return 解密后的字符串
 66          * @throws Exception
 67          */
 68         public String decrypt(String strIn) throws Exception {
 69             return new String(decrypt(hexStr2ByteArr(strIn)));
 70         }
 71 
 72 
 73         /**
 74          * 解密字節數組
 75          * @param arrB 需解密的字節數組
 76          * @return 解密后的字節數組
 77          * @throws Exception
 78          */
 79         public byte[] decrypt(byte[] arrB) throws Exception {
 80             return decryptCipher.doFinal(arrB);
 81         }
 82 
 83 
 84 
 85         /**
 86          * 從指定字符串生成密鑰,密鑰所需的字節數組長度為8位
 87          * 不足8位時后面補0,超出8位只取前8位
 88          * @param arrBTmp 構成該字符串的字節數組
 89          * @return 生成的密鑰
 90          * @throws java.lang.Exception
 91          */
 92         private Key getKey(byte[] arrBTmp) throws Exception {
 93             byte[] arrB = new byte[8];
 94             for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
 95                 arrB[i] = arrBTmp[i];
 96             }
 97             Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
 98             return key;
 99         }
100 
101 
102 
103         /**
104          * 將byte數組轉換為表示16進制值的字符串,
105          * 如:byte[]{8,18}轉換為:0813,
106          * 和public static byte[] hexStr2ByteArr(String strIn)
107          * 互為可逆的轉換過程
108          * @param arrB 需要轉換的byte數組
109          * @return 轉換后的字符串
110          * @throws Exception 本方法不處理任何異常,所有異常全部拋出
111          */
112         public static String byteArr2HexStr(byte[] arrB) throws Exception {
113             int iLen = arrB.length;
114             StringBuffer sb = new StringBuffer(iLen * 2);
115             for (int i = 0; i < iLen; i++) {
116                 int intTmp = arrB[i];
117                 while (intTmp < 0) {
118                     intTmp = intTmp + 256;
119                 }
120                 if (intTmp < 16) {
121                     sb.append("0");
122                 }
123                 sb.append(Integer.toString(intTmp, 16));
124             }
125             return sb.toString();
126         }
127 
128 
129 
130         /**
131          * 將表示16進制值的字符串轉換為byte數組,
132          * 和public static String byteArr2HexStr(byte[] arrB)
133          * 互為可逆的轉換過程
134          * @param strIn 需要轉換的字符串
135          * @return 轉換后的byte數組
136          * @throws Exception 本方法不處理任何異常,所有異常全部拋出
137          */
138         public static byte[] hexStr2ByteArr(String strIn) throws Exception {
139             byte[] arrB = strIn.getBytes();
140             int iLen = arrB.length;
141             byte[] arrOut = new byte[iLen / 2];
142             for (int i = 0; i < iLen; i = i + 2) {
143                 String strTmp = new String(arrB, i, 2);
144                 arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
145             }
146             return arrOut;
147         }
148 }
View Code

EncryUtil.java

 1 package com.fastech.utils;
 2 
 3 import java.text.DateFormat;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 
 7 /**
 8  * 加密解密工具類
 9  */
10 public class EncryUtil {
11     /**
12      * 使用默認密鑰進行DES加密
13      */
14     public static String encrypt(String plainText) {
15         try {
16             return new DES().encrypt(plainText);
17         } catch (Exception e) {
18             return null;
19         }
20     }
21 
22 
23     /**
24      * 使用指定密鑰進行DES解密
25      */
26     public static String encrypt(String plainText, String key) {
27         try {
28             return new DES(key).encrypt(plainText);
29         } catch (Exception e) {
30             return null;
31         }
32     }
33 
34 
35     /**
36      * 使用默認密鑰進行DES解密
37      */
38     public static String decrypt(String plainText) {
39         try {
40             return new DES().decrypt(plainText);
41         } catch (Exception e) {
42             return null;
43         }
44     }
45 
46 
47     /**
48      * 使用指定密鑰進行DES解密
49      */
50     public static String decrypt(String plainText, String key) {
51         try {
52             return new DES(key).decrypt(plainText);
53         } catch (Exception e) {
54             return null;
55         }
56     }
57 
58     public static int compareDate(String deadLine, String now) {
59         DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
60         try {
61             Date dt1 = df.parse(now);
62             Date dt2 = df.parse(deadLine);
63             if (dt1.getTime() < dt2.getTime()) {
64                 //System.out.println("now = "+now);
65                 //System.out.println("deadLine = "+deadLine);
66                 //System.out.println("在截止日期前");
67                 return 1;
68             } else if (dt1.getTime() > dt2.getTime()) {
69                 //System.out.println("在截止日期后");
70                 return -1;
71             } else {
72                 return 0;
73             }
74         } catch (Exception exception) {
75             exception.printStackTrace();
76         }
77         return 0;
78     }
79 }
View Code

UserController.java

 1 @Value("${Key}")
 2 private String Key;//讀取config文件中的key值
 3 //解密
 4 private Boolean desDecode(String str) {
 5         String t = "";  
 6         //System.out.println("加密后:" + EncryUtil.encrypt(t));  
 7         t = EncryUtil.decrypt(str);
 8         //System.out.println("解密后:" + t);  
 9         if(t.equals("perpetual license")) {
10             return true;
11         }else {
12             DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
13             Date date = new Date();
14             String nowDate = format.format(date);
15             Integer result = EncryUtil.compareDate(t,nowDate);
16             if(result == -1) {
17                 return false;
18             }
19         }
20 
21         return true;
22     }
23 //加密
24 private String desCode(String str) {
25         //str為加密的截止日期
26         String t = EncryUtil.encrypt(str); 
27         //System.out.println("加密后:" + t);  
28         return t;
29     }
View Code

config.properties

Key=6c9414166ca7c922eea3ff8f8bba22faa137f2e

流程

加密

調用EncryUtil.encrypt()函數,入參為待加密的截止日期字符串

解密

這里寫圖片描述

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM