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