MD5加密

来源:互联网 发布:nginx ip映射域名 编辑:程序博客网 时间:2024/06/05 19:26
  1. import java.security.MessageDigest;  
  2. public class MD5Util {  
  3.     public final static String MD5(String s) {  
  4.         char hexDigits[] = { '0''1''2''3''4',  
  5.                              '5''6''7''8''9',  
  6.                              'A''B''C''D''E''F' };  
  7.         try {  
  8.             byte[] btInput = s.getBytes();  
  9.      //获得MD5摘要算法的 MessageDigest 对象  
  10.             MessageDigest mdInst = MessageDigest.getInstance("MD5");  
  11.      //使用指定的字节更新摘要  
  12.             mdInst.update(btInput);  
  13.      //获得密文  
  14.             byte[] md = mdInst.digest();  
  15.      //把密文转换成十六进制的字符串形式  
  16.             int j = md.length;  
  17.             char str[] = new char[j * 2];  
  18.             int k = 0;  
  19.             for (int i = 0; i < j; i++) {  
  20.                 byte byte0 = md[i];  
  21.                 str[k++] = hexDigits[byte0 >>> 4 & 0xf];  
  22.                 str[k++] = hexDigits[byte0 & 0xf];  
  23.             }  
  24.             return new String(str);  
  25.         }  
  26.         catch (Exception e) {  
  27.             e.printStackTrace();  
  28.             return null;  
  29.         }  
  30.     }  
  31.     public static void main(String[] args) {  
  32.         System.out.print(MD5Util.MD5("password"));  
  33.     }  
0 0