首页 > 代码库 > MD5加密

MD5加密

import java.security.MessageDigest;
import java.util.*;
public class Hello2{
  public static void main(String[] args) throws Exception{
     Random r = new Random();//从用户的用户名中解析出一个随机数
     int ran  = 1+r.nextInt(10);
     System.out.println("ran is:"+ran);
     //即使是相同的密码,加密的结果也不相同。salt盐值

     String pwd = "";
     MessageDigest md5 = MessageDigest.getInstance("MD5");  
     byte[] bt = md5.digest(pwd.getBytes());
     StringBuffer sb = new StringBuffer();
     for(int i=0;i<bt.length;i++){
        int mm = bt>>ran;
        mm = Math.abs(mm);
if(mm<16){
           mm+=16;
}
        String hex = Integer.toHexString(mm);
System.out.println("mm:"+mm+","+hex);
        sb.append(hex);
     }
     System.out.println(sb.toString()+","+sb.toString().length());
  }
}