MD5加密(简写)

直接贴代码吧:

 1 import java.security.MessageDigest;
 2 import java.security.NoSuchAlgorithmException;
 3 public class Md5 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         
10         String str = "";
11         MessageDigest md5 = null;
12         try {
13             md5 = MessageDigest.getInstance("MD5");
14             //返回实现指定摘要算法的 MessageDigest对象
15         } catch (NoSuchAlgorithmException e) {
16             e.printStackTrace();
17             System.out.println("error");
18         }
19         
20         char[] charArray = str.toCharArray();
21         byte[] byteArray = new byte[charArray.length];
22         
23         for(int i=0;i<charArray.length;i++){
24             byteArray[i] = (byte)charArray[i];
25         }
26         byte[] md5Bytes = md5.digest(byteArray);
27         
28         StringBuffer hexValue = new StringBuffer();
29         for(int i = 0; i<md5Bytes.length; i++){
30             int val = ((int)md5Bytes[i]&0xff);
31             if(val < 16){
32                 hexValue.append("0");
33             }
34             hexValue.append(Integer.toHexString(val));
35         }
36         System.out.println("md5 code:"+hexValue.toString());
37         
38     }
39 
40 }

 

posted @ 2013-02-26 11:07  黄泉hj  阅读(320)  评论(0编辑  收藏  举报
沪江在线词典