在一个Web系统中,如果注册用户的密码直接以明文存储,则数据库管理员可以查看所有用户的任意信息,包括密码。如果数据库被黑客入侵,用户信息就会轻易的被泄露。为了加强数据的安全性,我们通常会把用户信息通过一定算法加密之后,在将其进行存储到数据库中。MD5算法就是若干加密算法中被广泛使用的一个,自己写程序的话,用这个加密算法也是不错的选择。
目录
一、MD5加密算法
1.MD5是一种加密算法,它能够对字节数组进行加密。
2.MD5的特点:
①、不能根据加密后的信息(密文)来获取加密前的信息(明文)。因为它的加密过程本身就是一个有损的加密过程,因此很难还原。
②、对于不同的明文,加密后的密文也是不同的。
二、MD5算法实现
1.在Java类库中,java.security.MessageDigest和sum.misc.BASE64Encoder类提供了对MD5算法实现的支持,因此不用了解MD5算法的细节就能实现它。
2.将加密的字符串转换为字节数组。
3.获取MessageDigest对象,利用该对象digest方法完成加密,返回字节数组。
4.将字符数组利用base64算法转换成等长的字符串。
三、MD5算法实例--对字符串完成加密
我采用的是MVC的开发模式,一共分为四部分,一个javabean类,一个servlet类,两个jsp文件。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>数据提交</title>
</head>
<body>
<p>数据提交</p>
<hr>
<form method="post" action="../a02a_huan">
<table>
<tr><td>信息:</td><td><input name="xinxi"/></td></tr>
<tr><td><input type="submit" value="确认"/></td><td><input type="reset" value="取消"/></td></tr>
</table>
</form>
</body>
</html>
package a03a;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
public class a03a_zhuan {
private String xinxi;
public a03a_zhuan() {
super();
// TODO Auto-generated constructor stub
}
public a03a_zhuan(String xinxi) {
super();
this.xinxi = xinxi;
}
public String getXinxi() {
return xinxi;
}
public void setXinxi(String xinxi) {
this.xinxi = xinxi;
}
//完成对MD5算法的书写
public String md5(String s) throws NoSuchAlgorithmException {
byte[] old = s.getBytes();
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
byte[] newbytes = md.digest(old);
BASE64Encoder encoder = new BASE64Encoder();
String newstr = encoder.encode(newbytes);
return newstr;
} catch (NoSuchAlgorithmException e) {
return null;
}
}
}
package a03a;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class a02a_huan
*/
@WebServlet("/a02a_huan")
public class a03a_huan extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public a03a_huan() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
String a=request.getParameter("xinxi");
a03a_zhuan zhuan=new a03a_zhuan(a);
try {
//调用函数完成加密的最终运算,并将数据传到jsp文件
request.setAttribute("xinxi", zhuan.md5(zhuan.getXinxi()));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getRequestDispatcher("/a03a/a03a_show.jsp").forward(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>显示信息</title>
</head>
<body>
<p>显示信息</p>
<hr>
<!-- 获取servlet类传递的数据 -->
<%=(String)request.getAttribute("xinxi")%>
</body>
</html>