--------------------MD5加密-----------
public String encodePassword(String password)
{
try {
MessageDigest sha = MessageDigest.getInstance("SHA-1");
sha.update(password.getBytes());
return new BASE64Encoder().encode(sha.digest());
}catch(java.security.NoSuchAlgorithmException e) {
e.printStackTrace();//打印错误信息;
return null;
}
}
------------------------------------------------------------------------------------
加密后的字符串你保存在你的数据库字段中
后面将来登录校验,大概就是如下啦:
String user = request.getParameter("user");
String password = request.getParameter("password");
String encPass = encodePassword(password);
然后执行类似的SQL语句
prep = conn.prepareStatement("select ID,name from users where name=? and password=?");
prep.setString(1,user);
prep.setString(2,encPass);
ResultSet rs = prep.executeQuery();
如果rs.next(),那么就是登录成功。如果false则登录不成功啊。
------------------------------------------------------------------------------------
我是这样写的
reg.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>
<body background="../netcenter/images/left_background.jpg" leftmargin="0" topmargin="0">
<p> </p><table width="55%" height="108" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="108" valign="top">
<form method="post" action="reg_ok.jsp">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr bgcolor="">
<td height="30" colspan="2"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="1">
<tr>
<td bgcolor="#648fd7"> </td>
</tr>
</table></td>
</tr>
<tr>
<td width="17%" height="26" class="9point"> 用户名:</td>
<td width="83%" height="26"> <input name="username" type="text" class="uloginuser" id="username"></td>
</tr>
<tr>
<td height="26" class="9point"> 密码:</td>
<td height="26"><input name="password" type="password" class="uloginuser" id="password"></td>
</tr>
<tr>
<td height="26"> </td>
<td height="26"><input type="submit" name="Submit" value="提交">
</td>
</tr>
</table>
</form ></td >
</tr>
</table>
</body>
</html>
reg_ok.jsp
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=gb2312" errorPage=""%>
<%@ page import="sun.misc.BASE64Encoder"%>
<%@ page import="java.security.MessageDigest"%>
<jsp:useBean id="connBean" scope="session" class="conn.DBconn" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<%!
public String encodePassword(String password)
{
try {
MessageDigest sha = MessageDigest.getInstance("SHA-1");
sha.update(password.getBytes());
return new BASE64Encoder().encode(sha.digest());
}catch(java.security.NoSuchAlgorithmException e) {
e.printStackTrace();//打印错误信息;
return null;
}
}
%>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String encPass = encodePassword(password);
String sql="insert userlogin (username,password) values('"+username+"','"+encPass+"')";
connBean.executeQuery(sql);
%>
</body>
</html>
------------------------------------------------------------------------------------
/**
* 对字符串进行md5加密
public String encodeMD5(String u_pwd)
{
String hs="";
String stmp="";
try{
byte[] a=u_pwd.getBytes();
MessageDigest mda=MessageDigest.getInstance("MD5");
mda.update(a);
byte[] da=mda.digest();
for (int n=0;n<da.length;n++)
{
stmp=(java.lang.Integer.toHexString(da[n] & 0XFF));
if (stmp.length()==1)
hs=hs+"0"+stmp;
else hs=hs+stmp;
}
hs=hs.toUpperCase();
}
catch(Exception e)
{
System.out.print(e);
}
return hs;
return u_pwd;
}
*/
---------------------------------------------------------------------------------