【Jsp精品源码栏目提醒】:网学会员在Jsp精品源码频道为大家收集整理了“在JSP中如何实现MD5加密 - 其它资料“提供大家参考,希望对大家有所帮助!
源码 / 类名:MD5Digest 说明:用来进行密码加密的 md5 公用参数 编写日期:2001/03/05 修改者: 修改信息: authoredgarlo edgarlochina.com version1.0 / import java.security.MessageDigest import java.security.NoSuchAlgorithmException public class MD5Digest private MessageDigest __md5 null private StringBuffer __digestBuffer null public MD5Digest throws NoSuchAlgorithmException __md5 MessageDigest.getInstanceMD5 __digestBuffer new StringBuffer public String md5cryptString s __digestBuffer.setLength0 byte abyte0 __md5.digests.getBytes forint i 0 i abyte0.length i __digestBuffer.appendtoHexabyte0i return __digestBuffer.toString public String toHexbyte one String HEX0123456789ABCDEF char resultnew char2 result0HEX.charAtone 0xf0 4 result1HEX.charAtone 0x0f String mmnew Stringresult return mm --------------------------------------------------------------------------------/MD5 算法的 Java Beanauthor:Topcat TuppinLast Modified:10Mar2001/package beartoolimport java.lang.reflect./md5 类实现了 RSA Data Security Inc.在提交给 IETF的 RFC1321 中的 MD5 message-digest 算法。
/public class MD5 / 下面这些 S11-S44 实际上是一个 44 的矩阵,在原始的 C 实现中是用define 实现的,这里把它们实现成为 static final 是表示了只读,切能在同一个进程空间内的多个Instance 间共享/static final int S11 7static final int S12 12static final int S13 17static final int S14 22static final int S21 5static final int S22 9static final int S23 14static final int S24 20static final int S31 4static final int S32 11static final int S33 16static final int S34 23static final int S41 6static final int S42 10static final int S43 15static final int S44 21static final byte PADDING -128 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 / 下面的三个成员是 MD5 计算过程中用到的 3 个核心数据,在原始的 C 实现中被定义到 MD5_CTX 结构中/private long state new long4// state ABCDprivate long count new long2// number of bits modulo 264 lsb firstprivate byte buffer new byte64 // input buffer/ digestHexStr 是 MD5 的唯一一个公共成员,是最新一次计算结果的 16 进制 ASCII 表示./public String digestHexStr/ digest是最新一次计算结果的 2 进制内部表示,表示 128bit 的 MD5 值./private byte digest new byte16/getMD5ofStr 是类 MD5 最主要的公共方法,入口参数是你想要进行 MD5 变换的字符串返回的是变换完的结果,这个结果是从公共成员 digestHexStr 取得的./public String getMD5ofStrString inbuf md5Initmd5Updateinbuf.getBytes inbuf.lengthmd5FinaldigestHexStr for int i 0 i 16 i digestHexStr byteHEXdigestireturn digestHexStr// 这是 MD5 这个类的标准构造函数,JavaBean 要求有一个 public 的并且没有参数的构造函数public MD5 md5Initreturn/ md5Init 是一个初始化函数,初始化核心变量,装入标准的幻数 /private void md5Init count0 0Lcount1 0L/// Load magic initialization constants.state0 0x67452301Lstate1 0xefcdab89Lstate2 0x98badcfeLstate3 0x10325476Lreturn/ F G H I 是 4 个基本的 MD5 函数,在原始的 MD5 的 C 实现中,由于它们是简单的位运算,可能出于效率的考虑把它们实现成了宏,在 java 中,我们把它们 实现成了 private 方法,名字保持了原来 C 中的。
/private long Flong x long y long z return x y x zprivate long Glong x long y long z return x z y zprivate long Hlong x long y long z return x y zprivate long Ilong x long y long z return y x z/FFGGHH 和 II 将调用 FGHI 进行近一步变换FF GG HH and II transformations for rounds 1 2 3 and 4.Rotation is separate from addition to prevent recomputation./private long FFlong a long b long c long d long x long slong ac a F b c d x aca int a 32 - sa breturn aprivate long GGlong a long b long c long d long x long slong ac a G b c d x aca int a 32 - sa breturn aprivate long HHlong a long b long c long d long x long slong ac a H b c d x aca int a 32 - sa breturn aprivate long IIlong a long b long c long d long x long slong ac a I b c d x aca int a 32 - sa breturn a/md5Update 是 MD5 的主计算过程,inbuf 是要变换的字节串,inputlen 是长度,这个函数由 getMD5ofStr 调用,调用之前需要调用 md5init,因此把它设计成 private 的/private void md5Updatebyte inbuf int inputLen int i index partLenbyte block new byte64index intcount0 3 0x3F// / Update number of bits /if count0 inputLen 29partLen 64 - index// Transform as many times as possible.if inputLen partLen md5Memcpybuffer inbuf index 0 partLenmd5Transformbufferfor i partLen i 63 inputLen i 64 md5Memcpyblock inbuf 0 i 64md5Transform blockindex 0 elsei 0/// Buffer remaining input /md5Memcpybuffer inbuf index i inputLen - i/md5Final 整理和填写输出结果/private void md5Final byte bits new byte8int index padLen/// Save number of bits /Encode bits count 8/// Pad out to 56 mod 64.index intcount0 3 0x3fpadLen index 56 56 - index : 120 - indexmd5Update PADDING padLen/// Append length before padding /md5Updatebits 8/// Store state in digest /Encode digest state 16/ md5Memcpy 是一个内部使用的 byte 数组的块拷贝函数,从 input 的 inpos 开始把 len 长度的 字节拷贝到 output 的 outpos 位置开始/private void md5Memcpy byte output byte inputint outpos int inpos int lenint ifor i 0 i len ioutputoutpos i inputinpos i/md5Transform 是 MD5 核心变换程序,有 md5Update 调用,block 是分块的原始字节/private void md5Transform byte block long a state0 b state1 c state2 d state3long x new long16Decode x block 64/ Round 1 /a FF a b c d x0 S11 0xd76aa478L / 1 /d FF d a b c x1 S12 0xe8c7b756L / 2 /c FF c d a b x2 S13 0x242070dbL / 3 /b FF b c d a x3 S14 0xc1bdceeeL / 4 /a FF a b c d x4 S11 0xf57c0fafL / 5 /d FF d a b c x5 S12 0x4787c62aL / 6 /c FF c d a b x6 S13 0xa8304613L / 7 /b FF b c d a x7 S14 0xfd469501L / 8 /a FF a b c d x8 S11 0x698098d8L / 9 /d FF d a b c x9 S12 0x8b44f7afL / 10 /c FF c d a b x10 S13 0xffff5bb1L / 11 /b FF b c d a x11 S14 0x895cd7beL / 12 /a FF a b c d x12 S11 0x6b901122L / 13 /d FF d a b c x13 S12 0xfd987193L / 14 /c FF c d a b x14 S13 0xa679438eL / 15 /b FF b c d a x15 S14 0x49b40821L / 16 // Round 2 /a GG a b c d x1 S21 0xf61e2562L / 17 /d GG d a b c x6 S22 0xc040b340L / 18 /c GG c d a b x11 S23 0x265e5a51L / 19 /b GG b c d a x0 S24 0xe9b6c7aaL / 20 /a GG a b c d x5 S21 0xd62f105dL / 21 /d GG d a b c x10 S22 0x2441453L / 22 /c GG c d a b x15 S23 0xd8a1e681L / 23 /b GG b c d a x4 S24 0xe7d3fbc8L / 24 /a GG a b c d x9 S21 0x21e1cde6L / 25 /d GG d a b c x14 S22 0xc33707d6L / 26 /c GG c d a b x3 S23 0xf4d50d87L / 27 /b GG b c d a x8 S24 0x455a14edL / 28 /a GG a b c d x13 S21 0xa9e3e905L / 29 /d GG d a b c x2 S22 0xfcefa3f8L / 30 /c GG c d a b x7 S23 0x676f02d9L / 31 /b GG b c d a x12 S24 0x8d2a4c8aL / 32 // Round 3 /a HH a b c d x5 S31 0xfffa3942L / 33 /d HH d a b c x8 S32 0x8771f681L / 34 /c HH c d a b x11 S33 0x6d9d6122L / 35 /b HH b c d a x14 S34 0xfde5380cL / 36 /a HH a b c d x1 S31 0xa4beea44L / 37 /d HH d a b c x4 S32 0x4bdecfa9L / 38 /c HH c d a b x7 S33 0xf6bb4b60L / 39 /b HH b c d a x10 S34 0xbebfbc70L / 40 /a HH a b c d x13 S31 0x289b7ec6L / 41 /d HH d a b c x0 S32 0xeaa127faL / 42 /c HH c d a b x3 S33 0xd4ef3085L / 43 /b HH b c d a x6 S34 0x4881d05L / 44 /a HH a b c d x9 S31 0xd9d4d039L / 45 /d HH d a b c x12 S32 0xe6db99e5L / 46 /c HH c d a b x15 S33 0x1fa27cf8L / 47 /b HH b c d a x2 S34 0xc4ac5665L / 48 // Round 4 /a II a b c d x0 S41 0xf4292244L / 49 /d II d a b c x7 S42 0x432aff97L / 50 /c II c d a b x14 S43 0xab9423a7L / 51 /b II b c d a x5 S44 0xfc93a039L / 52 /a II a b c d x12 S41 0x655b59c3L / 53 /d II d a b c x3 S42 0x8f0ccc92L / 54 /c II c d a b x10 S43 0xffeff47dL / 55 /b II b c d a x1 S44 0x85845dd1L / 56 /a II a b c d x8 S41 0x6fa87e4fL / 57 /d II d a b c x15 S42 0xfe2ce6e0L / 58 /c II c d a b x6 S43 0xa3014314L / 59 /b II b c d a x13 S44 0x4e0811a1L / 60 /a II a b c d x4 S41 0xf7537e82L / 61 /d II d a b c x11 S42 0xbd3af235L / 62 /c II c d a b x2 S43 0x2ad7d2bbL / 63 /b II b c d a x9 S44 0xeb86d391L / 64 /state0 astate1 bstate2 cstate3 d/Encode 把 long 数组按顺序拆成 byte 数组,因为 java 的 long 类型是 64bit 的,只拆低 32bit,以适应原始 C 实现的用途/private void Encode byte output long input int len int i jfor i 0 j 0 j len i j 4 outputj byteinputi 0xffLoutputj 1 byteinputi 8 0xffLoutputj 2 byteinputi 16 0xffLoutputj 3 byteinputi 24 0xffL/Decode 把 byte 数组按顺序合成成 long 数组,因为 java 的 long 类型是 64bit 的,只合成低 32bit,高 32bit 清零,以适应原始 C 实现的用途/private void Decode long output byte input int len int i jfor i 0 j 0 j len i j 4outputi b2iuinputj b2iuinputj 1 4 0X0Fob1 Digitib 0X0FString s new Stringobreturn s public static void mainString args MD5 m new MD5 if Array.getLengthargs 0 //如果没有参数,执行标准的 Test Suite System.out.printlnMD5 Test suite: System.out.printlnMD5:m.getMD5ofStr System.out.printlnMD5a:m.getMD5ofStra System.out.printlnMD5abc:m.getMD5ofStrabc System.out.printlnMD5message digest:m.getMD5ofStrmessage digest System.out.printlnMD5abcdefghijklmnopqrstuvwxyz: m.getMD5ofStrabcdefghijklmnopqrstuvwxyzSystem.out.printlnMD5ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789: m.getMD5ofStrABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 else System.out.printlnMD5 args0 m.getMD5ofStrargs0
JSP 中的使用方法 -------------------------------------------------------------------------------