
求vb md5加解密
Ifpassword.Text="haha"ThenShell("cmd.exe"),vbNormalFocusElsemsgbox"密码不对!"本来是想设置密码,不输密...
If password.Text = "haha" Then
Shell ("cmd.exe"), vbNormalFocus
Elsemsgbox "密码不对!" 本来是想设置密码,不输密码就不能运行指定程序。但是这样太简单了,别人直接用记事本打开这个exe就可以看到密码了。所以希望大家帮我写个md5解密的过程,完善这个小程序。 假如 密码haha MD5加密后为 dadsasdads ,加密后的结果大家可以到 www.cmd5.com 查看 那么原程序就变为 dim md5mima md5mima = md5("dadsasdads") If password.Text = md5mima Then 只要源代码中不出现 haha 这个原密码就可以。要写的就是 md5() 这个 function 过程 就这样简单。。。 我不知道上面的语法有没有写对,不会vb,但我相信大家明白意思。会的帮我写个程序,不要编译好的exe ,要全部的源代码 。 展开
Shell ("cmd.exe"), vbNormalFocus
Elsemsgbox "密码不对!" 本来是想设置密码,不输密码就不能运行指定程序。但是这样太简单了,别人直接用记事本打开这个exe就可以看到密码了。所以希望大家帮我写个md5解密的过程,完善这个小程序。 假如 密码haha MD5加密后为 dadsasdads ,加密后的结果大家可以到 www.cmd5.com 查看 那么原程序就变为 dim md5mima md5mima = md5("dadsasdads") If password.Text = md5mima Then 只要源代码中不出现 haha 这个原密码就可以。要写的就是 md5() 这个 function 过程 就这样简单。。。 我不知道上面的语法有没有写对,不会vb,但我相信大家明白意思。会的帮我写个程序,不要编译好的exe ,要全部的源代码 。 展开
2个回答
2013-05-24
展开全部
代码很多。仔细看先建一个类Class Module 取名为ClsAPIMD5复制下面的代码到类里:'API 做的MD5类Option Explicit
Private Declare Function CryptAcquireContext Lib "advapi32.dll" _
Alias "CryptAcquireContextA" ( _
ByRef phProv As Long, _
ByVal pszContainer As String, _
ByVal pszProvider As String, _
ByVal dwProvType As Long, _
ByVal dwFlags As Long) As LongPrivate Declare Function CryptReleaseContext Lib "advapi32.dll" ( _
ByVal hProv As Long, _
ByVal dwFlags As Long) As LongPrivate Declare Function CryptCreateHash Lib "advapi32.dll" ( _
ByVal hProv As Long, _
ByVal Algid As Long, _
ByVal hKey As Long, _
ByVal dwFlags As Long, _
ByRef phHash As Long) As LongPrivate Declare Function CryptDestroyHash Lib "advapi32.dll" ( _
ByVal hHash As Long) As LongPrivate Declare Function CryptHashData Lib "advapi32.dll" ( _
ByVal hHash As Long, _
pbData As Any, _
ByVal dwDataLen As Long, _
ByVal dwFlags As Long) As LongPrivate Declare Function CryptGetHashParam Lib "advapi32.dll" ( _
ByVal hHash As Long, _
ByVal dwParam As Long, _
pbData As Any, _
pdwDataLen As Long, _
ByVal dwFlags As Long) As LongPrivate Const PROV_RSA_FULL = 1Private Const ALG_CLASS_HASH = 32768Private Const ALG_TYPE_ANY = 0Private Const ALG_SID_MD2 = 1
Private Const ALG_SID_MD4 = 2
Private Const ALG_SID_MD5 = 3
Private Const ALG_SID_SHA1 = 4Enum HashAlgorithm
md2 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2
MD4 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4
md5 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5
SHA1 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA1
End EnumPrivate Const HP_HASHVAL = 2
Private Const HP_HASHSIZE = 4Function HashString( _
ByVal Str As String, _
Optional ByVal Algorithm As HashAlgorithm = md5) As String
Dim hCtx As Long
Dim hHash As Long
Dim lRes As Long
Dim lLen As Long
Dim lIdx As Long
Dim abData() As Byte ' Get default provider context handle
lRes = CryptAcquireContext(hCtx, vbNullString, _
vbNullString, PROV_RSA_FULL, 0) If lRes <> 0 Then ' Create the hash
lRes = CryptCreateHash(hCtx, Algorithm, 0, 0, hHash) If lRes <> 0 Then ' Hash the string
lRes = CryptHashData(hHash, ByVal Str, Len(Str), 0) If lRes <> 0 Then
' Get the hash lenght
lRes = CryptGetHashParam(hHash, HP_HASHSIZE, lLen, 4, 0) If lRes <> 0 Then ' Initialize the buffer
ReDim abData(0 To lLen - 1) ' Get the hash value
lRes = CryptGetHashParam(hHash, HP_HASHVAL, abData(0), lLen, 0) If lRes <> 0 Then ' Convert value to hex string
For lIdx = 0 To UBound(abData)
HashString = HashString & _
Right$("0" & Hex$(abData(lIdx)), 2)
Next End If End If End If ' Release the hash handle
CryptDestroyHash hHash End If
End If ' Release the provider context
CryptReleaseContext hCtx, 0 ' Raise an error if lRes = 0
If lRes = 0 Then Err.Raise Err.LastDllErrorEnd FunctionFunction HashFile( _
ByVal Filename As String, _
Optional ByVal Algorithm As HashAlgorithm = md5) As String
Dim hCtx As Long
Dim hHash As Long
Dim lFile As Long
Dim lRes As Long
Dim lLen As Long
Dim lIdx As Long
Dim abHash() As Byte ' Check if the file exists (not the best method BTW!)
If Len(Dir$(Filename)) = 0 Then Err.Raise 53
' Get default provider context handle
lRes = CryptAcquireContext(hCtx, vbNullString, _
vbNullString, PROV_RSA_FULL, 0) If lRes = 0 And Err.LastDllError = &H80090016 Then
' There's no default keyset container!!!
' Get the provider context and create
' a default keyset container
lRes = CryptAcquireContext(hCtx, vbNullString, _
vbNullString, PROV_RSA_FULL, CRYPT_NEWKEYSET)
End If
If lRes <> 0 Then ' Create the hash
lRes = CryptCreateHash(hCtx, Algorithm, 0, 0, hHash) If lRes <> 0 Then ' Get a file handle
lFile = FreeFile
' Open the file
Open Filename For Binary As lFile
If Err.Number = 0 Then
Const BLOCK_SIZE As Long = 32 * 1024& ' 32K
ReDim abBlock(1 To BLOCK_SIZE) As Byte
Dim lCount As Long
Dim lBlocks As Long
Dim lLastBlock As Long
' Calculate how many full blocks
' the file contains
lBlocks = LOF(lFile) \ BLOCK_SIZE
' Calculate the remaining data length
lLastBlock = LOF(lFile) - lBlocks * BLOCK_SIZE
' Hash the blocks
For lCount = 1 To lBlocks
Get lFile, , abBlock
' Add the chunk to the hash
lRes = CryptHashData(hHash, abBlock(1), BLOCK_SIZE, 0)
' Stop the loop if CryptHashData fails
If lRes = 0 Then Exit For
Next ' Is there more data?
If lLastBlock > 0 And lRes <> 0 Then
' Get the last block
ReDim abBlock(1 To lLastBlock) As Byte
Get lFile, , abBlock
' Hash the last block
lRes = CryptHashData(hHash, abBlock(1), lLastBlock, 0)
End If
' Close the file
Close lFile
End If If lRes <> 0 Then
' Get the hash lenght
lRes = CryptGetHashParam(hHash, HP_HASHSIZE, lLen, 4, 0) If lRes <> 0 Then ' Initialize the buffer
ReDim abHash(0 To lLen - 1) ' Get the hash value
lRes = CryptGetHashParam(hHash, HP_HASHVAL, abHash(0), lLen, 0) If lRes <> 0 Then ' Convert value to hex string
For lIdx = 0 To UBound(abHash)
HashFile = HashFile & _
Right$("0" & Hex$(abHash(lIdx)), 2)
Next End If End If End If ' Release the hash handle
CryptDestroyHash hHash End If
End If ' Release the provider context
CryptReleaseContext hCtx, 0 ' Raise an error if lRes = 0
If lRes = 0 Then Err.Raise Err.LastDllErrorEnd Function
使用时:Private Sub Form_Load()
Dim mymd5 As New ClsAPIMD5
Dim md5str As String
md5str = mymd5.HashString(" test", md5)
Set mymd5 = Nothing '用完释放内存End Sub
Private Declare Function CryptAcquireContext Lib "advapi32.dll" _
Alias "CryptAcquireContextA" ( _
ByRef phProv As Long, _
ByVal pszContainer As String, _
ByVal pszProvider As String, _
ByVal dwProvType As Long, _
ByVal dwFlags As Long) As LongPrivate Declare Function CryptReleaseContext Lib "advapi32.dll" ( _
ByVal hProv As Long, _
ByVal dwFlags As Long) As LongPrivate Declare Function CryptCreateHash Lib "advapi32.dll" ( _
ByVal hProv As Long, _
ByVal Algid As Long, _
ByVal hKey As Long, _
ByVal dwFlags As Long, _
ByRef phHash As Long) As LongPrivate Declare Function CryptDestroyHash Lib "advapi32.dll" ( _
ByVal hHash As Long) As LongPrivate Declare Function CryptHashData Lib "advapi32.dll" ( _
ByVal hHash As Long, _
pbData As Any, _
ByVal dwDataLen As Long, _
ByVal dwFlags As Long) As LongPrivate Declare Function CryptGetHashParam Lib "advapi32.dll" ( _
ByVal hHash As Long, _
ByVal dwParam As Long, _
pbData As Any, _
pdwDataLen As Long, _
ByVal dwFlags As Long) As LongPrivate Const PROV_RSA_FULL = 1Private Const ALG_CLASS_HASH = 32768Private Const ALG_TYPE_ANY = 0Private Const ALG_SID_MD2 = 1
Private Const ALG_SID_MD4 = 2
Private Const ALG_SID_MD5 = 3
Private Const ALG_SID_SHA1 = 4Enum HashAlgorithm
md2 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2
MD4 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4
md5 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5
SHA1 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA1
End EnumPrivate Const HP_HASHVAL = 2
Private Const HP_HASHSIZE = 4Function HashString( _
ByVal Str As String, _
Optional ByVal Algorithm As HashAlgorithm = md5) As String
Dim hCtx As Long
Dim hHash As Long
Dim lRes As Long
Dim lLen As Long
Dim lIdx As Long
Dim abData() As Byte ' Get default provider context handle
lRes = CryptAcquireContext(hCtx, vbNullString, _
vbNullString, PROV_RSA_FULL, 0) If lRes <> 0 Then ' Create the hash
lRes = CryptCreateHash(hCtx, Algorithm, 0, 0, hHash) If lRes <> 0 Then ' Hash the string
lRes = CryptHashData(hHash, ByVal Str, Len(Str), 0) If lRes <> 0 Then
' Get the hash lenght
lRes = CryptGetHashParam(hHash, HP_HASHSIZE, lLen, 4, 0) If lRes <> 0 Then ' Initialize the buffer
ReDim abData(0 To lLen - 1) ' Get the hash value
lRes = CryptGetHashParam(hHash, HP_HASHVAL, abData(0), lLen, 0) If lRes <> 0 Then ' Convert value to hex string
For lIdx = 0 To UBound(abData)
HashString = HashString & _
Right$("0" & Hex$(abData(lIdx)), 2)
Next End If End If End If ' Release the hash handle
CryptDestroyHash hHash End If
End If ' Release the provider context
CryptReleaseContext hCtx, 0 ' Raise an error if lRes = 0
If lRes = 0 Then Err.Raise Err.LastDllErrorEnd FunctionFunction HashFile( _
ByVal Filename As String, _
Optional ByVal Algorithm As HashAlgorithm = md5) As String
Dim hCtx As Long
Dim hHash As Long
Dim lFile As Long
Dim lRes As Long
Dim lLen As Long
Dim lIdx As Long
Dim abHash() As Byte ' Check if the file exists (not the best method BTW!)
If Len(Dir$(Filename)) = 0 Then Err.Raise 53
' Get default provider context handle
lRes = CryptAcquireContext(hCtx, vbNullString, _
vbNullString, PROV_RSA_FULL, 0) If lRes = 0 And Err.LastDllError = &H80090016 Then
' There's no default keyset container!!!
' Get the provider context and create
' a default keyset container
lRes = CryptAcquireContext(hCtx, vbNullString, _
vbNullString, PROV_RSA_FULL, CRYPT_NEWKEYSET)
End If
If lRes <> 0 Then ' Create the hash
lRes = CryptCreateHash(hCtx, Algorithm, 0, 0, hHash) If lRes <> 0 Then ' Get a file handle
lFile = FreeFile
' Open the file
Open Filename For Binary As lFile
If Err.Number = 0 Then
Const BLOCK_SIZE As Long = 32 * 1024& ' 32K
ReDim abBlock(1 To BLOCK_SIZE) As Byte
Dim lCount As Long
Dim lBlocks As Long
Dim lLastBlock As Long
' Calculate how many full blocks
' the file contains
lBlocks = LOF(lFile) \ BLOCK_SIZE
' Calculate the remaining data length
lLastBlock = LOF(lFile) - lBlocks * BLOCK_SIZE
' Hash the blocks
For lCount = 1 To lBlocks
Get lFile, , abBlock
' Add the chunk to the hash
lRes = CryptHashData(hHash, abBlock(1), BLOCK_SIZE, 0)
' Stop the loop if CryptHashData fails
If lRes = 0 Then Exit For
Next ' Is there more data?
If lLastBlock > 0 And lRes <> 0 Then
' Get the last block
ReDim abBlock(1 To lLastBlock) As Byte
Get lFile, , abBlock
' Hash the last block
lRes = CryptHashData(hHash, abBlock(1), lLastBlock, 0)
End If
' Close the file
Close lFile
End If If lRes <> 0 Then
' Get the hash lenght
lRes = CryptGetHashParam(hHash, HP_HASHSIZE, lLen, 4, 0) If lRes <> 0 Then ' Initialize the buffer
ReDim abHash(0 To lLen - 1) ' Get the hash value
lRes = CryptGetHashParam(hHash, HP_HASHVAL, abHash(0), lLen, 0) If lRes <> 0 Then ' Convert value to hex string
For lIdx = 0 To UBound(abHash)
HashFile = HashFile & _
Right$("0" & Hex$(abHash(lIdx)), 2)
Next End If End If End If ' Release the hash handle
CryptDestroyHash hHash End If
End If ' Release the provider context
CryptReleaseContext hCtx, 0 ' Raise an error if lRes = 0
If lRes = 0 Then Err.Raise Err.LastDllErrorEnd Function
使用时:Private Sub Form_Load()
Dim mymd5 As New ClsAPIMD5
Dim md5str As String
md5str = mymd5.HashString(" test", md5)
Set mymd5 = Nothing '用完释放内存End Sub
2013-05-24
展开全部
你的思路完全正确,Md5的Function可以直接到百度里搜索下,现成的很多,一般都是来自最早的一个外国牛人写的.另外.你的程序再小改一下:md5mima = "事先算出的MD5值"If md5(password.Text) = md5mima Then 事实上,写网站的话,都是这么处理的
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询