思路
- 在config文件中配置一个变量,license。
- license中包括该license允许的截止日期,使用DES算法进行加密
- 在用户登录时,读取该license,并对license进行解密,用截止日期与当前日期进行对比,如果小于当前日期,则允许用户登录
- 否则,则提示用户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()函数,入参为待加密的截止日期字符串