设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

12下一页
返回列表 发新帖
查看: 8522|回复: 15
打印 上一主题 下一主题

[模块/函数] 【转载 / 源码】MD5加密的VB类模块源码

[复制链接]
跳转到指定楼层
1#
发表于 2005-9-8 20:01:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
标 题:改了个MD5的VB类模块源码,可支持中文,也没有网上流传的那个超过16位就错了的毛病! (11千字)

发信人:simonyan

时 间:2002-11-4 14:31:00

详细信息:

<BLOCKQUOTE>那个网上的MD5.BAS我研究了下,有严重问题.只好另找了个自已改了下.

在窗体中调用时如下:

Dim md5 As New class 'class 为你存的类模快名

Text1.Value=md5.MD5_string_calc(sourcestring)



类模块源码如下:









<DIV class=quote>Option Explicit



' Visual Basic MD5 Implementation

' Robert Hubley and David Midkiff (mdj2023@hotmail.com)

' modify by simonyan, Support chinese

' Standard MD5 implementation optimised for the Visual Basic environment.

' Conforms to all standards and can be used in digital signature or password

' protection related schemes.



Private Const OFFSET_4 = 4294967296#

Private Const MAXINT_4 = 2147483647

Private State(4) As Long

Private ByteCounter As Long

Private ByteBuffer(63) As Byte

Private Const S11 = 7

Private Const S12 = 12

Private Const S13 = 17

Private Const S14 = 22

Private Const S21 = 5

Private Const S22 = 9

Private Const S23 = 14

Private Const S24 = 20

Private Const S31 = 4

Private Const S32 = 11

Private Const S33 = 16

Private Const S34 = 23

Private Const S41 = 6

Private Const S42 = 10

Private Const S43 = 15

Private Const S44 = 21

Property Get RegisterA() As String

    RegisterA = State(1)

End Property

Property Get RegisterB() As String

    RegisterB = State(2)

End Property



Property Get RegisterC() As String

    RegisterC = State(3)

End Property



Property Get RegisterD() As String

    RegisterD = State(4)

End Property

Public Function Md5_String_Calc(SourceString As String) As String

    MD5Init

    MD5Update LenB(StrConv(SourceString, vbFromUnicode)), StringToArray(SourceString)

    MD5Final

    Md5_String_Calc = GetValues

End Function

Public Function Md5_File_Calc(InFile As String) As String

On Error GoTo errorhandler

GoSub begin



errorhandler

    DigestFileToHexStr = ""

    Exit Function

   

begin:

    Dim FileO As Integer

    FileO = FreeFile

    Call FileLen(InFile)

    Open InFile For Binary Access Read As #FileO

    MD5Init

    Do While Not EOF(FileO)

        Get #FileO, , ByteBuffer

        If Loc(FileO) < LOF(FileO) Then

            ByteCounter = ByteCounter + 64

            MD5Transform ByteBuffer

        End If

    Loop

    ByteCounter = ByteCounter + (LOF(FileO) Mod 64)

    Close #FileO

    MD5Final

    Md5_File_Calc = GetValues

End Function

Private Function StringToArray(InString As String) As Byte()

    Dim I As Integer, bytBuffer() As Byte

    ReDim bytBuffer(LenB(StrConv(InString, vbFromUnicode)))

    bytBuffer = StrConv(InString, vbFromUnicode)

    StringToArray = bytBuffer

End Function

Public Function GetValues() As String

    GetValues = LongToString(State(1)) & LongToString(State(2)) & LongToString(State(3)) & LongToString(State(4))

End Function

Private Function LongToString(Num As Long) As String

        Dim A As Byte, B As Byte, C As Byte, D As Byte

        A = Num And &HFF&

        If A < 16 Then LongToString = "0" & Hex(A) Else LongToString = Hex(A)

        B = (Num And &HFF00&) \ 256

        If B < 16 Then LongToString = LongToString & "0" & Hex(B) Else LongToString = LongToString & Hex(B)

        C = (Num And &HFF0000) \ 65536

        If C < 16 Then LongToString = LongToString & "0" & Hex(C) Else LongToString = LongToString & Hex(C)

        If Num < 0 Then D = ((Num And &H7F000000) \ 16777216) Or &H80& Else D = (Num And &HFF000000) \ 16777216

        If D < 16 Then LongToString = LongToString & "0" & Hex(D) Else LongToString = LongToString & Hex(D)

End Function



Public Sub MD5Init()

    ByteCounter = 0

    State(1) = UnsignedToLong(1732584193#)

    State(2) = UnsignedToLong(4023233417#)

    State(3) = UnsignedToLong
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅

点击这里给我发消息

2#
发表于 2005-9-8 22:10:00 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

点击这里给我发消息

3#
发表于 2005-9-10 21:41:00 | 只看该作者


这个为什么不行?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
4#
 楼主| 发表于 2005-9-10 23:04:00 | 只看该作者
以下是引用真主在2005-9-10 13:41:00的发言:



这个为什么不行?



Private Sub Form_Current()

Dim c1 As New softdog 'class 为你存的类模快名

a.value = c1.Md5_String_Calc("c")

点击这里给我发消息

5#
发表于 2005-9-11 03:21:00 | 只看该作者
如果A中为已通过MD5加密过的值,C中为录入原始值,要比对C通过MD5加密的值是否与A一样,请问代码如何写?
6#
 楼主| 发表于 2005-9-11 19:55:00 | 只看该作者
以下是引用真主在2005-9-10 19:21:00的发言:



如果A中为已通过MD5加密过的值,C中为录入原始值,要比对C通过MD5加密的值是否与A一样,请问代码如何写?

这就是用MD5来存储密码的好处了,对于一个密码,先转换成MD5字符串,然后和数据库中存储的MD5字符串进行比较,相等则密码相同。
7#
发表于 2005-11-27 23:48:00 | 只看该作者
非常感谢!
8#
发表于 2006-2-3 04:15:00 | 只看该作者
不太懂
9#
发表于 2006-3-8 06:01:00 | 只看该作者
编译不了啊出错

但是可以运行





[此贴子已经被作者于2006-3-7 22:13:37编辑过]

10#
发表于 2006-4-26 00:28:00 | 只看该作者
编译不了啊出错
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-11-25 05:19 , Processed in 0.089577 second(s), 35 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表