网上已经有C,C++,VB6,java的MD5算法源代码了,甚至已经有了C#的MD5算法代码,唯独VB.NET的MD5算法代码是在是少而又少,因此贴出下列代码供大家雅正。
有人说了,.NET自带MD5算法,何必多此一举呢?如下所示:
'MD5
Public Shared Function MD5(ByVal strKey As String) As String
Dim sPwd As String
Dim bytPwd As [Byte]() = ConStrArr(strKey)
Dim hashPwd As Byte() = CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"), _
System.Security.Cryptography.HashAlgorithm).ComputeHash(bytPwd)
sPwd = BitConverter.ToString(hashPwd)
sPwd = LCase(sPwd.Replace("-", "")) '去掉中间的"-"符号并转换为小写字母
Return sPwd
End Function
Public Shared Function MD5(ByVal Key As Byte()) As Byte()
Return CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"), _
System.Security.Cryptography.HashAlgorithm).ComputeHash(Key)
End Function
当初写这代码是为了将用了.NET的MD5验证的程序能够用于没有MD5算法的Windows 2000 SP4以下版本,另外也可以更好的了解MD5算法的原理和步骤。
代码如下:
Private Const AA As Integer = &H67452301
Private Const BB As Integer = &HEFCDAB89
Private Const CC As Integer = &H98BADCFE
Private Const DD As Integer = &H10325476
Private Const Ts As Long = &H100000000
Private Const S11 As Integer = 7
Private Const S12 As Integer = 12
Private Const S13 As Integer = 17
Private Const S14 As Integer = 22
Private Const S21 As Integer = 5
Private Const S22 As Integer = 9
Private Const S23 As Integer = 14
Private Const S24 As Integer = 20
Private Const S31 As Integer = 4
Private Const S32 As Integer = 11
Private Const S33 As Integer = 16
Private Const S34 As Integer = 23
Private Const S41 As Integer = 6
Private Const S42 As Integer = 10
Private Const S43 As Integer = 15
Private Const S44 As Integer = 21
Private Si As Integer() = New Integer() {}
字节函数#Region " 字节函数 "
Private Shared Function Int64ToBytes()Function Int64ToBytes(ByVal IntValue As Int64) As Byte()
Return New Byte() {(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 _
}
End Function
Public Shared Function Int32ToBytes()Function Int32ToBytes(ByVal IntValue As Int32) As Byte()
Return New Byte() {(IntValue >> 0) And &HFF, _
(IntValue >> 8) And &HFF, _
(IntValue >> 16) And &HFF, _
(IntValue >> 24) And &HFF _
}
End Function
Public Shared Function BytesToInt32()Function BytesToInt32(ByVal Buff As Byte(), Optional ByVal StartIndex As Integer = 0) As Int32
If Buff Is Nothing OrElse Buff.Length = 0 Then Return 0
If StartIndex >= Buff.Length Then Return 0
Dim cValue As Int32 = 0
'正常转换
For i As Integer = Math.Min(StartIndex + 3, Buff.Length - 1) To StartIndex Step -1
cValue = (cValue << 8) Or Buff(i)
Next
Return cValue
End Function
Public Shared Function BytesToInt64()Function BytesToInt64(ByVal Buff As Byte(), Optional ByVal StartIndex As Integer = 0) As Int64
If Buff Is Nothing OrElse Buff.Length = 0 Then Return 0
If StartIndex >= Buff.Length Then Return 0
Dim cValue As Int32 = 0
'正常转换
For i As Integer = Math.Min(StartIndex + 7, Buff.Length - 1) To StartIndex Step -1
cValue = (cValue << 8) Or Buff(i)
Next
Return cValue
End Function
#End Region
Private Function InitByte()Function InitByte(ByRef Data As Byte()) As Byte()
Dim kl As ArrayList = New ArrayList(Data)
Dim dbLength As Int64 = Data.GetLongLength(0)
If dbLength Mod 56 <> 0 OrElse dbLength = 0 Then
Dim i As Integer = 56 - (kl.Count Mod 56) - 1
kl.Add(CByte(&H80)) '补1
If i > 0 Then
kl.AddRange(New Byte(i - 1) {}) '补0
End If
End If
kl.AddRange(Int64ToBytes(dbLength << 3)) '添加长度
Dim buffout As Byte() = kl.ToArray(GetType(Byte))
kl.Clear()
Return buffout
End Function
Private Function InitT()Function InitT() As Int32()
Dim T As Int32() = New Int32(63) {}
For i As Integer = 0 To 63
T(i) = BytesToInt32(Int64ToBytes(CLng(Ts * Math.Abs(Math.Sin(i + 1))) And &HFFFFFFFF&))
Next
Return T
End Function
Private Function InitM()Function InitM(ByRef Data As Byte(), ByVal StartIndex As Long) As Int32()
Dim M As Int32() = New Int32(15) {}
For i As Integer = 0 To 15
M(i) = BytesToInt32(Data, i << 2)
Next
Return M
End Function
Private Function LongAdd()Function LongAdd(ByVal lngVal1 As Integer, ByVal lngVal2 As Integer) As Integer
Dim lngHighWord As Integer
Dim lngLowWord As Integer
Dim lngOverflow As Integer
lngLowWord = (lngVal1 And &HFFFF&) + (lngVal2 And &HFFFF&)
lngOverflow = lngLowWord 65536
lngHighWord = (((lngVal1 And &HFFFF0000) 65536) + ((lngVal2 And &HFFFF0000) 65536) + lngOverflow) And &HFFFF&
Return lngHighWord << 16 Or (lngLowWord And &HFFFF) 'LongConversion((lngHighWord * 65536.0#) + (lngLowWord And &HFFFF&))
End Function