您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 数据通信与网络 > Java对MD5算法详细的设计代码
packagecom.std.safety;/********************************************************************************keyBean类实现了RSADataSecurity,Inc.在提交给IETF的RFC1321中的keyBeanmessage-digest*算法。******************************************************************************/publicclasskeyBean{/**下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define实现的,这里把它们实现成为static*final是表示了只读,切能在同一个进程空间内的多个Instance间共享*/staticfinalintS11=7;staticfinalintS12=12;staticfinalintS13=17;staticfinalintS14=22;staticfinalintS21=5;staticfinalintS22=9;staticfinalintS23=14;staticfinalintS24=20;staticfinalintS31=4;staticfinalintS32=11;staticfinalintS33=16;staticfinalintS34=23;staticfinalintS41=6;staticfinalintS42=10;staticfinalintS43=15;staticfinalintS44=21;staticfinalbyte[]PADDING={-128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};/**下面的三个成员是keyBean计算过程中用到的3个核心数据,在原始的C实现中被定义到keyBean_CTX结构中*/privatelong[]state=newlong[4];//state(ABCD)privatelong[]count=newlong[2];//numberofbits,modulo2^64(lsb//first)privatebyte[]buffer=newbyte[64];//inputbuffer/**digestHexStr是keyBean的唯一一个公共成员,是最新一次计算结果的16进制ASCII表示.*/publicStringdigestHexStr;/**digest,是最新一次计算结果的2进制内部表示,表示128bit的keyBean值.*/privatebyte[]digest=newbyte[16];/**getkeyBeanofStr是类keyBean最主要的公共方法,入口参数是你想要进行keyBean变换的字符串*返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.*/publicStringgetkeyBeanofStr(Stringinbuf){keyBeanInit();keyBeanUpdate(inbuf.getBytes(),inbuf.length());keyBeanFinal();digestHexStr=;for(inti=0;i16;i++){digestHexStr+=byteHEX(digest[i]);}returndigestHexStr;}//这是keyBean这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数publickeyBean(){keyBeanInit();return;}/*keyBeanInit是一个初始化函数,初始化核心变量,装入标准的幻数*/privatevoidkeyBeanInit(){count[0]=0L;count[1]=0L;///*Loadmagicinitializationconstants.state[0]=0x67452301L;state[1]=0xefcdab89L;state[2]=0x98badcfeL;state[3]=0x10325476L;return;}/**F,G,H,I是4个基本的keyBean函数,在原始的keyBean的C实现中,由于它们是*简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们实现成了private方法,名字保持了原来C中的。*/privatelongF(longx,longy,longz){return(x&y)|((~x)&z);}privatelongG(longx,longy,longz){return(x&z)|(y&(~z));}privatelongH(longx,longy,longz){returnx^y^z;}privatelongI(longx,longy,longz){returny^(x|(~z));}/**FF,GG,HH和II将调用F,G,H,I进行近一步变换FF,GG,HH,andIItransformationsfor*rounds1,2,3,and4.Rotationisseparatefromadditiontoprevent*recomputation.*/privatelongFF(longa,longb,longc,longd,longx,longs,longac){a+=F(b,c,d)+x+ac;a=((int)as)|((int)a(32-s));a+=b;returna;}privatelongGG(longa,longb,longc,longd,longx,longs,longac){a+=G(b,c,d)+x+ac;a=((int)as)|((int)a(32-s));a+=b;returna;}privatelongHH(longa,longb,longc,longd,longx,longs,longac){a+=H(b,c,d)+x+ac;a=((int)as)|((int)a(32-s));a+=b;returna;}privatelongII(longa,longb,longc,longd,longx,longs,longac){a+=I(b,c,d)+x+ac;a=((int)as)|((int)a(32-s));a+=b;returna;}/**keyBeanUpdate是keyBean的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个*函数由getkeyBeanofStr调用,调用之前需要调用keyBeaninit,因此把它设计成private的*/privatevoidkeyBeanUpdate(byte[]inbuf,intinputLen){inti,index,partLen;byte[]block=newbyte[64];index=(int)(count[0]3)&0x3F;///*Updatenumberofbits*/if((count[0]+=(inputLen3))(inputLen3))count[1]++;count[1]+=(inputLen29);partLen=64-index;//Transformasmanytimesaspossible.if(inputLen=partLen){keyBeanMemcpy(buffer,inbuf,index,0,partLen);keyBeanTransform(buffer);for(i=partLen;i+63inputLen;i+=64){keyBeanMemcpy(block,inbuf,0,i,64);keyBeanTransform(block);}index=0;}elsei=0;///*Bufferremaininginput*/keyBeanMemcpy(buffer,inbuf,index,i,inputLen-i);}/**keyBeanFinal整理和填写输出结果*/privatevoidkeyBeanFinal(){byte[]bits=newbyte[8];intindex,padLen;///*Savenumberofbits*/Encode(bits,count,8);///*Padoutto56mod64.index=(int)(count[0]3)&0x3f;padLen=(index56)?(56-index):(120-index);keyBeanUpdate(PADDING,padLen);///*Appendlength(beforepadding)*/keyBeanUpdate(bits,8);///*Storestateindigest*/Encode(digest,state,16);}/**keyBeanMemcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的*字节拷贝到output的outpos位置开始*/privatevoidkeyBeanMemcpy(byte[]output,byte[]input,intoutpos,intinpos,intlen){inti;for(i=0;ilen;i++)output[outpos+i]=input[inpos+i];}/**keyBeanTransform是keyBean核心变换程序,有keyBeanUpdate调用,block是分块的原始字节*/privatevoidkeyBeanTransform(byteblock[]){longa=state[0],b=state[1],c=state[2],d=state[3];long[]x=newlong[16];Decode(x,block,64);/*Round1*/a=FF(a,b,c,d,x[0],S11,0xd76aa478L);/*1*/d=FF(d,a,b,c,x[1],S12,0xe8c7b756L);/*2*/c=FF(c,d,a,b,x[2],S13,0x242070dbL);/*3*/b=FF(b,c,d,a,x[3],S14,0xc1bdceeeL);/*4*/a=FF(a,b,c,d,x[4],S11,0xf57c0fafL);/*5*/d=FF(d,a,b,c,x[5],S12,0x4787c62aL);/*6*/c=FF(c,d,a,b,x[6],S13,0xa8304613L);/*7*/b=FF(b,c,d,a,x[7],S14,0xfd469501L);/*8*/a=FF(a,b,c,d,x[8],S11,0x698098d8L);/*9*/d=FF(d,a,b,c,x[9],S12,0x8b44f7afL);/*10*/c=FF(c,d,a,b,x[10],S13,0xffff5bb1L);/*11*/b=FF(b,c,d,a,x[11],S14,0x895cd7beL);/*12*/a=FF(a,b,c,d,x[12],S11,0x6b901122L);/*13*/d=FF(d,a,b,c,x[13],S12,0xfd987193L);/*14*/c=FF(c,d,a,b,x[14],S13,0xa679438eL);/*15*/b=FF(b,c,d,a,x[15],S14,0x49b40821L);/*16*//*Round2*/a=GG(a,b,c,d,x[1],S21,0xf61e2562L);/*17*/d=GG(d,a,b,c,x[6],S22,0xc040b340L);/*18*/c=GG(c,d,
本文标题:Java对MD5算法详细的设计代码
链接地址:https://www.777doc.com/doc-4239466 .html