当前位置:编程学习 > JAVA >>

java几种加密解密方法

1. import java.security.MessageDigest;
2. 
3. import javax.crypto.Cipher;
4. import javax.crypto.SecretKey;
5. import javax.crypto.spec.SecretKeySpec;
6. 
7. import sun.misc.BASE64Decoder;
8. import sun.misc.BASE64Encoder;
9. 
10. /**
11. * Java 加解密工具类
12. *
13. * @author mosesxin@gmail.com
14. *
15. */
16. public class EncryptUtil {
17. 
18. private static final String UTF8 = "utf-8";
19. //定义 加密算法,可用 DES,DESede,Blowfish
20. private static final String ALGORITHM_DESEDE = "DESede";
21. 
22. /**
23. * MD5数字签名
24. *
25. * @param src
26. * @return
27. * @throws Exception
28. */
29. public String md5Digest(String src) throws Exception {
30. // 定义数字签名方法, 可用:MD5, SHA-1
31. MessageDigest md = MessageDigest.getInstance("MD5");
32. byte[] b = md.digest(src.getBytes(UTF8));
33. 
34. return this.byte2HexStr(b);
35. }
36. 
37. /**
38. * BASE64 加密
39. *
40. * @param src
41. * @return
42. * @throws Exception
43. */
44. public String base64Encoder(String src) throws Exception {
45. BASE64Encoder encoder = new BASE64Encoder();
46. return encoder.encode(src.getBytes(UTF8));
47. }
48. 
49. /**
50. * BASE64解密
51. *
52. * @param dest
53. * @return
54. * @throws Exception
55. */
56. public String base64Decoder(String dest) throws Exception {
57. BASE64Decoder decoder = new BASE64Decoder();
58. return new String(decoder.decodeBuffer(dest), UTF8);
59. }
60. 
61. /**
62. * 3DES加密
63. *
64. * @param src
65. * @param key
66. * @return
67. * @throws Exception
68. */
69. public String desedeEncoder(String src, String key) throws Exception {
70. SecretKey secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESEDE);
71. Cipher cipher = Cipher.getInstance(ALGORITHM_DESEDE);
72. cipher.init(Cipher.ENCRYPT_MODE, secretKey);
73. byte[] b = cipher.doFinal(src.getBytes(UTF8));
74. 
75. return byte2HexStr(b);
76. }
77. 
78. /**
79. * 3DES解密
80. *
81. * @param dest
82. * @param key
83. * @return
84. * @throws Exception
85. */
86. public String desedeDecoder(String dest, String key) throws Exception {
87. SecretKey secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESEDE);
88. Cipher cipher = Cipher.getInstance(ALGORITHM_DESEDE);
89. cipher.init(Cipher.DECRYPT_MODE, secretKey);
90. byte[] b = cipher.doFinal(str2ByteArray(dest));
91. 
92. return new String(b, UTF8);
93. 
94. }
95. 
96. /**
97. * 字节数组转化为大写16进制字符串
98. *
99. * @param b
100. * @return
101. */
102. private String byte2HexStr(byte[] b) {
103. StringBuilder sb = new StringBuilder();
104. for (int i = 0; i < b.length; i++) {
105. String s = Integer.toHexString(b[i] & 0xFF);
106. if (s.length() == 1) {
107. sb.append("0");
108. }
109. 
110. sb.append(s.toUpperCase());
111. }
112. 
113. return sb.toString();
114. }
115. 
116. /**
117. * 字符串转字节数组
118. *
119. * @param s
120. * @return
121. */
122. private byte[] str2ByteArray(String s) {
123. int byteArrayLength = s.length()/2;
124. byte[] b = new byte[byteArrayLength];
125. for (int i = 0; i < byteArrayLength; i++) {
126. byte b0 = (byte) Integer.valueOf(s.substring(i*2, i*2+2), 16).intValue();
127. b[i] = b0;
128. }
129. 
130. return b;
131. }
132. 
133. /**
134. * 构造3DES加解密方法key
135. *
136. * @param keyStr
137. * @return
138. * @throws Exception
139. */
140. private byte[] build3DesKey(String keyStr) throws Exception {
141. byte[] key = new byte[24];
142. byte[] temp = keyStr.getBytes(UTF8);
143. if (key.length > temp.length) {
144. System.arraycopy(temp, 0, key, 0, temp.length);
145. } else {
146. System.arraycopy(temp, 0, key, 0, key.length);
147. }
148. 
149. return key;
150. }
151. }

补充:综合编程 , 安全编程 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,