使用MD5加密的两种方式:
1.使用MessageDigest类
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.commons.codec.binary.Hex; public String encode(String passWord) { // TODO Auto-generated method stub String algorithm ="MD5"; MessageDigest instance = null; try { instance = MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } byte[] digest = instance.digest(passWord.getBytes());// 纯md5加密 char[] encodeHex = Hex.encodeHex(digest);//16进制加密 String string = new String(encodeHex); return string; }
2.使用Spring自带的工具类DigestUtils
import org.springframework.util.DigestUtils; //对密码进行 md5 加密 String md5Password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());