MD5加密

 1 package com.estone.manage.districtTest.action;
 2 
 3 import java.security.MessageDigest;
 4 import java.security.NoSuchAlgorithmException;
 5 
 6 public class Md5 {
 7     
 8     private final static String[] strs ={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
 9     
10     public Md5(){
11         
12     }
13     
14     //返回形式为数字和字符串
15     public static String byteToArrayString(byte bByte){
16         int iInt = bByte;
17         System.out.println("iInt----------------"+iInt);
18         if(iInt<0){
19             iInt+=256;
20         }
21         int d1 = iInt/16;
22         int d2 = iInt%16;
23         return strs[d1]+strs[d2];
24     }
25     
26     //返回形式只为数字
27     public static String byteToNum(byte bByte){
28         int iInt0 = bByte;
29         System.out.println("iInt0----------------"+iInt0);
30         if(iInt0<0){
31             iInt0+=256;
32         }
33         
34         return String.valueOf(iInt0);
35     }
36     
37     //转换字节数组为十六进制字符串
38     public static String byteToString(byte[] bByte){
39         StringBuffer sBuffer = new StringBuffer();
40         for(int i=0;i<bByte.length;i++){
41             sBuffer.append(byteToArrayString(bByte[i]));
42             System.out.println("byteToArrayString(bByte[i])----------------"+byteToArrayString(bByte[i]));
43         }
44         
45         return sBuffer.toString();
46     }
47     
48     public static String getMd5Code(String str){
49         String resultString = null;
50         try {
51             resultString = new String(str);
52             MessageDigest md = MessageDigest.getInstance("MD5");
53             resultString = byteToString(md.digest(str.getBytes()));
54             
55         } catch (NoSuchAlgorithmException e) {
56             // TODO Auto-generated catch block
57             e.printStackTrace();
58         }
59         return resultString;
60     }
61     public static void main(String [] args){
62         System.out.println(Md5.getMd5Code("yang"));
63     }
64 }    
View Code
原文地址:https://www.cnblogs.com/yang1018/p/7205291.html