*典型的不可逆的加密方式;
*丢失数据精度的加密.
用途:在页面密码输入时进行MD5加密,让数据库中存储的的密码不再是明文,在校验登录的时候,对比加密后的内容是否一次.存的时候进行加密,对比的时候也需要加密.
public class MD5Utils {
public static String encrypt(String plainText) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());
System.out.println("secretBytes:" + secretBytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
String code = new BigInteger(1,secretBytes).toString(16); //转换成16进制的数字
//如果生成的数字不满32位,则在前面补0.
for (int i = 0; i < 32-code.length(); i++) {
code = "0" + code;
}
return code;
}
}