VB.NET实现的MD5加密算法示例【32位】
本文实例讲述了VB.NET实现的MD5加密算法。分享给大家供大家参考,具体如下:
FunctionMD5F(ByValtempstrAsString,ByValwAsString,ByValXAsString,ByValyAsString,ByValzAsString,ByValXinAsString,ByValqdataAsString,ByValrotsAsInteger) MD5F=BigMod32Add(RotLeft(BigMod32Add(BigMod32Add(w,tempstr),BigMod32Add(Xin,qdata)),rots),X) EndFunction SubMD5F1(ByRefwAsString,ByValXAsString,ByValyAsString,ByValzAsString,ByValXinAsString,ByValqdataAsString,ByValrotsAsInteger) DimtempstrAsString tempstr=BigOR(BigAND(X,y),BigAND(BigNOT(X),z)) w=MD5F(tempstr,w,X,y,z,Xin,qdata,rots) EndSub SubMD5F2(ByRefwAsString,ByValXAsString,ByValyAsString,ByValzAsString,ByValXinAsString,ByValqdataAsString,ByValrotsAsInteger) DimtempstrAsString tempstr=BigOR(BigAND(X,z),BigAND(y,BigNOT(z))) 'tempstr=BigXOR(y,BigAND(z,BigXOR(X,y))) w=MD5F(tempstr,w,X,y,z,Xin,qdata,rots) EndSub SubMD5F3(ByRefwAsString,ByValXAsString,ByValyAsString,ByValzAsString,ByValXinAsString,ByValqdataAsString,ByValrotsAsInteger) DimtempstrAsString tempstr=BigXOR(BigXOR(X,y),z) 'tempstr=BigXOR(X,BigXOR(y,z)) w=MD5F(tempstr,w,X,y,z,Xin,qdata,rots) EndSub SubMD5F4(ByRefwAsString,ByValXAsString,ByValyAsString,ByValzAsString,ByValXinAsString,ByValqdataAsString,ByValrotsAsInteger) DimtempstrAsString tempstr=BigXOR(y,BigOR(X,BigNOT(z))) w=MD5F(tempstr,w,X,y,z,Xin,qdata,rots) EndSub FunctionMD5_Calc(ByValhashthisAsString)AsString Dimbuf(4)AsString DimXin(16)AsString DimtempnumAsInteger,tempnum2AsInteger,loopitAsInteger,loopouterAsInteger,loopinnerAsInteger DimaAsString,bAsString,cAsString,dAsString,tempstr99AsString tempnum=8*Len(hashthis) hashthis=hashthis+Chr(128)'Addbinary10000000 tempnum2=56-Len(hashthis)Mod64 Iftempnum2<0Then tempnum2=64+tempnum2 EndIf 'hashthis=hashthis+String(tempnum2,Chr(0)) Forloopit=1Totempnum2 hashthis=hashthis+Chr(0) Nextloopit Forloopit=1To8 hashthis=hashthis+Chr(tempnumMod256) tempnum=tempnum-tempnumMod256 tempnum=tempnum/256 Nextloopit buf(0)="67452301" buf(1)="efcdab89" buf(2)="98badcfe" buf(3)="10325476" Forloopouter=0To(Len(hashthis)/64-1) a=buf(0) b=buf(1) c=buf(2) d=buf(3) 'Getthe512bits Forloopit=0To15 Xin(loopit)="" Forloopinner=1To4 'tempstr99=Hex$(Asc(Mid$(hashthis,64*loopouter+4*loopit+loopinner,1))) 'IfLen(tempstr99)<2Thentempstr99="0"+tempstr99 'Xin(loopit)=Xin(loopit)+tempstr99 Xin(loopit)=Hex$(Asc(Mid$(hashthis,64*loopouter+4*loopit+loopinner,1)))+Xin(loopit) IfLen(Xin(loopit))Mod2ThenXin(loopit)="0"+Xin(loopit) Nextloopinner Nextloopit '第一轮循环 MD5F1(a,b,c,d,Xin(0),"d76aa478",7) MD5F1(d,a,b,c,Xin(1),"e8c7b756",12) MD5F1(c,d,a,b,Xin(2),"242070db",17) MD5F1(b,c,d,a,Xin(3),"c1bdceee",22) MD5F1(a,b,c,d,Xin(4),"f57c0faf",7) MD5F1(d,a,b,c,Xin(5),"4787c62a",12) MD5F1(c,d,a,b,Xin(6),"a8304613",17) MD5F1(b,c,d,a,Xin(7),"fd469501",22) MD5F1(a,b,c,d,Xin(8),"698098d8",7) MD5F1(d,a,b,c,Xin(9),"8b44f7af",12) MD5F1(c,d,a,b,Xin(10),"ffff5bb1",17) MD5F1(b,c,d,a,Xin(11),"895cd7be",22) MD5F1(a,b,c,d,Xin(12),"6b901122",7) MD5F1(d,a,b,c,Xin(13),"fd987193",12) MD5F1(c,d,a,b,Xin(14),"a679438e",17) MD5F1(b,c,d,a,Xin(15),"49b40821",22) '第二轮循环 MD5F2(a,b,c,d,Xin(1),"f61e2562",5) MD5F2(d,a,b,c,Xin(6),"c040b340",9) MD5F2(c,d,a,b,Xin(11),"265e5a51",14) MD5F2(b,c,d,a,Xin(0),"e9b6c7aa",20) MD5F2(a,b,c,d,Xin(5),"d62f105d",5) MD5F2(d,a,b,c,Xin(10),"02441453",9) MD5F2(c,d,a,b,Xin(15),"d8a1e681",14) MD5F2(b,c,d,a,Xin(4),"e7d3fbc8",20) MD5F2(a,b,c,d,Xin(9),"21e1cde6",5) MD5F2(d,a,b,c,Xin(14),"c33707d6",9) MD5F2(c,d,a,b,Xin(3),"f4d50d87",14) MD5F2(b,c,d,a,Xin(8),"455a14ed",20) MD5F2(a,b,c,d,Xin(13),"a9e3e905",5) MD5F2(d,a,b,c,Xin(2),"fcefa3f8",9) MD5F2(c,d,a,b,Xin(7),"676f02d9",14) MD5F2(b,c,d,a,Xin(12),"8d2a4c8a",20) '第三轮循环 MD5F3(a,b,c,d,Xin(5),"fffa3942",4) MD5F3(d,a,b,c,Xin(8),"8771f681",11) MD5F3(c,d,a,b,Xin(11),"6d9d6122",16) MD5F3(b,c,d,a,Xin(14),"fde5380c",23) MD5F3(a,b,c,d,Xin(1),"a4beea44",4) MD5F3(d,a,b,c,Xin(4),"4bdecfa9",11) MD5F3(c,d,a,b,Xin(7),"f6bb4b60",16) MD5F3(b,c,d,a,Xin(10),"bebfbc70",23) MD5F3(a,b,c,d,Xin(13),"289b7ec6",4) MD5F3(d,a,b,c,Xin(0),"eaa127fa",11) MD5F3(c,d,a,b,Xin(3),"d4ef3085",16) MD5F3(b,c,d,a,Xin(6),"04881d05",23) MD5F3(a,b,c,d,Xin(9),"d9d4d039",4) MD5F3(d,a,b,c,Xin(12),"e6db99e5",11) MD5F3(c,d,a,b,Xin(15),"1fa27cf8",16) MD5F3(b,c,d,a,Xin(2),"c4ac5665",23) '第四轮循环 MD5F4(a,b,c,d,Xin(0),"f4292244",6) MD5F4(d,a,b,c,Xin(7),"432aff97",10) MD5F4(c,d,a,b,Xin(14),"ab9423a7",15) MD5F4(b,c,d,a,Xin(5),"fc93a039",21) MD5F4(a,b,c,d,Xin(12),"655b59c3",6) MD5F4(d,a,b,c,Xin(3),"8f0ccc92",10) MD5F4(c,d,a,b,Xin(10),"ffeff47d",15) MD5F4(b,c,d,a,Xin(1),"85845dd1",21) MD5F4(a,b,c,d,Xin(8),"6fa87e4f",6) MD5F4(d,a,b,c,Xin(15),"fe2ce6e0",10) MD5F4(c,d,a,b,Xin(6),"a3014314",15) MD5F4(b,c,d,a,Xin(13),"4e0811a1",21) MD5F4(a,b,c,d,Xin(4),"f7537e82",6) MD5F4(d,a,b,c,Xin(11),"bd3af235",10) MD5F4(c,d,a,b,Xin(2),"2ad7d2bb",15) MD5F4(b,c,d,a,Xin(9),"eb86d391",21) buf(0)=BigAdd(buf(0),a) buf(1)=BigAdd(buf(1),b) buf(2)=BigAdd(buf(2),c) buf(3)=BigAdd(buf(3),d) Nextloopouter hashthis="" Forloopit=0To3 Forloopinner=3To0Step-1 hashthis=hashthis+Mid(buf(loopit),1+2*loopinner,2) Nextloopinner Nextloopit MD5_Calc=hashthis EndFunction FunctionBigMod32Add(ByValvalue1AsString,ByValvalue2AsString)AsString BigMod32Add=Right$(BigAdd(value1,value2),8) EndFunction PublicFunctionBigAdd(ByValvalue1AsString,ByValvalue2AsString)AsString DimvalueansAsString DimloopitAsInteger,tempnumAsInteger tempnum=Len(value1)-Len(value2) Iftempnum<0Then value1=Space$(Math.Abs(tempnum))+value1 ElseIftempnum>0Then value2=Space$(Math.Abs(tempnum))+value2 EndIf tempnum=0 Forloopit=Len(value1)To1Step-1 tempnum=tempnum+Val("&H"+Mid$(value1,loopit,1))+Val("&H"+Mid$(value2,loopit,1)) valueans=Hex$(tempnumMod16)+valueans tempnum=Int(tempnum/16) Nextloopit Iftempnum<>0Then valueans=Hex$(tempnum)+valueans EndIf BigAdd=Right(valueans,8) EndFunction PublicFunctionRotLeft(ByValvalue1AsString,ByValrotsAsInteger)AsString DimtempstrAsString DimloopitAsInteger,loopinnerAsInteger DimtempnumAsInteger rots=rotsMod32 Ifrots=0Then RotLeft=value1 ExitFunction EndIf value1=Right$(value1,8) 'tempstr=String$(8-Len(value1),"0")+value1 Forloopit=1To8-Len(value1) tempstr=tempstr+"0" Nextloopit tempstr=tempstr+value1 value1="" '转换成二进制形式 Forloopit=1To8 tempnum=Val("&H"+Mid$(tempstr,loopit,1)) Forloopinner=3To0Step-1 IftempnumAnd2^loopinnerThen value1=value1+"1" Else value1=value1+"0" EndIf Nextloopinner Nextloopit tempstr=Mid$(value1,rots+1)+Left$(value1,rots) '转换为十六进制 value1="" Forloopit=0To7 tempnum=0 Forloopinner=0To3 IfVal(Mid$(tempstr,4*loopit+loopinner+1,1))Then tempnum=tempnum+2^(3-loopinner) EndIf Nextloopinner value1=value1+Hex$(tempnum) Nextloopit RotLeft=Right(value1,8) EndFunction FunctionBigAND(ByValvalue1AsString,ByValvalue2AsString)AsString DimvalueansAsString DimloopitAsInteger,tempnumAsInteger tempnum=Len(value1)-Len(value2) Iftempnum<0Then value2=Mid$(value2,Math.Abs(tempnum)+1) ElseIftempnum>0Then value1=Mid$(value1,tempnum+1) EndIf Forloopit=1ToLen(value1) valueans=valueans+Hex$(Val("&H"+Mid$(value1,loopit,1))AndVal("&H"+Mid$(value2,loopit,1))) Nextloopit BigAND=valueans EndFunction FunctionBigNOT(ByValvalue1AsString)AsString DimvalueansAsString DimloopitAsInteger DimtempstrAsString value1=Right$(value1,8) 'value1=String$(8-Len(value1),"0")+value1 Forloopit=1To8-Len(value1) tempstr=tempstr+"0" Nextloopit tempstr=tempstr+value1 value1=tempstr Forloopit=1To8 valueans=valueans+Hex$(15XorVal("&H"+Mid$(value1,loopit,1))) Nextloopit BigNOT=valueans EndFunction FunctionBigOR(ByValvalue1AsString,ByValvalue2AsString)AsString DimvalueansAsString DimloopitAsInteger,tempnumAsInteger tempnum=Len(value1)-Len(value2) Iftempnum<0Then valueans=Left$(value2,Math.Abs(tempnum)) value2=Mid$(value2,Math.Abs(tempnum)+1) ElseIftempnum>0Then valueans=Left$(value1,Math.Abs(tempnum)) value1=Mid$(value1,tempnum+1) EndIf Forloopit=1ToLen(value1) valueans=valueans+Hex$(Val("&H"+Mid$(value1,loopit,1))OrVal("&H"+Mid$(value2,loopit,1))) Nextloopit BigOR=valueans EndFunction FunctionBigXOR(ByValvalue1AsString,ByValvalue2AsString)AsString DimvalueansAsString DimloopitAsInteger,tempnumAsInteger tempnum=Len(value1)-Len(value2) Iftempnum<0Then valueans=Left$(value2,Math.Abs(tempnum)) value2=Mid$(value2,Math.Abs(tempnum)+1) ElseIftempnum>0Then valueans=Left$(value1,Math.Abs(tempnum)) value1=Mid$(value1,tempnum+1) EndIf Forloopit=1ToLen(value1) valueans=valueans+Hex$(Val("&H"+Mid$(value1,loopit,1))XorVal("&H"+Mid$(value2,loopit,1))) Nextloopit BigXOR=Right(valueans,8) EndFunction
PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:
MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password
迅雷、快车、旋风URL加密/解密工具:
http://tools.jb51.net/password/urlrethunder
在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
希望本文所述对大家VB.NET程序设计有所帮助。