纯代码实现md5算法.docx

上传人:b****5 文档编号:2877963 上传时间:2022-11-16 格式:DOCX 页数:17 大小:20.66KB
下载 相关 举报
纯代码实现md5算法.docx_第1页
第1页 / 共17页
纯代码实现md5算法.docx_第2页
第2页 / 共17页
纯代码实现md5算法.docx_第3页
第3页 / 共17页
纯代码实现md5算法.docx_第4页
第4页 / 共17页
纯代码实现md5算法.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

纯代码实现md5算法.docx

《纯代码实现md5算法.docx》由会员分享,可在线阅读,更多相关《纯代码实现md5算法.docx(17页珍藏版)》请在冰豆网上搜索。

纯代码实现md5算法.docx

纯代码实现md5算法

纯代码实现md5算法

纯代码实现md5算法

网上已经有C,C++,VB6,java的MD5算法源代码了,甚至已经有了C#的MD5算法代码,唯独VB.NET的MD5算法代码是在是少而又少,因此贴出下列代码供大家雅正。

有人说了,.NET自带MD5算法,何必多此一举呢?

如下所示:

‘MD5

PublicSharedFunctionMD5(ByValstrKeyAsString)AsString

DimsPwdAsString

DimbytPwdAs[Byte]()=ConStrArr(strKey)

DimhashPwdAsByte()=CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"),_

System.Security.Cryptography.HashAlgorithm).ComputeHash(bytPwd)

sPwd=BitConverter.ToString(hashPwd)

sPwd=LCase(sPwd.Replace("-",""))‘去掉中间的"-"符号并转换为小写字母

ReturnsPwd

EndFunction

PublicSharedFunctionMD5(ByValKeyAsByte())AsByte()

ReturnCType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"),_

System.Security.Cryptography.HashAlgorithm).ComputeHash(Key)

EndFunction

当初写这代码是为了将用了.NET的MD5验证的程序能够用于没有MD5算法的Windows2000SP4以下版本,另外也可以更好的了解MD5算法的原理和步骤。

代码如下:

PublicClassMD5ClassMD5

PrivateConstAAAsInteger=&H67452301

PrivateConstBBAsInteger=&HEFCDAB89

PrivateConstCCAsInteger=&H98BADCFE

PrivateConstDDAsInteger=&H10325476PrivateConstTsAsLong=&H100000000PrivateConstS11AsInteger=7

PrivateConstS12AsInteger=12

PrivateConstS13AsInteger=17

PrivateConstS14AsInteger=22

PrivateConstS21AsInteger=5

PrivateConstS22AsInteger=9

PrivateConstS23AsInteger=14

PrivateConstS24AsInteger=20

PrivateConstS31AsInteger=4

PrivateConstS32AsInteger=11

PrivateConstS33AsInteger=16

PrivateConstS34AsInteger=23

PrivateConstS41AsInteger=6

PrivateConstS42AsInteger=10

PrivateConstS43AsInteger=15

PrivateConstS44AsInteger=21PrivateSiAsInteger()=NewInteger(){}字节函数#Region"字节函数"

PrivateSharedFunctionInt64ToBytes()FunctionInt64ToBytes(ByValIntValueAsInt64)AsByte()

ReturnNewByte(){(IntValue>>0)And&HFF,_

(IntValue>>8)And&HFF,_

(IntValue>>16)And&HFF,_

(IntValue>>24)And&HFF,_

(IntValue>>32)And&HFF,_

(IntValue>>40)And&HFF,_

(IntValue>>48)And&HFF,_

(IntValue>>56)And&HFF_

}

EndFunctionPublicSharedFunctionInt32ToBytes()FunctionInt32ToBytes(ByValIntValueAsInt32)AsByte()

ReturnNewByte(){(IntValue>>0)And&HFF,_

(IntValue>>8)And&HFF,_

(IntValue>>16)And&HFF,_

(IntValue>>24)And&HFF_

}

EndFunctionPublicSharedFunctionBytesToInt32()FunctionBytesToInt32(ByValBuffAsByte(),OptionalByValStartIndexAsInteger=0)AsInt32

IfBuffIsNothingOrElseBuff.Length=0ThenReturn0

IfStartIndex>=Buff.LengthThenReturn0

DimcValueAsInt32=0‘正常转换

ForiAsInteger=Math.Min(StartIndex+3,Buff.Length-1)ToStartIndexStep-1

cValue=(cValue<<8)OrBuff(i)

Next

ReturncValue

EndFunctionPublicSharedFunctionBytesToInt64()FunctionBytesToInt64(ByValBuffAsByte(),OptionalByValStartIndexAsInteger=0)AsInt64

IfBuffIsNothingOrElseBuff.Length=0ThenReturn0

IfStartIndex>=Buff.LengthThenReturn0

DimcValueAsInt32=0‘正常转换

ForiAsInteger=Math.Min(StartIndex+7,Buff.Length-1)ToStartIndexStep-1

cValue=(cValue<<8)OrBuff(i)

NextReturncValue

EndFunction

#EndRegionPrivateFunctionInitByte()FunctionInitByte(ByRefDataAsByte())AsByte()

DimklAsArrayList=NewArrayList(Data)

DimdbLengthAsInt64=Data.GetLongLength(0)

IfdbLengthMod56<>0OrElsedbLength=0Then

DimiAsInteger=56-(kl.CountMod56)-1

kl.Add(CByte(&H80))‘补1

Ifi>0Then

kl.AddRange(NewByte(i-1){})‘补0

EndIf

EndIf

kl.AddRange(Int64ToBytes(dbLength<<3))‘添加长度

DimbuffoutAsByte()=kl.ToArray(GetType(Byte))

kl.Clear()

Returnbuffout

EndFunctionPrivateFunctionInitT()FunctionInitT()AsInt32()

DimTAsInt32()=NewInt32(63){}

ForiAsInteger=0To63

T(i)=BytesToInt32(Int64ToBytes(CLng(Ts*Math.Abs(Math.Sin(i+1)))And&HFFFFFFFF&))

Next

ReturnT

EndFunctionPrivateFunctionInitM()FunctionInitM(ByRefDataAsByte(),ByValStartIndexAsLong)AsInt32()

DimMAsInt32()=NewInt32(15){}

ForiAsInteger=0To15

M(i)=BytesToInt32(Data,i<<2)

Next

ReturnM

EndFunctionPrivateFunctionLongAdd()FunctionLongAdd(ByVallngVal1AsInteger,ByVallngVal2AsInteger)AsIntegerDimlngHighWordAsInteger

DimlngLowWordAsInteger

DimlngOverflowAsIntegerlngLowWord=(lngVal1And&HFFFF&)+(lngVal2And&HFFFF&)

lngOverflow=lngLowWord65536

lngHighWord=(((lngVal1And&HFFFF0000)65536)+((lngVal2And&HFFFF0000)65536)+lngOverflow)And&HFFFF&ReturnlngHighWord<<16Or(lngLowWordAnd&HFFFF)‘LongConversion((lngHighWord*65536.0#)+(lngLowWordAnd&HFFFF&a

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 预防医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1