我需要在我的應用程序中生成MD5。如何在傳統ASP中使用VBScript生成MD5?
我試過谷歌,但只能找到MD5的PHP代碼。我需要連接到使用MD5哈希驗證的客戶端系統,但它們的代碼是使用PHP,而我的使用VBScript使用的是傳統ASP。
我的服務器支持.Net,因此我無法使用PHP腳本。 Classic ASP中有沒有VBScript的MD5代碼?
我需要在我的應用程序中生成MD5。如何在傳統ASP中使用VBScript生成MD5?
我試過谷歌,但只能找到MD5的PHP代碼。我需要連接到使用MD5哈希驗證的客戶端系統,但它們的代碼是使用PHP,而我的使用VBScript使用的是傳統ASP。
我的服務器支持.Net,因此我無法使用PHP腳本。 Classic ASP中有沒有VBScript的MD5代碼?
我不知道這段代碼是否工作,因爲我沒有辦法測試它。但是,這似乎是你要求的。
http://www.bullzip.com/md5/vb/md5-vb-class.htm
這裏是哈希一篇有趣的文章由傑夫·阿特伍德。他有一些重要的東西,說MD5:
感謝閱讀@camainc,但我沒有太多的選擇,我連接的系統大量使用MD5哈希鍵進行仲裁,並且這些是他們連接到它們的要求。我會嘗試一下bullzip代碼,並建議 – user1270384 2012-04-17 20:51:37
我瞭解.... – camainc 2012-04-17 20:53:50
這甚至不是[tag:vbscript]實現是[tag:vb]。 – Lankymart 2015-09-10 15:48:39
有產生一個MD5校驗和JavaScript代碼。其中一個來自Google關閉庫,是available here。
從Javascript生成Windows腳本組件很容易,然後從任何支持COM的語言(包括VB)調用該組件。
謝謝我也會嘗試這個以及 – user1270384 2012-04-17 20:52:04
這些鏈接似乎已經死了,fyi – 2015-11-17 21:02:33
感謝上面提供的所有鏈接,它們很有用,但如果有人需要它,我發現這個工作真的很有用。 VBScript-MD5
這裏是MD5的可讀和可下載的版本VBS腳本:
https://github.com/Wikinaut/md5.vbs
這是一個從http://chayoung.tistory.com/entry/VBScript-MD5代碼(感謝您對這種獨特的代碼)。
沒有問題的作品,太棒了! – Mat41 2014-10-13 00:24:45
更新2017年2月21日 - 現在添加了HMACSHA256爲JWTs
更新2016年7月5日 - 添加了SHA1和SHA256
權,所有現在你一直在與這個(像我一樣)掙扎,並想知道,這是可能的!
下面的代碼被分成幾個函數,以便您可以MD5/sha1/sha256字符串或文件。
我從另一個stackexchange中借用函數GetBytes和BytesToBase64,stringToUTFBytes中的代碼基於另一個stackexchange。
function md5hashBytes(aBytes)
Dim MD5
set MD5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
MD5.Initialize()
'Note you MUST use computehash_2 to get the correct version of this method, and the bytes MUST be double wrapped in brackets to ensure they get passed in correctly.
md5hashBytes = MD5.ComputeHash_2((aBytes))
end function
function sha1hashBytes(aBytes)
Dim sha1
set sha1 = CreateObject("System.Security.Cryptography.SHA1Managed")
sha1.Initialize()
'Note you MUST use computehash_2 to get the correct version of this method, and the bytes MUST be double wrapped in brackets to ensure they get passed in correctly.
sha1hashBytes = sha1.ComputeHash_2((aBytes))
end function
function sha256hashBytes(aBytes)
Dim sha256
set sha256 = CreateObject("System.Security.Cryptography.SHA256Managed")
sha256.Initialize()
'Note you MUST use computehash_2 to get the correct version of this method, and the bytes MUST be double wrapped in brackets to ensure they get passed in correctly.
sha256hashBytes = sha256.ComputeHash_2((aBytes))
end function
function sha256HMACBytes(aBytes, aKey)
Dim sha256
set sha256 = CreateObject("System.Security.Cryptography.HMACSHA256")
sha256.Initialize()
sha256.key=aKey
'Note you MUST use computehash_2 to get the correct version of this method, and the bytes MUST be double wrapped in brackets to ensure they get passed in correctly.
sha256HMACBytes = sha256.ComputeHash_2((aBytes))
end function
function stringToUTFBytes(aString)
Dim UTF8
Set UTF8 = CreateObject("System.Text.UTF8Encoding")
stringToUTFBytes = UTF8.GetBytes_4(aString)
end function
function bytesToHex(aBytes)
dim hexStr, x
for x=1 to lenb(aBytes)
hexStr= hex(ascb(midb((aBytes),x,1)))
if len(hexStr)=1 then hexStr="0" & hexStr
bytesToHex=bytesToHex & hexStr
next
end function
Function BytesToBase64(varBytes)
With CreateObject("MSXML2.DomDocument").CreateElement("b64")
.dataType = "bin.base64"
.nodeTypedValue = varBytes
BytesToBase64 = .Text
End With
End Function
'Special version that produces the URLEncoded variant of Base64 used in JWTs.
Function BytesToBase64UrlEncode(varBytes)
With CreateObject("MSXML2.DomDocument").CreateElement("b64")
.dataType = "bin.base64"
.nodeTypedValue = varBytes
BytesToBase64UrlEncode = replace(replace(replace(replace(replace(.Text,chr(13),""),chr(10),""),"+", "-"),"/", "_"),"=", "")
End With
End Function
Function GetBytes(sPath)
With CreateObject("Adodb.Stream")
.Type = 1 ' adTypeBinary
.Open
.LoadFromFile sPath
.Position = 0
GetBytes = .Read
.Close
End With
End Function
這些可以使用如下:
BytesToBase64(md5hashBytes(stringToUTFBytes("Hello World")))
產地:sQqNsWTgdUEFt6mb5y4/5Q ==
bytesToHex(md5hashBytes(stringToUTFBytes("Hello World")))
產地:B10A8DB164E0754105B7A99BE72E3FE5
對於SHA1:
bytesToHex(sha1hashBytes(stringToUTFBytes("Hello World")))
產地:0A4D55A8D778E5022FAB701977C5D840BBC486D0
對於SHA256:
bytesToHex(sha256hashBytes(stringToUTFBytes("Hello World")))
產地:A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E
要獲得文件的MD5(亞馬遜S3 MD5校驗有用):
BytesToBase64(md5hashBytes(GetBytes(sPath)))
其中sPath是th的路徑本地文件。
最後,創建一個JWT:
'define the JWT header, needs to be converted to UTF bytes:
aHead=stringToUTFBytes("{""alg"":""HS256"",""typ"":""JWT""}")
'define the JWT payload, again needs to be converted to UTF Bytes.
aPayload=stringToUTFBytes("{""sub"":""1234567890"",""name"":""John Doe"",""admin"":true}")
'Your shared key.
theKey="mySuperSecret"
aSigSource=stringToUTFBytes(BytesToBase64UrlEncode(aHead) & "." & BytesToBase64UrlEncode(aPayload))
'The full JWT correctly Base 64 URL encoded.
aJWT=BytesToBase64UrlEncode(aHead) & "." & BytesToBase64UrlEncode(aPayload) & "." & BytesToBase64UrlEncode(sha256HMACBytes(aSigSource,stringToUTFBytes(theKey)))
將產生以下有效JWT: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.7ofvtkn0z_pTl6WcqRTxw-4eSE3NqcEq9_3ax0YcuIQ
到目前爲止的最佳答案...正是我在找的東西,除了我用bytesToHex(md5hashBytes(GetBytes(sPath)))從文件中獲取我需要的值。其他工作的優秀彙編先生和最好的部分是沒有雜項可執行文件或庫需要。 MD5與sigcheck和其他misc可執行實用程序直接對齊。 – 2015-10-23 17:19:55
這是薄荷。我一直在苦苦掙扎。很好地放在一起。 – GWR 2016-01-29 03:05:53
非常好!正是我想找到的。我剛剛添加了這行WScript.Echo bytesToHex(sha256hashBytes(GetBytes(「我的文件的路徑」))) – Magnus 2017-12-31 14:25:35
首先,謝謝SgtWilko! :)
根據您收集的信息,我已經完成了所有功能(不適用於base64 /文件)。
你的代碼對我來說非常有用,但我正在尋找一個更類似於PHP的(簡單)函數來處理純文本和更明確的代碼。
編輯:
基於問題How to hash a UTF-8 string in Classic ASP,我拿出ADODB.Stream解決方案。您現在可以使用非英文字符。
編輯:
參數純文本改爲目標。 您現在可以使用HMAC版本。
只需使用Target參數作爲數組。
Target(0) = PlainText
Target(1) = SharedKey
再次SgtWilko謝謝;)
Announcing the first SHA1 collision(谷歌安全博客)2017年2月23日。
有了這個功能,你可以哈希純文本:
MD5,RIPEMD160,SHA1,SHA256,SHA384,SHA512,HMACMD5,HMACRIPEMD160,HMACSHA1,HMACSHA256,HMACSHA384和HMACSHA512
如果您需要更多,你可以發現它在:System.Security.Cryptography Namespace
Function Hash(HashType, Target)
On Error Resume Next
Dim PlainText
If IsArray(Target) = True Then PlainText = Target(0) Else PlainText = Target End If
With CreateObject("ADODB.Stream")
.Open
.CharSet = "Windows-1252"
.WriteText PlainText
.Position = 0
.CharSet = "UTF-8"
PlainText = .ReadText
.Close
End With
Set UTF8Encoding = CreateObject("System.Text.UTF8Encoding")
Dim PlainTextToBytes, BytesToHashedBytes, HashedBytesToHex
PlainTextToBytes = UTF8Encoding.GetBytes_4(PlainText)
Select Case HashType
Case "md5": Set Cryptography = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider") '< 64 (collisions found)
Case "ripemd160": Set Cryptography = CreateObject("System.Security.Cryptography.RIPEMD160Managed")
Case "sha1": Set Cryptography = CreateObject("System.Security.Cryptography.SHA1Managed") '< 80 (collision found)
Case "sha256": Set Cryptography = CreateObject("System.Security.Cryptography.SHA256Managed")
Case "sha384": Set Cryptography = CreateObject("System.Security.Cryptography.SHA384Managed")
Case "sha512": Set Cryptography = CreateObject("System.Security.Cryptography.SHA512Managed")
Case "md5HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACMD5")
Case "ripemd160HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACRIPEMD160")
Case "sha1HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACSHA1")
Case "sha256HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACSHA256")
Case "sha384HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACSHA384")
Case "sha512HMAC": Set Cryptography = CreateObject("System.Security.Cryptography.HMACSHA512")
End Select
Cryptography.Initialize()
If IsArray(Target) = True Then Cryptography.Key = UTF8Encoding.GetBytes_4(Target(1))
BytesToHashedBytes = Cryptography.ComputeHash_2((PlainTextToBytes))
For x = 1 To LenB(BytesToHashedBytes)
HashedBytesToHex = HashedBytesToHex & Right("0" & Hex(AscB(MidB(BytesToHashedBytes, x, 1))), 2)
Next
If Err.Number <> 0 Then Response.Write(Err.Description) Else Hash = LCase(HashedBytesToHex)
On Error GoTo 0
End Function
這些可以使用如下:
Hash("sha512", "Hello World")
產地:
2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0daa8f51671064615d645ae27acb15bfb1447f459b
Hash("sha256", "Hello World")
產地:
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
Hash("md5", "muñeca")
產地:
ea07bec1f37f4b56ebe368355d1c058f
Hash("sha512HMAC", Array("Hello World", "Shared Key"))
產地:
28e72824c48da5a5f14b59246905d2839e7c50e271fc078b1c0a75c89b6a3998746bd8b2dc1764b19d312702cf5e15b38ce799156af28b98ce08b85e4df65b32
很高興我能幫上忙。 您可以通過設置代碼頁和字符集在ASP文件,以繞過UTF-8的問題: Response.CodePage = 65001 Response.Charset的=「UTF-8」 ,如果你還要把它設置在session asp也會正確解釋響應: Session.Codepage = 65001 – SgtWilko 2017-06-16 14:25:21
另外:僅供參考,但Windows IIS運行PHP代碼就好了。只需使用FastCGI模塊的處理程序創建一個不同的應用程序池即可。 – 2012-04-17 20:40:52
可能重複的[md5 /散列在vb6?](http://stackoverflow.com/questions/6579523/md5-hash-on-vb6) – 2012-04-17 20:43:12
不是不是不是vb 6,但在asp頁面的VB腳本? – user1270384 2012-04-17 20:53:32