ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

MD5加密解密工具

2020-12-04 11:34:26  阅读:160  来源: 互联网

标签:sb return String int text 解密 b3 加密 MD5


  1 import java.io.ByteArrayOutputStream;
  2 import java.io.UnsupportedEncodingException;
  3 import java.net.URLDecoder;
  4 import java.net.URLEncoder;
  5 import java.security.MessageDigest;
  6 
  7 /**
  8  2020年12月04日
  9  **/
 10 public class MD5_Base64 {
 11 
 12     public static void main(String[] args)  {
 13 
 14         String text ="123456";
 15         System.out.println("md5:"+MD5_Base64.EncryptionByMD5(text));
 16         System.out.println("Base64:"+MD5_Base64.EncryptionByBase64(text));
 17         System.out.println("md5_Base64:"+MD5_Base64.EncryptionByMD5AndBase64(text)+"\n");
 18 
 19         String text2 =MD5_Base64.EncryptionByBase64(text);//MTIzNDU2
 20         System.out.println(MD5_Base64.decryptBase64(text2)+"\n");
 21 
 22         String text3 =MD5_Base64.EncryptionByMD5AndBase64(text);//4QrcOUm6Wau+VuBX8g+IPg==
 23         System.out.println(MD5_Base64.decryptBase64To(text3)+"\n");
 24 
 25     }
 26 
 27     private final static char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
 28     private static char[] base64EncodeChars = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
 29             'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
 30             'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
 31 
 32     private static byte[] base64DecodeChars = new byte[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 33             -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55,
 34             56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
 35             21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
 36             47, 48, 49, 50, 51, -1, -1, -1, -1, -1 };
 37     /**
 38      * MD5加密
 39      * @param text
 40      * @return String
 41      */
 42     public static String EncryptionByMD5(String text){
 43         try {
 44             byte[] btInput = text.getBytes();
 45             // 获得MD5摘要算法的 MessageDigest 对象
 46             MessageDigest mdInst = MessageDigest.getInstance("MD5");
 47             // 使用指定的字节更新摘要
 48             mdInst.update(btInput);
 49             // 获得密文
 50             byte[] md = mdInst.digest();
 51             // 把密文转换成十六进制的字符串形式
 52             int j = md.length;
 53             char str[] = new char[j * 2];
 54             int k = 0;
 55             for (int i = 0; i < j; i++) {
 56                 byte byte0 = md[i];
 57                 str[k++] = hexDigits[byte0 >>> 4 & 0xf];
 58                 str[k++] = hexDigits[byte0 & 0xf];
 59             }
 60             return new String(str);
 61         } catch (Exception e) {
 62             e.printStackTrace();
 63             return null;
 64         }
 65 
 66     }
 67     /**
 68      * MD5加密
 69      * @param text
 70      * @return byte[]
 71      */
 72     public static byte[] EncryptionByMD5T(String text){
 73         try {
 74             byte[] btInput = text.getBytes();
 75             // 获得MD5摘要算法的 MessageDigest 对象
 76             MessageDigest mdInst = MessageDigest.getInstance("MD5");
 77             // 使用指定的字节更新摘要
 78             mdInst.update(btInput);
 79             // 获得密文
 80             byte[] md = mdInst.digest();
 81             return md;
 82         } catch (Exception e) {
 83             e.printStackTrace();
 84             return null;
 85         }
 86 
 87     }
 88     /**
 89      * Base64加密
 90      * @param text
 91      * @return String
 92      */
 93     public static String EncryptionByBase64(String text) {
 94         byte[] data=text.getBytes();
 95         StringBuffer sb = new StringBuffer();
 96         int len = data.length;
 97         int i = 0;
 98         int b1, b2, b3;
 99 
100         while (i < len) {
101             b1 = data[i++] & 0xff;
102             if (i == len) {
103                 sb.append(base64EncodeChars[b1 >>> 2]);
104                 sb.append(base64EncodeChars[(b1 & 0x3) << 4]);
105                 sb.append("==");
106                 break;
107             }
108             b2 = data[i++] & 0xff;
109             if (i == len) {
110                 sb.append(base64EncodeChars[b1 >>> 2]);
111                 sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
112                 sb.append(base64EncodeChars[(b2 & 0x0f) << 2]);
113                 sb.append("=");
114                 break;
115             }
116             b3 = data[i++] & 0xff;
117             sb.append(base64EncodeChars[b1 >>> 2]);
118             sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
119             sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]);
120             sb.append(base64EncodeChars[b3 & 0x3f]);
121         }
122         return sb.toString();
123     }
124 
125     /**
126      * Base64加密
127      * @param data
128      * @return String
129      */
130     public static String EncryptionByBase64(byte[] data) {
131         StringBuffer sb = new StringBuffer();
132         int len = data.length;
133         int i = 0;
134         int b1, b2, b3;
135 
136         while (i < len) {
137             b1 = data[i++] & 0xff;
138             if (i == len) {
139                 sb.append(base64EncodeChars[b1 >>> 2]);
140                 sb.append(base64EncodeChars[(b1 & 0x3) << 4]);
141                 sb.append("==");
142                 break;
143             }
144             b2 = data[i++] & 0xff;
145             if (i == len) {
146                 sb.append(base64EncodeChars[b1 >>> 2]);
147                 sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
148                 sb.append(base64EncodeChars[(b2 & 0x0f) << 2]);
149                 sb.append("=");
150                 break;
151             }
152             b3 = data[i++] & 0xff;
153             sb.append(base64EncodeChars[b1 >>> 2]);
154             sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
155             sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]);
156             sb.append(base64EncodeChars[b3 & 0x3f]);
157         }
158         return sb.toString();
159     }
160 
161 
162     /**
163      * 先MD5加密,再base64加密
164      * @param text
165      * @return String
166      */
167     public static String EncryptionByMD5AndBase64(String text){
168 
169         return EncryptionByBase64(EncryptionByMD5T(text));
170     }
171 
172     /**
173      * Base64 解密
174      * @return String
175      */
176     public static String decryptBase64(String text) {
177         byte[] data = text.getBytes();
178         int len = data.length;
179         ByteArrayOutputStream buf = new ByteArrayOutputStream(len);
180         int i = 0;
181         int b1, b2, b3, b4;
182 
183         while (i < len)
184         {
185 
186             /* b1 */
187             do
188             {
189                 b1 = base64DecodeChars[data[i++]];
190             }
191             while (i < len && b1 == -1);
192             if (b1 == -1)
193             {
194                 break;
195             }
196 
197             /* b2 */
198             do
199             {
200                 b2 = base64DecodeChars[data[i++]];
201             }
202             while (i < len && b2 == -1);
203             if (b2 == -1)
204             {
205                 break;
206             }
207             buf.write((int) ((b1 << 2) | ((b2 & 0x30) >>> 4)));
208 
209             /* b3 */
210             do
211             {
212                 b3 = data[i++];
213                 if (b3 == 61)
214                 {
215                     return new String(buf.toByteArray());
216                 }
217                 b3 = base64DecodeChars[b3];
218             }
219             while (i < len && b3 == -1);
220             if (b3 == -1)
221             {
222                 break;
223             }
224             buf.write((int) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));
225 
226             /* b4 */
227             do
228             {
229                 b4 = data[i++];
230                 if (b4 == 61)
231                 {
232                     return new String(buf.toByteArray());
233                 }
234                 b4 = base64DecodeChars[b4];
235             }
236             while (i < len && b4 == -1);
237             if (b4 == -1)
238             {
239                 break;
240             }
241             buf.write((int) (((b3 & 0x03) << 6) | b4));
242 
243         }
244 
245         return new String(buf.toByteArray());
246 
247     }
248 
249     /**
250      * Base64 解密到MD5
251      * @return String
252      */
253     public static String decryptBase64To(String text) {
254         byte[] data = text.getBytes();
255         int len = data.length;
256         ByteArrayOutputStream buf = new ByteArrayOutputStream(len);
257         int i = 0;
258         int b1, b2, b3, b4;
259 
260         while (i < len)
261         {
262 
263             /* b1 */
264             do
265             {
266                 b1 = base64DecodeChars[data[i++]];
267             }
268             while (i < len && b1 == -1);
269             if (b1 == -1)
270             {
271                 break;
272             }
273 
274             /* b2 */
275             do
276             {
277                 b2 = base64DecodeChars[data[i++]];
278             }
279             while (i < len && b2 == -1);
280             if (b2 == -1)
281             {
282                 break;
283             }
284             buf.write((int) ((b1 << 2) | ((b2 & 0x30) >>> 4)));
285 
286             /* b3 */
287             do
288             {
289                 b3 = data[i++];
290                 if (b3 == 61)
291                 {
292                     byte[] br = buf.toByteArray();
293                     // 把密文转换成十六进制的字符串形式
294                     int p = br.length;
295                     char str[] = new char[p * 2];
296                     int k = 0;
297                     for (int r = 0; r < p; r++) {
298                         byte byte0 = br[r];
299                         str[k++] = hexDigits[byte0 >>> 4 & 0xf];
300                         str[k++] = hexDigits[byte0 & 0xf];
301                     }
302                     return new String(str);
303                 }
304                 b3 = base64DecodeChars[b3];
305             }
306             while (i < len && b3 == -1);
307             if (b3 == -1)
308             {
309                 break;
310             }
311             buf.write((int) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));
312 
313             /* b4 */
314             do
315             {
316                 b4 = data[i++];
317                 if (b4 == 61)
318                 {
319                     byte[] br = buf.toByteArray();
320                     // 把密文转换成十六进制的字符串形式
321                     int p = br.length;
322                     char str[] = new char[p * 2];
323                     int k = 0;
324                     for (int r = 0; r < p; r++) {
325                         byte byte0 = br[r];
326                         str[k++] = hexDigits[byte0 >>> 4 & 0xf];
327                         str[k++] = hexDigits[byte0 & 0xf];
328                     }
329                     return new String(str);
330                 }
331                 b4 = base64DecodeChars[b4];
332             }
333             while (i < len && b4 == -1);
334             if (b4 == -1)
335             {
336                 break;
337             }
338             buf.write((int) (((b3 & 0x03) << 6) | b4));
339 
340         }
341         byte[] br = buf.toByteArray();
342         // 把密文转换成十六进制的字符串形式
343         int p = br.length;
344         char str[] = new char[p * 2];
345         int k = 0;
346         for (int r = 0; r < p; r++) {
347             byte byte0 = br[r];
348             str[k++] = hexDigits[byte0 >>> 4 & 0xf];
349             str[k++] = hexDigits[byte0 & 0xf];
350         }
351         return new String(str);
352     }
353 
354     /**
355      * 网络传输中文UTF-8加密,防止乱码
356      * @param text
357      * @return String
358      */
359     public static String netEnCoderUTF8(String text){
360         return netEnCoder(text,"UTF-8");
361     }
362 
363     /***
364      * 网络传输中文UTF-8解码
365      * @param text
366      * @return String
367      */
368     public static String netEDeCoderUTF8(String text){
369         return netDecoder(text,"UTF-8");
370     }
371 
372 
373     /**
374      *
375      * @param text
376      * @param charsetName 字符集
377      * @return String
378      */
379     public static String netEnCoder(String text,String charsetName){
380         String result=null;
381         try {
382             result= URLEncoder.encode(text,charsetName);
383         } catch (UnsupportedEncodingException e) {
384             e.printStackTrace();
385         }
386         return result;
387     }
388 
389     /**
390      *
391      * @param text
392      * @param charsetName 字符集
393      * @return String
394      */
395     public static String netDecoder(String text,String charsetName){
396         String result=null;
397         try {
398             result= URLDecoder.decode(text,charsetName);
399         } catch (UnsupportedEncodingException e) {
400             e.printStackTrace();
401         }
402         return result;
403     }
404 
405 }

 

标签:sb,return,String,int,text,解密,b3,加密,MD5
来源: https://www.cnblogs.com/zengsiqi/p/14084909.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有