注册 | 登录 | 设为首页 | 加入收藏
您当前的位置:飞翔学院-IT中国 → 编程开发JSP → 文章内容

在JSP中如何实现MD5加密

作者:佚名 来源:不详 发布时间:2007-11-30 0:08:29

       /*Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的,
          只拆低32bit,以适应原始C实现的用途
        */
        private void Encode (byte[] output, long[] input, int len) {
                int i, j;

                for (i = 0, j = 0; j < len; i++, j += 4) {
                        output[j] = (byte)(input[i] & 0xffL);
                        output[j + 1] = (byte)((input[i] >>> 8) & 0xffL);
                        output[j + 2] = (byte)((input[i] >>> 16) & 0xffL);
                        output[j + 3] = (byte)((input[i] >>> 24) & 0xffL);
                }
        }

        /*Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的,
          只合成低32bit,高32bit清零,以适应原始C实现的用途
        */
        private void Decode (long[] output, byte[] input, int len) {
                int i, j;

                for (i = 0, j = 0; j < len; i++, j += 4)
                        output[i] = b2iu(input[j]) |
                                (b2iu(input[j + 1]) << 8) |
                                (b2iu(input[j + 2]) << 16) |
                                (b2iu(input[j + 3]) << 24);

                return;
        }
       
        /*
          b2iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算
        */
        public static long b2iu(byte b) {
                return b < 0 ? b & 0x7F + 128 : b;
        }
       
/*byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示,
 因为java中的byte的toString无法实现这一点,我们又没有C语言中的
 sprintf(outbuf,"%02X",ib)
*/
        public static String byteHEX(byte ib) {
                char[] Digit = { '0','1','2','3','4','5','6','7','8','9',
                'A','B','C','D','E','F' };
                char [] ob = new char[2];
                ob[0] = Digit[(ib >>> 4) & 0X0F];
                ob[1] = Digit[ib & 0X0F];
                String s = new String(ob);
                return s;
        }

        public static void main(String args[]) {

                MD5 m = new MD5();
                if (Array.getLength(args) == 0) {   //如果没有参数,执行标准的Test Suite
               
                        System.out.println("MD5 Test suite:");
                System.out.println("MD5(\"\"):"+m.getMD5ofStr(""));
                System.out.println("MD5(\"a\"):"+m.getMD5ofStr("a"));
                System.out.println("MD5(\"abc\"):"+m.getMD5ofStr("abc"));
                System.out.println("MD5(\"message digest\"):"+m.getMD5ofStr("message digest"));
                System.out.println("MD5(\"abcdefghijklmnopqrstuvwxyz\"):"+
                        m.getMD5ofStr("abcdefghijklmnopqrstuvwxyz"));
                System.out.println("MD5(\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\"):"+
                      m.getMD5ofStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));
                }
                else
                      System.out.println("MD5(" + args[0] + ")=" + m.getMD5ofStr(args[0]));
               
         
        }

}

JSP中的使用方法

-------------------------------------------------------------------------------
<%@ page language='java' %>
<jsp:useBean id='oMD5' scope='request' class='beartool.MD5'/>

<%@ page import='java.util.*'%>
<%@ page import='java.sql.*'%>
<html>
<body>
<%
  String userid = request.getParameter("UserID");  //获取用户输入UserID
  String password = request.getParameter("Password"); //获取用户输入的Password
 
  String pwdmd5 = oMD5.getMD5ofStr(password);  //计算MD5的值
 
  PrintWriter rp = response.getWriter();
 
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 
  Connection con = DriverManager.getConnection("jdbc:odbc:community", "", "");

  Statement stmt = con.createStatement();

  ResultSet rs = stmt.executeQuery("select * from users where userID ='"+userid+"' and pwdmd5= '" + pwdmd5+"'" );

  if (rs.next())
    {
      rp.print("Login OK");
           
    }
  else
    {
      rp.print("Login Fail");
    }

  stmt.close();
  con.close();
 
%>

</body>

</html>



上一页  [1] [2] [3] [4] 


  • 打印文档
  • 推荐好友
  • 返回顶部
  • 增大字体
  • 减少字体
关于本站 | 工作机会 | 合作网站 | 广告服务 | 市场合作| 联系我们 | 抽奖活动
版权所有: 武汉威俊科技有限公司 Copyright 2005-2007 www.ITCNW.COM All rights reserved