Classic ASP with Base64 Type Hex

Viewed 608

This code encrypted with sha256

7353cf97ed9471d8b1ca180b6277f855f27214668d40d3b0134b8c91c8bb51a8

The result when I encode with Base64

NzM1M2NmOTdlZDk0NzFkOGIxY2ExODBiNjI3N2Y4NTVmMjcyMTQ2NjhkNDBkM2IwMTM0YjhjOTFjOGJiNTFhOA==

but I want to get a result like this.

c1PPl+2UcdixyhgLYnf4VfJyFGaNQNOwE0uMkci7Uag=

https://emn178.github.io/online-tools/base64_encode.html I can get this result on this online converter site. (You must select the hex input type)

base64 code I use:

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

how can i do with classic asp

1 Answers

You must decode hex string first.

After getting corresponding raw value, you can convert it to Base64 string.

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=
Related