在JSP网页中用MD5加密

--------------------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>&nbsp;</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">&nbsp;</td>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td width="17%" height="26" class="9point">&nbsp;&nbsp;用户名:</td>
            <td width="83%" height="26"> <input name="username" type="text" class="uloginuser" id="username"></td>
          </tr>
          <tr>
            <td height="26" class="9point">&nbsp;&nbsp;密码:</td>
            <td height="26"><input name="password" type="password" class="uloginuser" id="password"></td>
          </tr>
          <tr>
            <td height="26">&nbsp;</td>
            <td height="26"><input type="submit" name="Submit" value="提交"> &nbsp;
            </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;
  }
 */


---------------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值