VBS base64转码
时间: 2023-07-06 14:34:57 浏览: 181
base64转码
4星 · 用户满意度95%
以下是在 VBS 中实现 base64 编码和解码的示例代码:
```
' 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
' base64 解码
Function Base64Decode(sText)
Dim oXML, oNode
Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
Set oNode = oXML.CreateElement("base64")
oNode.DataType = "bin.base64"
oNode.Text = sText
Base64Decode = Stream_BinaryToString(oNode.nodeTypedValue)
Set oNode = Nothing
Set oXML = Nothing
End Function
' 将字符串转换为二进制流
Function Stream_StringToBinary(sText)
Dim oStream, iPosition
Set oStream = CreateObject("ADODB.Stream")
oStream.Type = 2 ' adTypeText
oStream.Charset = "us-ascii"
oStream.Open
oStream.WriteText sText
oStream.Position = 0
oStream.Type = 1 ' adTypeBinary
oStream.Position = 0
Stream_StringToBinary = oStream.Read
Set oStream = Nothing
End Function
' 将二进制流转换为字符串
Function Stream_BinaryToString(Binary)
Dim oStream
Set oStream = CreateObject("ADODB.Stream")
oStream.Type = 1 ' adTypeBinary
oStream.Open
oStream.Write Binary
oStream.Position = 0
oStream.Type = 2 ' adTypeText
oStream.Charset = "us-ascii"
Stream_BinaryToString = oStream.ReadText
Set oStream = Nothing
End Function
```
您可以调用 `Base64Encode` 函数将字符串进行 base64 编码,调用 `Base64Decode` 函数将 base64 编码的字符串进行解码。例如:
```
' 编码
sText = "20230601007436"
sEncoded = Base64Encode(sText)
WScript.Echo sEncoded
' 解码
sEncoded = "MjAyMzA2MDEwMDc0MzY="
sText = Base64Decode(sEncoded)
WScript.Echo sText
```
阅读全文