最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

JSP中如何实现MD5加密

日期:07-13    来源:中国设计秀    作者:cnwebshow.com

源码zDj中国设计秀

/**zDj中国设计秀
 * 类名:      md5Digest<br>zDj中国设计秀
 * 说明:   用来进行密码加密的md5公用参数<br>zDj中国设计秀
 * 编写日期:  2001/03/05<br>zDj中国设计秀
 * 修改者:    <br>zDj中国设计秀
 * 修改信息:  <br>zDj中国设计秀
 * @author     edgarlo edgarlo@china.comzDj中国设计秀
 * @version    1.0<br>zDj中国设计秀
 */zDj中国设计秀
 zDj中国设计秀
import java.security.MessageDigest;zDj中国设计秀
import java.security.NoSuchAlgorithmException;zDj中国设计秀

public class MD5DigestzDj中国设计秀
{zDj中国设计秀

    PRivate MessageDigest __md5 = null;zDj中国设计秀
    private StringBuffer __digestBuffer = null;zDj中国设计秀

    public MD5Digest()zDj中国设计秀
        throws NoSuchAlgorithmExceptionzDj中国设计秀
    {zDj中国设计秀
        __md5 = MessageDigest.getInstance("MD5");zDj中国设计秀
        __digestBuffer = new StringBuffer();zDj中国设计秀
    }zDj中国设计秀

    public String md5crypt(String s)zDj中国设计秀
    {zDj中国设计秀
        __digestBuffer.setLength(0);zDj中国设计秀
        byte abyte0[] = __md5.digest(s.getBytes());zDj中国设计秀
        for(int i = 0; i < abyte0.length; i++)zDj中国设计秀
            __digestBuffer.append(toHex(abyte0[i]));zDj中国设计秀

        return __digestBuffer.toString();zDj中国设计秀
    }zDj中国设计秀
    public String toHex(byte one){zDj中国设计秀
   String HEX="0123456789ABCDEF";zDj中国设计秀
   char[] result=new char[2];zDj中国设计秀
   result[0]=HEX.charAt((one & 0xf0) >> 4);zDj中国设计秀
   result[1]=HEX.charAt(one & 0x0f);zDj中国设计秀
   String mm=new String(result);zDj中国设计秀
   return mm;zDj中国设计秀
  }zDj中国设计秀
}zDj中国设计秀

 zDj中国设计秀

 zDj中国设计秀

 zDj中国设计秀

--------------------------------------------------------------------------------zDj中国设计秀
/************************************************zDj中国设计秀
MD5 算法的Java BeanzDj中国设计秀
@author:Topcat TuppinzDj中国设计秀
Last Modified:10,Mar,2001zDj中国设计秀
*************************************************/zDj中国设计秀
package beartool;zDj中国设计秀
import java.lang.reflect.*;zDj中国设计秀
/*************************************************zDj中国设计秀
md5 类实现了RSA Data Security, Inc.在提交给IETFzDj中国设计秀
的RFC1321中的MD5 message-digest 算法。zDj中国设计秀
*************************************************/zDj中国设计秀

public class MD5 {zDj中国设计秀
/* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的,zDj中国设计秀
这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个zDj中国设计秀
Instance间共享*/zDj中国设计秀
        static final int S11 = 7;zDj中国设计秀
        static final int S12 = 12;zDj中国设计秀
        static final int S13 = 17;zDj中国设计秀
        static final int S14 = 22;zDj中国设计秀

        static final int S21 = 5;zDj中国设计秀
        static final int S22 = 9;zDj中国设计秀
        static final int S23 = 14;zDj中国设计秀
        static final int S24 = 20;zDj中国设计秀

        static final int S31 = 4;zDj中国设计秀
        static final int S32 = 11;zDj中国设计秀
        static final int S33 = 16;zDj中国设计秀
        static final int S34 = 23;zDj中国设计秀

        static final int S41 = 6;zDj中国设计秀
        static final int S42 = 10;zDj中国设计秀
        static final int S43 = 15;zDj中国设计秀
        static final int S44 = 21;zDj中国设计秀

        static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0,zDj中国设计秀
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,zDj中国设计秀
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,zDj中国设计秀
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };zDj中国设计秀
        /* 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中zDj中国设计秀
           被定义到MD5_CTX结构中zDj中国设计秀
        zDj中国设计秀
         */zDj中国设计秀
        private long[] state = new long[4];  // state (ABCD)zDj中国设计秀
        private long[] count = new long[2];  // number of bits, modulo 2^64 (lsb first)zDj中国设计秀
        private byte[] buffer = new byte[64]; // input bufferzDj中国设计秀
        zDj中国设计秀
/* digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的zDj中国设计秀
  16进制ASCII表示.zDj中国设计秀
*/zDj中国设计秀
        public String digestHexStr;zDj中国设计秀
        zDj中国设计秀
        /* digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值.zDj中国设计秀
*/zDj中国设计秀
        private byte[] digest = new byte[16];zDj中国设计秀
        zDj中国设计秀
/*zDj中国设计秀
 getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串zDj中国设计秀
 返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.zDj中国设计秀
*/zDj中国设计秀
        public String getMD5ofStr(String inbuf) {zDj中国设计秀
                md5Init();zDj中国设计秀
                md5Update(inbuf.getBytes(), inbuf.length());zDj中国设计秀
                md5Final();zDj中国设计秀
                digestHexStr = "";zDj中国设计秀
                for (int i = 0; i < 16; i++) {zDj中国设计秀
                        digestHexStr += byteHEX(digest[i]);zDj中国设计秀
                }zDj中国设计秀
                return digestHexStr;zDj中国设计秀

        }zDj中国设计秀
        // 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数zDj中国设计秀
        public MD5() {zDj中国设计秀
                md5Init();zDj中国设计秀

                return;zDj中国设计秀
        }zDj中国设计秀
       zDj中国设计秀

zDj中国设计秀
        /* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */zDj中国设计秀
        private void md5Init() {zDj中国设计秀
                count[0] = 0L;zDj中国设计秀
                count[1] = 0L;zDj中国设计秀
                ///* Load magic initialization constants.zDj中国设计秀

                state[0] = 0x67452301L;zDj中国设计秀
                state[1] = 0xefcdab89L;zDj中国设计秀
                state[2] = 0x98badcfeL;zDj中国设计秀
                state[3] = 0x10325476L;zDj中国设计秀

                return;zDj中国设计秀
        }zDj中国设计秀
        /* F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是zDj中国设计秀
        简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们zDj中国设计秀
       实现成了private方法,名字保持了原来C中的。 */zDj中国设计秀

        private long F(long x, long y, long z) {zDj中国设计秀
                return (x & y) | ((~x) & z);zDj中国设计秀

        }zDj中国设计秀
        private long G(long x, long y, long z) {zDj中国设计秀
                return (x & z) | (y & (~z));zDj中国设计秀

        }zDj中国设计秀
        private long H(long x, long y, long z) {zDj中国设计秀
                return x ^ y ^ z;zDj中国设计秀
        }zDj中国设计秀

        private long I(long x, long y, long z) {zDj中国设计秀
                return y ^ (x | (~z));zDj中国设计秀
        }zDj中国设计秀
        zDj中国设计秀
       /* zDj中国设计秀
          FF,GG,HH和II将调用F,G,H,I进行近一步变换zDj中国设计秀
          FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.zDj中国设计秀
          Rotation is separate from addition to prevent recomputation.zDj中国设计秀
       */ zDj中国设计秀

        private long FF(long a, long b, long c, long d, long x, long s,zDj中国设计秀
                long ac) {zDj中国设计秀
                a += F (b, c, d) + x + ac;zDj中国设计秀
                a = ((int) a << s) | ((int) a >>> (32 - s));zDj中国设计秀
                a += b;zDj中国设计秀
                return a;zDj中国设计秀
        }zDj中国设计秀

        private long GG(long a, long b, long c, long d, long x, long s,zDj中国设计秀
                long ac) {zDj中国设计秀
                a += G (b, c, d) + x + ac;zDj中国设计秀
                a = ((int) a << s) | ((int) a >>> (32 - s));zDj中国设计秀
                a += b;zDj中国设计秀
                return a;zDj中国设计秀
        }zDj中国设计秀
        private long HH(long a, long b, long c, long d, long x, long s,zDj中国设计秀
                long ac) {zDj中国设计秀
                a += H (b, c, d) + x + ac;zDj中国设计秀
                a = ((int) a << s) | ((int) a >>> (32 - s));zDj中国设计秀
                a += b;zDj中国设计秀
                return a;zDj中国设计秀
        }zDj中国设计秀
        private long II(long a, long b, long c, long d, long x, long s,zDj中国设计秀
                long ac) {zDj中国设计秀
                a += I (b, c, d) + x + ac;zDj中国设计秀
                a = ((int) a << s) | ((int) a >>> (32 - s));zDj中国设计秀
                a += b;zDj中国设计秀
                return a;zDj中国设计秀
        }zDj中国设计秀
        /*zDj中国设计秀
         md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个zDj中国设计秀
         函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的zDj中国设计秀
        */zDj中国设计秀
        private void md5Update(byte[] inbuf, int inputLen) {zDj中国设计秀

                int i, index, partLen;zDj中国设计秀
                byte[] block = new byte[64];zDj中国设计秀
                index = (int)(count[0] >>> 3) & 0x3F;zDj中国设计秀
                // /* Update number of bits */zDj中国设计秀
                if ((count[0] += (inputLen << 3)) < (inputLen << 3))zDj中国设计秀
                        count[1]++;zDj中国设计秀
                count[1] += (inputLen >>> 29);zDj中国设计秀

                partLen = 64 - index;zDj中国设计秀

                // Transform as many times as possible.zDj中国设计秀
                if (inputLen >= partLen) {zDj中国设计秀
                        md5Memcpy(buffer, inbuf, index, 0, partLen);zDj中国设计秀
                        md5Transform(buffer);zDj中国设计秀

                        for (i = partLen; i + 63 < inputLen; i += 64) {zDj中国设计秀

                                md5Memcpy(block, inbuf, 0, i, 64);zDj中国设计秀
                                md5Transform (block);zDj中国设计秀
                        }zDj中国设计秀
                        index = 0;zDj中国设计秀

                } elsezDj中国设计秀

                        i = 0;zDj中国设计秀

                ///* Buffer remaining input */zDj中国设计秀
                md5Memcpy(buffer, inbuf, index, i, inputLen - i);zDj中国设计秀

        }zDj中国设计秀
        zDj中国设计秀
        /*zDj中国设计秀
          md5Final整理和填写输出结果zDj中国设计秀
        */zDj中国设计秀
        private void md5Final () {zDj中国设计秀
                byte[] bits = new byte[8];zDj中国设计秀
                int index, padLen;zDj中国设计秀

                ///* Save number of bits */zDj中国设计秀
                Encode (bits, count, 8);zDj中国设计秀

                ///* Pad out to 56 mod 64.zDj中国设计秀
                index = (int)(count[0] >>> 3) & 0x3f;zDj中国设计秀
                padLen = (index < 56) ? (56 - index) : (120 - index);zDj中国设计秀
                md5Update (PADDING, padLen);zDj中国设计秀

                ///* Append length (before padding) */zDj中国设计秀
                md5Update(bits, 8);zDj中国设计秀

                ///* Store state in digest */zDj中国设计秀
                Encode (digest, state, 16);zDj中国设计秀

        }zDj中国设计秀
         zDj中国设计秀
        /* md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的zDj中国设计秀
      字节拷贝到output的outpos位置开始 zDj中国设计秀
        */zDj中国设计秀

        private void md5Memcpy (byte[] output, byte[] input,zDj中国设计秀
                int outpos, int inpos, int len)zDj中国设计秀
        {zDj中国设计秀
                int i;zDj中国设计秀

                for (i = 0; i < len; i++)zDj中国设计秀
                        output[outpos + i] = input[inpos + i];zDj中国设计秀
        }zDj中国设计秀
        zDj中国设计秀
        /*zDj中国设计秀
           md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节zDj中国设计秀
        */zDj中国设计秀
        private void md5Transform (byte block[]) {zDj中国设计秀
                long a = state[0], b = state[1], c = state[2], d = state[3];zDj中国设计秀
                long[] x = new long[16];zDj中国设计秀

                Decode (x, block, 64);zDj中国设计秀

                /* Round 1 */zDj中国设计秀
                a = FF (a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */zDj中国设计秀
                d = FF (d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */zDj中国设计秀
                c = FF (c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */zDj中国设计秀
                b = FF (b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */zDj中国设计秀
                a = FF (a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */zDj中国设计秀
                d = FF (d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */zDj中国设计秀
                c = FF (c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */zDj中国设计秀
                b = FF (b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */zDj中国设计秀
                a = FF (a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */zDj中国设计秀
                d = FF (d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */zDj中国设计秀
                c = FF (c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */zDj中国设计秀
                b = FF (b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */zDj中国设计秀
                a = FF (a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */zDj中国设计秀
                d = FF (d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */zDj中国设计秀
                c = FF (c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */zDj中国设计秀
                b = FF (b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */zDj中国设计秀

                /* Round 2 */zDj中国设计秀
                a = GG (a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */zDj中国设计秀
                d = GG (d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */zDj中国设计秀
                c = GG (c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */zDj中国设计秀
                b = GG (b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */zDj中国设计秀
                a = GG (a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */zDj中国设计秀
                d = GG (d, a, b, c, x[10], S22, 0x2441453L); /* 22 */zDj中国设计秀
                c = GG (c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */zDj中国设计秀
                b = GG (b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */zDj中国设计秀
                a = GG (a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */zDj中国设计秀
                d = GG (d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */zDj中国设计秀
                c = GG (c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */zDj中国设计秀
                b = GG (b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */zDj中国设计秀
                a = GG (a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */zDj中国设计秀
                d = GG (d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */zDj中国设计秀
                c = GG (c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */zDj中国设计秀
                b = GG (b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */zDj中国设计秀

                /* Round 3 */zDj中国设计秀
                a = HH (a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */zDj中国设计秀
                d = HH (d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */zDj中国设计秀
                c = HH (c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */zDj中国设计秀
                b = HH (b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */zDj中国设计秀
                a = HH (a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */zDj中国设计秀
                d = HH (d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */zDj中国设计秀
                c = HH (c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */zDj中国设计秀
                b = HH (b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */zDj中国设计秀
                a = HH (a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */zDj中国设计秀
                d = HH (d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */zDj中国设计秀
                c = HH (c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */zDj中国设计秀
                b = HH (b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */zDj中国设计秀
                a = HH (a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */zDj中国设计秀
                d = HH (d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */zDj中国设计秀
                c = HH (c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */zDj中国设计秀
                b = HH (b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */zDj中国设计秀

                /* Round 4 */zDj中国设计秀
                a = II (a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */zDj中国设计秀
                d = II (d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */zDj中国设计秀
                c = II (c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */zDj中国设计秀
                b = II (b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */zDj中国设计秀
                a = II (a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */zDj中国设计秀
                d = II (d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */zDj中国设计秀
                c = II (c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */zDj中国设计秀
                b = II (b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */zDj中国设计秀
                a = II (a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */zDj中国设计秀
                d = II (d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */zDj中国设计秀
                c = II (c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */zDj中国设计秀
                b = II (b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */zDj中国设计秀
                a = II (a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */zDj中国设计秀
                d = II (d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */zDj中国设计秀
                c = II (c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */zDj中国设计秀
                b = II (b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */zDj中国设计秀

                state[0] += a;zDj中国设计秀
                state[1] += b;zDj中国设计秀
                state[2] += c;zDj中国设计秀
                state[3] += d;zDj中国设计秀

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

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

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

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

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

        public static void main(String args[]) {zDj中国设计秀

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

}zDj中国设计秀

 zDj中国设计秀

jsp中的使用方法zDj中国设计秀

-------------------------------------------------------------------------------zDj中国设计秀
<%@ page language='java' %>zDj中国设计秀
<jsp:useBean id='oMD5' scope='request' class='beartool.MD5'/>zDj中国设计秀

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

  Statement stmt = con.createStatement();zDj中国设计秀

  ResultSet rs = stmt.executeQuery("select * from users where userID ='"+userid+"' and pwdmd5= '" + pwdmd5+"'" );zDj中国设计秀

  if (rs.next()) zDj中国设计秀
    {zDj中国设计秀
      rp.print("Login OK");zDj中国设计秀
            zDj中国设计秀
    }zDj中国设计秀
  elsezDj中国设计秀
    {zDj中国设计秀
      rp.print("Login Fail");zDj中国设计秀
    }zDj中国设计秀

  stmt.close();zDj中国设计秀
  con.close();zDj中国设计秀
 zDj中国设计秀
%>zDj中国设计秀

</body>zDj中国设计秀

</html> zDj中国设计秀
 zDj中国设计秀

本文引用地址:/bc/article_46632.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明