纯代码实现md5算法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
纯代码实现md5算法
纯代码实现md5算法
网上已经有C,C++,VB6,java的MD5算法源代码了,甚至已经有了C#的MD5算法代码,唯独的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.CreateFro mName("MD5"), _
System.Security.Cryptography.HashAlgorithm).ComputeHash(b ytPwd)
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.CreateFro mName("MD5"), _
System.Security.Cryptography.HashAlgorithm).ComputeHash( Key)
End Function
当初写这代码是为了将用了.NET的MD5验证的程序能够用于没有MD5算法的Windows 2000 SP4以下版本,另外也可以更好的了解MD5算法的原理和步骤。
代码如下:
Public Class MD5Class 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
(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