【问题标题】:Classic ASP with Base64 Type Hex带有 Base64 类型十六进制的经典 ASP
【发布时间】:2020-12-04 20:16:58
【问题描述】:

此代码使用 sha256 加密

7353cf97ed9471d8b1ca180b6277f855f27214668d40d3b0134b8c91c8bb51a8

我使用 Base64 编码时的结果

NzM1M2NmOTdlZDk0NzFkOGIxY2ExODBiNjI3N2Y4NTVmMjcyMTQ2NjhkNDBkM2IwMTM0YjhjOTFjOGJiNTFhOA==

但我想得到这样的结果。

c1PPl+2UcdixyhgLYnf4VfJyFGaNQNOwE0uMkci7Uag=

https://emn178.github.io/online-tools/base64_encode.html 我可以在这个在线转换器网站上得到这个结果。 (必须选择hex输入类型)

我使用的base64代码:

Function Base64Encode(sText)
    Dim oXML, oNode
    Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
    Set oNode = oXML.CreateElement("base64")
    oNode.dataType = "bin.base64"
    oNode.nodeTypedValue = Stream_StringToBinary(sText)
    Base64Encode = oNode.text
    Set oNode = Nothing
    Set oXML = Nothing
End Function
Function Base64Decode(ByVal vCode)
    Dim oXML, oNode
    Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
    Set oNode = oXML.CreateElement("base64")
    oNode.dataType = "bin.base64"
    oNode.text = vCode
    Base64Decode = Stream_BinaryToString(oNode.nodeTypedValue)
    Set oNode = Nothing
    Set oXML = Nothing
End Function
Private Function Stream_StringToBinary(Text)
  Const adTypeText = 2
  Const adTypeBinary = 1
  Dim BinaryStream 'As New Stream
  Set BinaryStream = CreateObject("ADODB.Stream")
  BinaryStream.Type = adTypeText
  BinaryStream.CharSet = "us-ascii"
  BinaryStream.Open
  BinaryStream.WriteText Text
  BinaryStream.Position = 0
  BinaryStream.Type = adTypeBinary
  BinaryStream.Position = 0
  Stream_StringToBinary = BinaryStream.Read
  Set BinaryStream = Nothing
End Function
Private Function Stream_BinaryToString(Binary)
  Const adTypeText = 2
  Const adTypeBinary = 1
  Dim BinaryStream 'As New Stream
  Set BinaryStream = CreateObject("ADODB.Stream")
  BinaryStream.Type = adTypeBinary
  BinaryStream.Open
  BinaryStream.Write Binary
  BinaryStream.Position = 0
  BinaryStream.Type = adTypeText
  BinaryStream.CharSet = "us-ascii"
  Stream_BinaryToString = BinaryStream.ReadText
  Set BinaryStream = Nothing
End Function

我该如何使用经典的 asp

【问题讨论】:

  • 你确定你使用的是正确的编码,不应该是 UTF-8 而不是 ASCII 吗?

标签: asp-classic base64 hex


【解决方案1】:

你必须先解码十六进制字符串。

得到对应的原始值后,可以将其转换为Base64字符串。

Function HexStringToBytes(hexString)
    With CreateObject("MSXML2.DOMDocument")
        .LoadXml "<node/>"
        With .DocumentElement
            .DataType = "bin.hex"
            .Text = hexString
            HexStringToBytes = .NodeTypedValue
        End With
    End With
End Function

Function BytesToBase64String(bytes)
    With CreateObject("MSXML2.DOMDocument")
        .LoadXml "<node/>"
        With .DocumentElement
            .DataType = "bin.base64"
            .NodeTypedValue = bytes
            BytesToBase64String = Replace(.Text, vbLf, "")
        End With
    End With
End Function

Function HexStringToBase64String(hexString)
    Dim bytes, base64string
    
    bytes = HexStringToBytes(hexString)
    base64string = BytesToBase64String(bytes)
    
    HexStringToBase64String = base64string
End Function

hexStr = "7353cf97ed9471d8b1ca180b6277f855f27214668d40d3b0134b8c91c8bb51a8"

base64str = HexStringToBase64String(hexStr)

'Response.Write(base64str) 'prints c1PPl+2UcdixyhgLYnf4VfJyFGaNQNOwE0uMkci7Uag=

【讨论】:

  • @Lankymart 你知道,很少能看到来自新贡献者的足够详细的问题。在可能的情况下,我会尽力解决此类问题。谢谢你的客气话。
  • 我有点专注于 ASCII 编码,甚至没有考虑过没有先将十六进制转换为字节的事实。
  • 非常感谢。我很感激。
猜你喜欢
  • 2017-12-05
  • 2013-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多