【问题标题】:How to generate MD5 using VBScript in classic ASP?如何在经典 ASP 中使用 VBScript 生成 MD5?
【发布时间】:2012-04-29 05:35:27
【问题描述】:

我需要在我的应用程序中生成一个 MD5。

我试过谷歌,但只能找到 MD5 的 PHP 代码。我需要连接到使用 MD5 哈希验证的客户端系统,但他们的代码是 PHP,我的是使用 VBScript 的经典 ASP。

我的服务器支持 .Net,所以我不能使用 PHP 脚本。 Classic ASP 中有没有这样的 VBScript MD5 代码?

【问题讨论】:

  • 另外:仅供参考,但 Windows IIS 将运行 php 代码就好了。只需使用 FastCGI 模块的处理程序创建一个不同的应用程序池。
  • md5/hash on vb6? 的可能重复项
  • 不不,不是 vb 6,而是 asp 页面中的 vb 脚本?
  • @Joel Coehoorn 我的 MD5 用于一页。你是说我可以在 php 中创建该页面,然后将该页面放入不同的应用程序池中?

标签: asp-classic vbscript md5 md5sum


【解决方案1】:

首先谢谢SgtWilko! :)

根据您收集的信息,我为所有人完成了一项功能(不适用于 base64/Files)。
您的代码对我非常有用,但我正在寻找一个更类似于 PHP(简单)的函数来处理纯文本和更明确的代码。

已编辑:
基于问题How to hash a UTF-8 string in Classic ASP,我提出了ADODB.Stream 解决方案。您现在可以使用非英文字符。

已编辑:
参数 PlainText 更改为 Target。 您现在可以使用 HMAC 版本。
只需将 Target 参数用作数组即可。

Target(0) = PlainText
Target(1) = SharedKey

再次感谢SgtWilko ;)

Announcing the first SHA1 collision(Google 安全博客)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" 如果您还在会话中设置它,asp 也会解释正确响应:Session.Codepage=65001
  • 这确实取决于@SgtWilko 建议在 ASP 页面中设置正确的编码,整个转换 ADODB.Stream 是毫无意义的步骤,可能会导致结果不一致。
【解决方案2】:

2017 年 2 月 21 日更新 - 现在为 JWT 添加了 HMACSHA256

2016-07-05 更新 - 现在添加了 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(对 Amazon S3 MD5 检查很有用):

BytesToBase64(md5hashBytes(GetBytes(sPath)))

其中 sPath 是本地文件的路径。

最后,创建 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 可执行实用程序对齐。
  • 这是薄荷糖。我一直在努力解决这个问题。很好地放在一起。
  • 非常好!正是我想要找到的。我刚刚添加了这一行 WScript.Echo bytesToHex(sha256hashBytes(GetBytes("the path to my file")))
【解决方案3】:

这是一个可读可下载的 MD5 VBS 脚本版本:

https://github.com/Wikinaut/md5.vbs

这是来自http://chayoung.tistory.com/entry/VBScript-MD5 的代码(感谢您提供这段独特的代码)。

【讨论】:

  • 工作没有问题,太棒了!
【解决方案4】:

感谢上面提供的所有链接,它们很有用,但如果有人需要,我发现这个确实可以完成工作。 VBScript-MD5

【讨论】:

    【解决方案5】:

    有产生 MD5 校验和的 Javascript 代码。其中之一来自 Google 闭包库,是 available here

    从 Javascript 生成 Windows 脚本组件非常容易,然后从任何支持 COM 的语言(包括 VB)调用该组件。

    Here's a working example.

    【讨论】:

    • 谢谢,我也试试这个
    • 这些链接似乎已失效,仅供参考
    【解决方案6】:

    我不知道这段代码是否有效,因为我无法测试它。但是,这似乎是您所要求的。

    http://www.bullzip.com/md5/vb/md5-vb-class.htm

    这是 Jeff Attwood 关于哈希的一篇有趣的文章。关于 MD5,他有一些重要的话要说:

    http://www.codinghorror.com/blog/2012/04/speed-hashing.html

    【讨论】:

    • 感谢@camainc 的阅读,但我没有太多选择,我连接的系统大量使用 MD5 哈希键进行身份验证,这些是他们为连接而给出的要求给他们。我会试试bullzip代码并提供建议
    • 这甚至不是vbscript 的实现是针对vb
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多