MD5的js插件下载:http://pan.baidu.com/s/1pLD6dL5
下载完成后,将其放到放置javascript的地方,并在需要加密的网页head上引用,下面做一个网页的简易版来说明使用方法:
注册页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="md5.js"></script>//插件的引用
</head>
<body>
<body>
<h1>用户注册</h1>
<form action="Register" method="post">
<table>
<tr>
<td><label for="realName">您的名字</label></td>
<td><input type="text" name="realName" id="realName"/></td>
</tr>
<tr>
<td><label for="userName">用户名</label></td>
<td><input type="text" name="userName" id="userName"/></td>
</tr>
<tr>
<td><label for="psd"></label>口令</td>
<td><input type="password" name="psd" id="psd"/></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<input type="submit" value="注册" onclick="return MD5Encrypt()"/>//在注册按钮中设置click事件会调用的函数
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
function MD5Encrypt(){//click调用的函数,主要作用是在点击按钮后将密码框的值加密后替换
document.getElementById("psd").value=MD5(document.getElementById("psd").value);
}
</script>
</body>
</html>
这样,在提交到后台servlet的时候,密码框中的值已经被取出、加密、替换了,servlet接收到的密码是一串32位字符串,存到相应的数据库
登录页面:
<%@ 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>Insert title here</title>
<script type="text/javascript" src="md5.js"></script>//插件的引用
</head>
<body>
<h1>用户登录</h1>
<form action="LoginCheck" method="post">
<table>
<tr>
<td><label for="userName">用户名</label></td>
<td><input type="text" name="userName" id="userName"/></td>
</tr>
<tr>
<td><label for="psd"></label>口令</td>
<td><input type="password" name="psd" id="psd"/></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<input type="submit" value="登录" onclick="return MD5Encrypt()"/>//在登录按钮中设置click事件会调用的函数
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
function MD5Encrypt(){//click调用的函数,主要作用是在点击按钮后将密码框的值加密后替换
document.getElementById("psd").value=MD5(document.getElementById("psd").value);
}
</script>
</body>
</html>
登录后加密与注册加密原理相同,登录后传递的数据会与数据库中记录的密码做匹配,相同的密码加密后的字符串也相同