· 9 years ago · Oct 12, 2016, 11:52 AM
1package com.medianova.security.token;
2
3import java.io.UnsupportedEncodingException;
4import java.text.DateFormat;
5import java.text.ParsePosition;
6import java.text.SimpleDateFormat;
7import java.util.Arrays;
8import java.util.Date;
9import java.util.Random;
10
11import javax.crypto.Cipher;
12import javax.crypto.SecretKey;
13import javax.crypto.SecretKeyFactory;
14import javax.crypto.spec.DESedeKeySpec;
15import javax.crypto.spec.IvParameterSpec;
16
17import org.apache.commons.codec.binary.Hex;
18
19/**
20 *
21 * @author yildizib
22 *
23 */
24public class SimpleDesEncrypterUtil {
25 private String desKey;
26 private String ivKey;
27 @SuppressWarnings("unused")
28 private String param;
29 private String algorithm;
30
31 /**
32 *
33 * @param key
34 * @param iv
35 */
36 public SimpleDesEncrypterUtil(String key, String iv) {
37 this.desKey = key;
38 this.ivKey = iv;
39 this.algorithm = "DESede";
40 }
41
42 /**
43 *
44 * @return
45 */
46 public String generateToken() {
47 DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH.mm.ss");
48 Date datetime = new Date();
49 String date = dateFormat.format(datetime);
50 SimpleDateFormat parser = new SimpleDateFormat("dd-MM-yyyy HH.mm.ss");
51
52 String tokenTemp = "'{R1}'MM-yyyy'{R2}'dd-mm-HH'{R3}'";
53 String temp = "abcdefghijklmnoprstuvyz1234567890";
54
55 StringBuilder sb = new StringBuilder();
56 sb.append(temp.charAt(new Random().nextInt(temp.length() - 1)));
57 sb.append(temp.charAt(new Random().nextInt(temp.length() - 1)));
58 sb.append(temp.charAt(new Random().nextInt(temp.length() - 1)));
59
60 tokenTemp = tokenTemp.replace("{R1}", sb.toString());
61
62 sb.delete(0, 3);
63
64 sb.append(temp.charAt(new Random().nextInt(temp.length() - 1)));
65 sb.append(temp.charAt(new Random().nextInt(temp.length() - 1)));
66
67 tokenTemp = tokenTemp.replace("{R2}", sb.toString());
68
69 sb.delete(0, 2);
70
71 sb.append(temp.charAt(new Random().nextInt(temp.length() - 1)));
72 sb.append(temp.charAt(new Random().nextInt(temp.length() - 1)));
73
74 tokenTemp = tokenTemp.replace("{R3}", sb.toString());
75
76 sb.delete(0, 2);
77
78 SimpleDateFormat formatter = new SimpleDateFormat(tokenTemp);
79
80 ParsePosition pos = new ParsePosition(0);
81 Date day = parser.parse(date, pos);
82
83 return crypt(formatter.format(day));
84 }
85
86 /**
87 *
88 * @param str
89 * @return
90 */
91 public String crypt(String str) {
92 try {
93 String dkey = this.desKey;
94 String ivk = this.ivKey;
95 String alg = this.algorithm;
96 String transformation = "DESede/CBC/NoPadding";
97 byte[] keyValue = dkey.getBytes("UTF-8");
98 byte[] ivValue = ivk.getBytes("UTF-8");
99 DESedeKeySpec keySpec = new DESedeKeySpec(keyValue);
100 IvParameterSpec iv = new IvParameterSpec(ivValue);
101 SecretKey key = SecretKeyFactory.getInstance(alg).generateSecret(
102 keySpec);
103 Cipher encrypter = Cipher.getInstance(transformation);
104 encrypter.init(1, key, iv);
105 byte[] input = getPaddedBytes(str);
106 byte[] encrypted = encrypter.doFinal(input);
107 return new String(Hex.encodeHex(encrypted));
108 } catch (Exception exception) {
109 }
110 return null;
111 }
112
113 /**
114 *
115 * @param str
116 * @return
117 */
118 public String decrypt(String str) {
119 try {
120 System.out.println(str);
121 String dkey = this.desKey;
122 String ivk = this.ivKey;
123 String alg = this.algorithm;
124 String transformation = "DESede/CBC/NoPadding";
125 byte[] keyValue = dkey.getBytes("UTF-8");
126 byte[] ivValue = ivk.getBytes("UTF-8");
127 DESedeKeySpec keySpec = new DESedeKeySpec(keyValue);
128 IvParameterSpec iv = new IvParameterSpec(ivValue);
129 SecretKey key = SecretKeyFactory.getInstance(alg).generateSecret(
130 keySpec);
131 Cipher decrypter = Cipher.getInstance(transformation);
132 decrypter.init(2, key, iv);
133 byte[] input = Hex.decodeHex(str.toCharArray());
134 byte[] decrypted = decrypter.doFinal(input);
135 return new String(decrypted);
136 } catch (Exception exception) {
137 }
138 return null;
139 }
140
141 /**
142 *
143 * @param s
144 * @return
145 * @throws UnsupportedEncodingException
146 */
147 public static byte[] getPaddedBytes(String s)
148 throws UnsupportedEncodingException {
149 try {
150 int n = s.length();
151 if (n % 8 != 0)
152 n += 8 - n % 8;
153 byte[] src = s.getBytes("UTF-8");
154 byte[] dst = Arrays.copyOf(src, n);
155 return dst;
156 } catch (Exception e) {
157 throw new RuntimeException(e);
158 }
159 }
160
161}