Classic ASP 与 Base64 类型十六进制

Classic ASP with Base64 Type Hex

此代码使用 sha256 加密

7353cf97ed9471d8b1ca180b6277f855f27214668d40d3b0134b8c91c8bb51a8

我用Base64编码后的结果

NzM1M2NmOTdlZDk0NzFkOGIxY2ExODBiNjI3N2Y4NTVmMjcyMTQ2NjhkNDBkM2IwMTM0YjhjOTFjOGJiNTFhOA==

但我想得到这样的结果。

c1PPl+2UcdixyhgLYnf4VfJyFGaNQNOwE0uMkci7Uag=

https://emn178.github.io/online-tools/base64_encode.html 我可以在这个在线转换器网站上得到这个结果。 (您必须 select 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

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

得到相应的原始值后,可以将其转换为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=