【asp精品源码栏目提醒】:网学会员鉴于大家对asp精品源码十分关注,论文会员在此为大家搜集整理了“在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
上一篇:
基于PHP和MVC技术架构的精品课程网站管理系统
下一篇:
白色姜花,略微开了