Java安全工具,生成MD5,Base64,UUID

24 篇文章 0 订阅
[java]  view plain copy
  1. /* 
  2. * EncryptUtils.java 
  3. * Copyright (C) 2007-3-19 <JustinLei@gmail.com> 
  4. * 
  5. * This program is free software; you can redistribute it and/or modify 
  6. * it under the terms of the GNU General Public License as published by 
  7. * the Free Software Foundation; either version 2 of the License, or 
  8. * (at your option) any later version. 
  9. * 
  10. * This program is distributed in the hope that it will be useful, 
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  13. * GNU General Public License for more details. 
  14. * 
  15. */  
  16. package org.lambdasoft.utils;  
  17. import java.io.ByteArrayInputStream;  
  18. import java.io.ByteArrayOutputStream;  
  19. import java.io.IOException;  
  20. import java.io.ObjectInputStream;  
  21. import java.io.ObjectOutputStream;  
  22. import java.io.Serializable;  
  23. import java.util.Date;  
  24. import java.util.UUID;  
  25. import sun.misc.BASE64Decoder;  
  26. import sun.misc.BASE64Encoder;  
  27. /** 
  28. * @author TangLei <justinlei@gmail.com> 
  29. * @date 2008-11-28 
  30. */  
  31. public class EncryptUtils {  
  32. static final int S11 = 7;  
  33. static final int S12 = 12;  
  34. static final int S13 = 17;  
  35. static final int S14 = 22;  
  36. static final int S21 = 5;  
  37. static final int S22 = 9;  
  38. static final int S23 = 14;  
  39. static final int S24 = 20;  
  40. static final int S31 = 4;  
  41. static final int S32 = 11;  
  42. static final int S33 = 16;  
  43. static final int S34 = 23;  
  44. static final int S41 = 6;  
  45. static final int S42 = 10;  
  46. static final int S43 = 15;  
  47. static final int S44 = 21;  
  48. static final byte[] PADDING = { -128000000000000,  
  49. 0000000000000000000000,  
  50. 0000000000000000000000,  
  51. 0000000 };  
  52. private long[] state = new long[4]; // state (ABCD)  
  53. private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb first)  
  54. private byte[] buffer = new byte[64]; // input buffer  
  55. public static String digestHexStr;  
  56. private static byte[] digest = new byte[16];  
  57. public static String getMD5ofStr(String inbuf) {  
  58. EncryptUtils md5 = new EncryptUtils();  
  59. md5.md5Init();  
  60. md5.md5Update(inbuf.getBytes(), inbuf.length());  
  61. md5.md5Final();  
  62. digestHexStr = "";  
  63. for (int i = 0; i < 16; i++) {  
  64. digestHexStr += byteHEX(digest[i]);  
  65. }  
  66. return digestHexStr;  
  67. }  
  68. public EncryptUtils() {  
  69. md5Init();  
  70. }  
  71. private void md5Init() {  
  72. count[0] = 0L;  
  73. count[1] = 0L;  
  74. // /* Load magic initialization constants.  
  75. state[0] = 0x67452301L;  
  76. state[1] = 0xefcdab89L;  
  77. state[2] = 0x98badcfeL;  
  78. state[3] = 0x10325476L;  
  79. return;  
  80. }  
  81. private long F(long x, long y, long z) {  
  82. return (x & y) | ((~x) & z);  
  83. }  
  84. private long G(long x, long y, long z) {  
  85. return (x & z) | (y & (~z));  
  86. }  
  87. private long H(long x, long y, long z) {  
  88. return x ^ y ^ z;  
  89. }  
  90. private long I(long x, long y, long z) {  
  91. return y ^ (x | (~z));  
  92. }  
  93. private long FF(long a, long b, long c, long d, long x, long s, long ac) {  
  94. a += F(b, c, d) + x + ac;  
  95. a = ((int) a << s) | ((int) a >>> (32 - s));  
  96. a += b;  
  97. return a;  
  98. }  
  99. private long GG(long a, long b, long c, long d, long x, long s, long ac) {  
  100. a += G(b, c, d) + x + ac;  
  101. a = ((int) a << s) | ((int) a >>> (32 - s));  
  102. a += b;  
  103. return a;  
  104. }  
  105. private long HH(long a, long b, long c, long d, long x, long s, long ac) {  
  106. a += H(b, c, d) + x + ac;  
  107. a = ((int) a << s) | ((int) a >>> (32 - s));  
  108. a += b;  
  109. return a;  
  110. }  
  111. private long II(long a, long b, long c, long d, long x, long s, long ac) {  
  112. a += I(b, c, d) + x + ac;  
  113. a = ((int) a << s) | ((int) a >>> (32 - s));  
  114. a += b;  
  115. return a;  
  116. }  
  117. private void md5Update(byte[] inbuf, int inputLen) {  
  118. int i, index, partLen;  
  119. byte[] block = new byte[64];  
  120. index = (int) (count[0] >>> 3) & 0x3F;  
  121. // /* Update number of bits */  
  122. if ((count[0] += (inputLen << 3)) < (inputLen << 3))  
  123. count[1]++;  
  124. count[1] += (inputLen >>> 29);  
  125. partLen = 64 - index;  
  126. // Transform as many times as possible.  
  127. if (inputLen >= partLen) {  
  128. md5Memcpy(buffer, inbuf, index, 0, partLen);  
  129. md5Transform(buffer);  
  130. for (i = partLen; i + 63 < inputLen; i += 64) {  
  131. md5Memcpy(block, inbuf, 0, i, 64);  
  132. md5Transform(block);  
  133. }  
  134. index = 0;  
  135. else  
  136. i = 0;  
  137. // /* Buffer remaining input */  
  138. md5Memcpy(buffer, inbuf, index, i, inputLen - i);  
  139. }  
  140. private void md5Final() {  
  141. byte[] bits = new byte[8];  
  142. int index, padLen;  
  143. // /* Save number of bits */  
  144. Encode(bits, count, 8);  
  145. // /* Pad out to 56 mod 64.  
  146. index = (int) (count[0] >>> 3) & 0x3f;  
  147. padLen = (index < 56) ? (56 - index) : (120 - index);  
  148. md5Update(PADDING, padLen);  
  149. // /* Append length (before padding) */  
  150. md5Update(bits, 8);  
  151. // /* Store state in digest */  
  152. Encode(digest, state, 16);  
  153. }  
  154. private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,  
  155. int len) {  
  156. int i;  
  157. for (i = 0; i < len; i++)  
  158. output[outpos + i] = input[inpos + i];  
  159. }  
  160. private void md5Transform(byte block[]) {  
  161. long a = state[0], b = state[1], c = state[2], d = state[3];  
  162. long[] x = new long[16];  
  163. Decode(x, block, 64);  
  164. /* Round 1 */  
  165. a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */  
  166. d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */  
  167. c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */  
  168. b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */  
  169. a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */  
  170. d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */  
  171. c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */  
  172. b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */  
  173. a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */  
  174. d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */  
  175. c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */  
  176. b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */  
  177. a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */  
  178. d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */  
  179. c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */  
  180. b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */  
  181. /* Round 2 */  
  182. a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */  
  183. d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */  
  184. c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */  
  185. b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */  
  186. a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */  
  187. d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */  
  188. c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */  
  189. b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */  
  190. a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */  
  191. d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */  
  192. c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */  
  193. b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */  
  194. a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */  
  195. d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */  
  196. c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */  
  197. b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */  
  198. /* Round 3 */  
  199. a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */  
  200. d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */  
  201. c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */  
  202. b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */  
  203. a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */  
  204. d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */  
  205. c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */  
  206. b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */  
  207. a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */  
  208. d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */  
  209. c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */  
  210. b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */  
  211. a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */  
  212. d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */  
  213. c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */  
  214. b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */  
  215. /* Round 4 */  
  216. a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */  
  217. d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */  
  218. c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */  
  219. b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */  
  220. a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */  
  221. d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */  
  222. c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */  
  223. b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */  
  224. a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */  
  225. d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */  
  226. c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */  
  227. b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */  
  228. a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */  
  229. d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */  
  230. c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */  
  231. b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */  
  232. state[0] += a;  
  233. state[1] += b;  
  234. state[2] += c;  
  235. state[3] += d;  
  236. }  
  237. private void Encode(byte[] output, long[] input, int len) {  
  238. int i, j;  
  239. for (i = 0, j = 0; j < len; i++, j += 4) {  
  240. output[j] = (byte) (input[i] & 0xffL);  
  241. output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);  
  242. output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);  
  243. output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);  
  244. }  
  245. }  
  246. private void Decode(long[] output, byte[] input, int len) {  
  247. int i, j;  
  248. for (i = 0, j = 0; j < len; i++, j += 4)  
  249. output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)  
  250. | (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);  
  251. return;  
  252. }  
  253. public static long b2iu(byte b) {  
  254. return b < 0 ? b & 0x7F + 128 : b;  
  255. }  
  256. public static String byteHEX(byte ib) {  
  257. char[] Digit = { '0''1''2''3''4''5''6''7''8''9''A',  
  258. 'B''C''D''E''F' };  
  259. char[] ob = new char[2];  
  260. ob[0] = Digit[(ib >>> 4) & 0X0F];  
  261. ob[1] = Digit[ib & 0X0F];  
  262. String s = new String(ob);  
  263. return s;  
  264. }  
  265. public static String getBase64String(byte[] bytes) {  
  266. return new BASE64Encoder().encode(bytes);  
  267. }  
  268. public static byte[] getBase64DecondigString(String base64String) {  
  269. try {  
  270. return new BASE64Decoder().decodeBuffer(base64String);  
  271. catch (IOException e) {  
  272. return null;  
  273. }  
  274. }  
  275.      
  276. public static Object getBase64Object(String objectValue) {  
  277. try {  
  278. byte[] objBytes = new BASE64Decoder().decodeBuffer(objectValue);  
  279. ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(  
  280. objBytes);  
  281. ObjectInputStream inputStream = new ObjectInputStream(  
  282. byteArrayInputStream);  
  283. return inputStream.readObject();  
  284. catch (Exception e) {  
  285. return null;  
  286. }  
  287. }  
  288. public static String getBase64ObjectString(Serializable serObject) {  
  289. try {  
  290. ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();  
  291. ObjectOutputStream outputStream = new ObjectOutputStream(  
  292. arrayOutputStream);  
  293. outputStream.writeObject(serObject);  
  294. return new BASE64Encoder().encode(arrayOutputStream.toByteArray());  
  295. catch (Exception e) {  
  296. e.printStackTrace();  
  297. }  
  298. return null;  
  299. }  
  300. public static String getUUIDFromDateTime(Date date) {  
  301. if(date == null)  
  302. date = new Date();  
  303. String uuid = UUID.nameUUIDFromBytes(Long.valueOf(date.getTime()).toString().getBytes()).toString();  
  304. return uuid.replaceAll("-""");  
  305. }  
  306. public static String getRandomUUID() {  
  307. String uuid = UUID.randomUUID().toString();  
  308. return uuid.replaceAll("-""");  
  309. }  
  310. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值