现在的位置: 首页 > 综合 > 正文

纯代码实现md5算法

2013年04月28日 ⁄ 综合 ⁄ 共 5390字 ⁄ 字号 评论关闭

 网上已经有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算法的原理和步骤。

代码如下:

 

Public Class 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() {}

字节函数

    
Private 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() 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(ByRef Data As Byte(), ByVal StartIndex As LongAs 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(ByVal lngVal1 As IntegerByVal lngVal2 As IntegerAs 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


抱歉!评论已关闭.