· 9 years ago · Nov 28, 2016, 07:22 AM
1import java.security.Key;
2import java.security.SecureRandom;
3import javax.crypto.Cipher;
4import javax.crypto.KeyGenerator;
5import sun.misc.BASE64Decoder;
6import sun.misc.BASE64Encoder;
7
8public class Encrypt {
9
10 private static String DES_ALGORITHM = "DES";
11
12 private static Key getKey(String strKey) {
13 try {
14 // KeyGenerator _generator = KeyGenerator.getInstance("DES");
15 // _generator.init(new SecureRandom(strKey.getBytes()));
16 // key = _generator.generateKey();
17 // _generator = null;
18
19 SecureRandom _secureRandom = SecureRandom.getInstance("SHA1PRNG");
20 _secureRandom.setSeed(strKey.getBytes());
21 KeyGenerator kg = null;
22 kg = KeyGenerator.getInstance(DES_ALGORITHM);
23 kg.init(_secureRandom);
24 // kg.init(56, secureRandom);
25
26 return kg.generateKey();
27 } catch (Exception e) {
28 throw new RuntimeException(
29 "Error initializing SqlMap class. Cause: " + e);
30 }
31 // return key;
32 }
33
34 /*
35 private SecretKey getKey(String secretKey)
36 throws NoSuchAlgorithmException, InvalidKeyException,
37 InvalidKeySpecException {
38 SecretKeyFactory keyFactory = SecretKeyFactory
39 .getInstance(DES_ALGORITHM);
40 DESKeySpec keySpec = new DESKeySpec(secretKey.getBytes());
41 keyFactory.generateSecret(keySpec);
42 return keyFactory.generateSecret(keySpec);
43 }*/
44
45 public static String encode(String strMing) {
46 byte[] byteMi = null;
47 byte[] byteMing = null;
48 String strMi = "";
49 BASE64Encoder base64en = new BASE64Encoder();
50 try {
51 byteMing = strMing.getBytes("UTF-8");
52 byteMi = Encrypt.encryptByte(byteMing);
53 strMi = base64en.encode(byteMi);
54 } catch (Exception e) {
55 throw new RuntimeException(
56 "Error initializing SqlMap class. Cause: " + e);
57 } finally {
58 base64en = null;
59 byteMing = null;
60 byteMi = null;
61 }
62 return strMi;
63 }
64
65 /**
66 *
67 * @param strMi
68 * @return
69 */
70 public static String decode(String strMi) {
71 BASE64Decoder base64De = new BASE64Decoder();
72 System.out.println(strMi.getBytes().length);
73 byte[] byteMing = null;
74 byte[] byteMi = null;
75 String strMing = "";
76 try {
77 byteMi = base64De.decodeBuffer(strMi);
78 byteMing = Encrypt.decryptByte(byteMi);
79 strMing = new String(byteMing, "UTF-8");
80 } catch (Exception e) {
81 throw new RuntimeException(
82 "Error initializing SqlMap class. Cause: " + e);
83 } finally {
84 base64De = null;
85 byteMing = null;
86 byteMi = null;
87 }
88 return strMing;
89 }
90
91 /**
92 *
93 * @param byteS
94 * @return
95 */
96 public static byte[] encryptByte(byte[] byteS) {
97 byte[] byteFina = null;
98 Cipher cipher;
99 try {
100 cipher = Cipher.getInstance("DES");
101 //cipher.init(Cipher.ENCRYPT_MODE, getKey(ENCRYPT_KEY));
102 cipher.init(Cipher.ENCRYPT_MODE, getKey(Util.getProperties("ENCRYPT_KEY")));
103 byteFina = cipher.doFinal(byteS);
104 } catch (Exception e) {
105 throw new RuntimeException(
106 "Error initializing SqlMap class. Cause: " + e);
107 } finally {
108 cipher = null;
109 }
110 return byteFina;
111 }
112
113 /**
114 *
115 * @param byteD
116 * @return
117 */
118 public static byte[] decryptByte(byte[] byteD) {
119 Cipher cipher;
120 byte[] byteFina = null;
121 try {
122 cipher = Cipher.getInstance("DES");
123 cipher.init(Cipher.DECRYPT_MODE, getKey(Util.getProperties("ENCRYPT_KEY")));
124 byteFina = cipher.doFinal(byteD);
125 } catch (Exception e) {
126 throw new RuntimeException(
127 "Error initializing SqlMap class. Cause: " + e);
128 } finally {
129 cipher = null;
130 }
131 System.out.println(byteFina.length);
132 return byteFina;
133 }
134
135 /**
136 * @param args
137 * @throws Exception
138 */
139 public static void main(String[] args) throws Exception {
140 String str1 = '123456';
141 String str2 = Encrypt.encode(str1);
142 String deStr = Encrypt.decode(str2);
143 System.out.println(str1);
144 System.out.println(str2);
145 System.out.println(deStr);
146 //System.out.println(""+Encrypt.decode(str4));
147 }
148}