Java MD5算法

package com.learn.servlet.control;

import java.security.MessageDigest;

public class HashMD5 {
   public final static char hexChar[] = {
     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 
   public static String convertBytesToString(byte[] bytes) {
     StringBuffer result = new StringBuffer();
     for (int i=0; i<bytes.length; i++) {
       result.append(convertByteToHex(bytes[i]));
     }
     return result.toString();
   }
   private static String convertByteToHex(byte b) {
     int n = b;
     int d1 = 0;
     int d2 = 0;
     if (n<0){
       n = 256 + n;              
     }
     d1 = n/16;
     d2 = n%16;
     StringBuffer str = new StringBuffer();
     str.append(hexChar[d1]).append(hexChar[d2]);
     return str.toString();
   }
 
   public static String Encryption(String inputpassword) {
     String encryptedPassword = null;
     try {
       byte[] strTmp = inputpassword.getBytes();
       MessageDigest mdTmp = MessageDigest.getInstance("MD5");
       mdTmp.update(strTmp);
       byte[] md = mdTmp.digest();
       encryptedPassword = convertBytesToString (md);
       return new String(encryptedPassword);
     }
     catch (Exception e){
     return null;
     }
   }
 }

你可能感兴趣的:(Java MD5算法)