MD5加密在java中使用

自己写好工具类(直接使用),不用引入额外的jar包;

package cn.com.txlian.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {

 public static String encode(String plainText){
  
  try {
   if(plainText != null){
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(plainText.getBytes());
    byte b[] = md.digest();
    int i;
    StringBuffer buf = new StringBuffer("");
    for(int offset=0;offset<b.length;offset++){
     i = b[offset];
     if(i<0) i += 256;
     if(i<16) buf.append("0");
     buf.append(Integer.toHexString(i));
    }
    return buf.toString();
   }
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  }
  return null;
 }
}

猜你喜欢

转载自qihaha.iteye.com/blog/2068511