MD5加密是这样的过程:用户输入密码转入后台,然后把该密码转为MD5后的加密字符,如果这串字符与数据库存的那串字符相匹配那就是正确了,反之错误。所以说数据库存的密码字符串也是加密过的,就算数据信息暴露也取不用户密码信息了。以下是一个JAVA的MD5应用实例,拷贝即可用了。
import org.apache.commons.codec.binary.Base64;
import java.security.MessageDigest;
public class MD5Utils {
/**
* @Description: 对字符串进行md5加密
*/
public static String getMD5Str(String strValue) throws Exception {
MessageDigest md5 = MessageDigest.getInstance("MD5");
String newstr = Base64.encodeBase64String(md5.digest(strValue.getBytes()));
return newstr;
}
public static void main(String[] args) {
try {
String md5 = getMD5Str("imooc");
System.out.println(md5);
} catch (Exception e) {
e.printStackTrace();
}
}
}
编译运行结果如下:
kU8h64TG/bK2Y91vRT9lyg==
进程完成,退出码 0