代码如下:
[Java] 纯文本查看 复制代码 1. package cn;
2.
3. import java.security.Key;
4. import java.security.SecureRandom;
5.
6. import javax.crypto.Cipher;
7. import javax.crypto.KeyGenerator;
8.
9. public class Arithmetic {
10.
11. static Key key;
12.
13. /**
14. * 根据参数生成KEY
15. *
16. * @param strKey
17. */
18. public static void getKey(String strKey) {
19. try {
20. KeyGenerator _generator = KeyGenerator.getInstance("DES");
21. _generator.init(new SecureRandom(strKey.getBytes()));
22. key = _generator.generateKey();
23. _generator = null;
24. } catch (Exception e) {
25. e.printStackTrace();
26. }
27. }
28.
29. /**
30. * 加密String明文输入,String密文输出
31. *
32. * @param strMing
33. * [url=home.php?mod=space&uid=3039]@return[/url]
34. */
35. public static String getEncString(String strMing) {
36. byte[] byteMi = null;
37. byte[] byteMing = null;
38. String strMi = "";
39. try {
40. return byte2hex(getEncCode(strMing.getBytes()));
41.
42. // byteMing = strMing.getBytes("UTF8");
43. // byteMi = this.getEncCode(byteMing);
44. // strMi = new String( byteMi,"UTF8");
45. } catch (Exception e) {
46. e.printStackTrace();
47. } finally {
48. byteMing = null;
49. byteMi = null;
50. }
51. return strMi;
52. }
53.
54. /**
55. * 解密 以String密文输入,String明文输出
56. *
57. * @param strMi
58. * @return
59. */
60. public static String getDesString(String strMi) {
61. byte[] byteMing = null;
62. byte[] byteMi = null;
63. String strMing = "";
64. try {
65. return new String(getDesCode(hex2byte(strMi.getBytes())));
66.
67. // byteMing = this.getDesCode(byteMi);
68. // strMing = new String(byteMing,"UTF8");
69. } catch (Exception e) {
70. e.printStackTrace();
71. } finally {
72. byteMing = null;
73. byteMi = null;
74. }
75. return strMing;
76. }
77.
78. /**
79. * 加密以byte[]明文输入,byte[]密文输出
80. *
81. * @param byteS
82. * @return
83. */
84. private static byte[] getEncCode(byte[] byteS) {
85. byte[] byteFina = null;
86. Cipher cipher;
87. try {
88. cipher = Cipher.getInstance("DES");
89. cipher.init(Cipher.ENCRYPT_MODE, key);
90. byteFina = cipher.doFinal(byteS);
91. } catch (Exception e) {
92. e.printStackTrace();
93. } finally {
94. cipher = null;
95. }
96. return byteFina;
97. }
98.
99. /**
100. * 解密以byte[]密文输入,以byte[]明文输出
101. *
102. * @param byteD
103. * @return
104. */
105. private static byte[] getDesCode(byte[] byteD) {
106. Cipher cipher;
107. byte[] byteFina = null;
108. try {
109. cipher = Cipher.getInstance("DES");
110. cipher.init(Cipher.DECRYPT_MODE, key);
111. byteFina = cipher.doFinal(byteD);
112. } catch (Exception e) {
113. e.printStackTrace();
114. } finally {
115. cipher = null;
116. }
117. return byteFina;
118. }
119.
120. /**
121. * 二行制转字符串
122. *
123. * @param b
124. * @return
125. */
126. public static String byte2hex(byte[] b) { // 一个字节的数,
127. // 转成16进制字符串
128. String hs = "";
129. String stmp = "";
130. for (int n = 0; n < b.length; n++) {
131. // 整数转成十六进制表示
132. stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
133. if (stmp.length() == 1)
134. hs = hs + "0" + stmp;
135. else
136. hs = hs + stmp;
137. }
138. return hs.toUpperCase(); // 转成大写
139. }
140.
141. public static byte[] hex2byte(byte[] b) {
142. if ((b.length % 2) != 0)
143. throw new IllegalArgumentException("长度不是偶数");
144. byte[] b2 = new byte[b.length / 2];
145. for (int n = 0; n < b.length; n += 2) {
146. String item = new String(b, n, 2);
147. // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个进制字节
148. b2[n / 2] = (byte) Integer.parseInt(item, 16);
149. }
150.
151. return b2;
152. }
153.
154. public static void main(String[] args) {
155. Arithmetic des = new Arithmetic();// 实例化一个对像
156. des.getKey("aadd");// 生成密匙
157.
158. String strEnc = des.getEncString("胡锦涛");// 加密字符串,返回String的密文
159. System.out.println(strEnc);
160.
161. String strDes = des.getDesString(strEnc);// 把String 类型的密文解密
162. System.out.println(strDes);
163. }
164.
165. }
|