Encoding VBScript中的Base64编码字符串
我有一个web服务加载驱动程序,它是一个Windows脚本文件(WSF),其中包括一些VBScript和JavaScript文件。我的web服务要求传入消息是base64编码的。我目前有一个VBScript函数可以实现这一点,但它的效率非常低(内存密集,主要是由于VBScript糟糕的字符串连接) [旁白;是的,我看到了。连接是在1000到10000字节大小的消息之间的循环中发生的。] 我尝试过使用一些自定义字符串连接例程;一个使用数组,另一个使用ADODB.Stream。这些有一点帮助,但我认为如果我有其他的方式来编码信息,而不是通过我自己的VBS函数,会有更大的帮助Encoding VBScript中的Base64编码字符串,encoding,vbscript,base64,wsh,Encoding,Vbscript,Base64,Wsh,我有一个web服务加载驱动程序,它是一个Windows脚本文件(WSF),其中包括一些VBScript和JavaScript文件。我的web服务要求传入消息是base64编码的。我目前有一个VBScript函数可以实现这一点,但它的效率非常低(内存密集,主要是由于VBScript糟糕的字符串连接) [旁白;是的,我看到了。连接是在1000到10000字节大小的消息之间的循环中发生的。] 我尝试过使用一些自定义字符串连接例程;一个使用数组,另一个使用ADODB.Stream。这些有一点帮助,但我认
是否有其他方式来编码我的消息,更倾向于使用本机Windows方法?我最初使用的是Antonin Foller提供的一些VBScript代码: 和 在搜索安东宁的网站时,我看到他有一些代码,所以我试了一下 最后,我移植了Mark对VBScript的回答中提到的代码(也使用了SO问题中的一些代码),并使用Antonin站点中的and函数来获取使用MSXML编码的函数 我运行了一个快速测试来测量所有四种方法中1500个字符的消息(我需要发送到web服务的平均消息大小)的编码时间:
- 本机VBScript(VBScript)
- 引用可打印,使用CDO.Message(QP)
- 引用的可打印二进制文件,使用CDO.Message(QP二进制文件)
- MSXML/ADODB.Stream(MSXML)
我最初使用的是Antonin Foller的一些VBScript代码: 和 在搜索安东宁的网站时,我看到他有一些代码,所以我试了一下 最后,我移植了Mark对VBScript的回答中提到的代码(也使用了SO问题中的一些代码),并使用Antonin站点中的and函数来获取使用MSXML编码的函数 我运行了一个快速测试来测量所有四种方法中1500个字符的消息(我需要发送到web服务的平均消息大小)的编码时间:
- 本机VBScript(VBScript)
- 引用可打印,使用CDO.Message(QP)
- 引用的可打印二进制文件,使用CDO.Message(QP二进制文件)
- MSXML/ADODB.Stream(MSXML)
我还有一些编码器和解码器的完整示例: 编码器:
' This script reads jpg picture named SuperPicture.jpg, converts it to base64
' code using encoding abilities of MSXml2.DOMDocument object and saves
' the resulting data to encoded.txt file
Option Explicit
Const fsDoOverwrite = true ' Overwrite file with base64 code
Const fsAsASCII = false ' Create base64 code file as ASCII file
Const adTypeBinary = 1 ' Binary file is encoded
' Variables for writing base64 code to file
Dim objFSO
Dim objFileOut
' Variables for encoding
Dim objXML
Dim objDocElem
' Variable for reading binary picture
Dim objStream
' Open data stream from picture
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary
objStream.Open()
objStream.LoadFromFile("SuperPicture.jpg")
' Create XML Document object and root node
' that will contain the data
Set objXML = CreateObject("MSXml2.DOMDocument")
Set objDocElem = objXML.createElement("Base64Data")
objDocElem.dataType = "bin.base64"
' Set binary value
objDocElem.nodeTypedValue = objStream.Read()
' Open data stream to base64 code file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileOut = objFSO.CreateTextFile("encoded.txt", fsDoOverwrite, fsAsASCII)
' Get base64 value and write to file
objFileOut.Write objDocElem.text
objFileOut.Close()
' Clean all
Set objFSO = Nothing
Set objFileOut = Nothing
Set objXML = Nothing
Set objDocElem = Nothing
Set objStream = Nothing
解码器:
' This script reads base64 encoded picture from file named encoded.txt,
' converts it in to back to binary reprisentation using encoding abilities
' of MSXml2.DOMDocument object and saves data to SuperPicture.jpg file
Option Explicit
Const foForReading = 1 ' Open base 64 code file for reading
Const foAsASCII = 0 ' Open base 64 code file as ASCII file
Const adSaveCreateOverWrite = 2 ' Mode for ADODB.Stream
Const adTypeBinary = 1 ' Binary file is encoded
' Variables for reading base64 code from file
Dim objFSO
Dim objFileIn
Dim objStreamIn
' Variables for decoding
Dim objXML
Dim objDocElem
' Variable for write binary picture
Dim objStream
' Open data stream from base64 code filr
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileIn = objFSO.GetFile("encoded.txt")
Set objStreamIn = objFileIn.OpenAsTextStream(foForReading, foAsASCII)
' Create XML Document object and root node
' that will contain the data
Set objXML = CreateObject("MSXml2.DOMDocument")
Set objDocElem = objXML.createElement("Base64Data")
objDocElem.DataType = "bin.base64"
' Set text value
objDocElem.text = objStreamIn.ReadAll()
' Open data stream to picture file
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary
objStream.Open()
' Get binary value and write to file
objStream.Write objDocElem.NodeTypedValue
objStream.SaveToFile "SuperPicture.jpg", adSaveCreateOverWrite
' Clean all
Set objFSO = Nothing
Set objFileIn = Nothing
Set objStreamIn = Nothing
Set objXML = Nothing
Set objDocElem = Nothing
Set objStream = Nothing
我还有一些编码器和解码器的完整示例: 编码器:
' This script reads jpg picture named SuperPicture.jpg, converts it to base64
' code using encoding abilities of MSXml2.DOMDocument object and saves
' the resulting data to encoded.txt file
Option Explicit
Const fsDoOverwrite = true ' Overwrite file with base64 code
Const fsAsASCII = false ' Create base64 code file as ASCII file
Const adTypeBinary = 1 ' Binary file is encoded
' Variables for writing base64 code to file
Dim objFSO
Dim objFileOut
' Variables for encoding
Dim objXML
Dim objDocElem
' Variable for reading binary picture
Dim objStream
' Open data stream from picture
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary
objStream.Open()
objStream.LoadFromFile("SuperPicture.jpg")
' Create XML Document object and root node
' that will contain the data
Set objXML = CreateObject("MSXml2.DOMDocument")
Set objDocElem = objXML.createElement("Base64Data")
objDocElem.dataType = "bin.base64"
' Set binary value
objDocElem.nodeTypedValue = objStream.Read()
' Open data stream to base64 code file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileOut = objFSO.CreateTextFile("encoded.txt", fsDoOverwrite, fsAsASCII)
' Get base64 value and write to file
objFileOut.Write objDocElem.text
objFileOut.Close()
' Clean all
Set objFSO = Nothing
Set objFileOut = Nothing
Set objXML = Nothing
Set objDocElem = Nothing
Set objStream = Nothing
解码器:
' This script reads base64 encoded picture from file named encoded.txt,
' converts it in to back to binary reprisentation using encoding abilities
' of MSXml2.DOMDocument object and saves data to SuperPicture.jpg file
Option Explicit
Const foForReading = 1 ' Open base 64 code file for reading
Const foAsASCII = 0 ' Open base 64 code file as ASCII file
Const adSaveCreateOverWrite = 2 ' Mode for ADODB.Stream
Const adTypeBinary = 1 ' Binary file is encoded
' Variables for reading base64 code from file
Dim objFSO
Dim objFileIn
Dim objStreamIn
' Variables for decoding
Dim objXML
Dim objDocElem
' Variable for write binary picture
Dim objStream
' Open data stream from base64 code filr
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileIn = objFSO.GetFile("encoded.txt")
Set objStreamIn = objFileIn.OpenAsTextStream(foForReading, foAsASCII)
' Create XML Document object and root node
' that will contain the data
Set objXML = CreateObject("MSXml2.DOMDocument")
Set objDocElem = objXML.createElement("Base64Data")
objDocElem.DataType = "bin.base64"
' Set text value
objDocElem.text = objStreamIn.ReadAll()
' Open data stream to picture file
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary
objStream.Open()
' Get binary value and write to file
objStream.Write objDocElem.NodeTypedValue
objStream.SaveToFile "SuperPicture.jpg", adSaveCreateOverWrite
' Clean all
Set objFSO = Nothing
Set objFileIn = Nothing
Set objStreamIn = Nothing
Set objXML = Nothing
Set objDocElem = Nothing
Set objStream = Nothing
这是一个不使用ADODB对象的解码示例
option explicit
dim inobj,outobj,infile,myname,state,rec,outfile,content,table(256),bits,c,x,outword
state = 0
const r64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
myname = wscript.scriptfullname
set inobj = createobject("Scripting.FileSystemObject")
set outobj = createobject("Scripting.FileSystemObject")
set infile = inobj.opentextfile(myname,1)
set outfile = outobj.createtextfile("q.png")
for x = 1 to 256 step 1
table(x) = -1
next
for x = 1 to 64 step 1
table(1+asc(mid(r64,x,1))) = x - 1
next
bits = 0
do until(infile.atendofstream)
dim size
rec = infile.readline
if (state = 1) then
content = mid(rec,2)
size = len(content)
for x = 1 to size step 1
c = table(1+asc(mid(content,x,1)))
if (c <> -1) then
if (bits = 0) then
outword = c*4
bits = 6
elseif (bits = 2) then
outword = c+outword
outfile.write(chr(clng("&H" & hex(outword mod 256))))
bits = 0
elseif (bits = 4) then
outword = outword + int(c/4)
outfile.write(chr(clng("&H" & hex(outword mod 256))))
outword = c*64
bits = 2
else
outword = outword + int(c/16)
outfile.write(chr(clng("&H" & hex(outword mod 256))))
outword = c*16
bits = 4
end if
end if
next
end if
if (rec = "'PAYLOAD") then
state = 1
end if
loop
infile.close
outfile.close
wscript.echo "q.png created"
wscript.quit
'PAYLOAD
'iVBORw0KGgoAAAANSUhEUgAAAD4AAAA+CAIAAAD8oz8TAAABoklEQVRo3u2awQrDMAxDl7H/
'/+Xu0EsgSDw7hRF7vWywpO0UW5acjOu6Xmde79ex1+f+GGPACfcqzePXdVvvts7iv6rx56Ou
'8FNYkgyZx9xzZ3TVHfg7VEHdR+o6ZsWV54O/yDvUQj2KzYyH5wof5f14fR97xdPrmjy1ArVQ
'55yteMYzEqma5B2qoM5VBK+OuXUrHutjJ8c59l4z/vV6Vv15PbOjiFRunB/rOcYgIz1jEPek
'nnh+rBPsiYbOaRu/DipzKrqkqNOJdgEIF3mNVLGa7jM9YSReg+t6U/UvFTYqmn13gGeUr9C1
'ul85rlCVgVTHnGeo2xGIdnT3PRR3vbUYhjAJqXxRHxTtslfsrxOe8aziWdlnAukRVPGmuX9P
'KnG0y9Wjv+71IPf8JEMIZxeP9ZHDkvO0z6XoXmlF1APTMIpR38R5qd8ZAa7gc76JaMl+ZwR4
'N0vdn6hRf89+ZwRIXZy/e473bks9sd9uterERvmbKP4end6cVlFRHt2n9mxTN9b3PTzfIco5
'4Ip9mGd1ud8bUriS3Oh6RuC318GofwHqKhl/Nn0DHQAAAABJRU5ErkJggg==
选项显式
dim inobj、outobj、INFLE、myname、state、rec、outfile、content、table(256)、bits、c、x、outword
状态=0
const r64=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz012456789+/”
myname=wscript.scriptfullname
set inobj=createobject(“Scripting.FileSystemObject”)
set-outobj=createobject(“Scripting.FileSystemObject”)
set infle=inobj.opentextfile(myname,1)
set outfile=outobj.createtextfile(“q.png”)
对于x=1到256,步骤1
表(x)=-1
下一个
对于x=1到64,步骤1
表(1+asc(mid(r64,x,1))=x-1
下一个
位=0
直到(填充atendofstream)
暗淡的尺寸
rec=infle.readline
如果(状态=1),则
内容=中间(记录,2)
大小=长度(内容)
对于x=1,调整步骤1的大小
c=表(1+asc(mid(内容,x,1)))
如果(c-1)那么
如果(位=0),则
向外=c*4
位=6
elseif(位=2)则
向外=c+向外
输出文件写入(chr(clng(“&H”&hex(输出模块256)))
位=0
elseif(位=4)则
外向=外向+内(c/4)
输出文件写入(chr(clng(“&H”&hex(输出模块256)))
向外=c*64
位=2
其他的
外向=外向+外向(c/16)
输出文件写入(chr(clng(“&H”&hex(输出模块256)))
向外=c*16
位=4
如果结束
如果结束
下一个
如果结束
如果(rec=“'PAYLOAD”),则
状态=1
如果结束
环
结束
关闭
wscript.echo“q.png已创建”
wscript.quit
“有效载荷
'Ivborw0kgoaaaansuhueugaaad4aaaa+caiaad8oz8taaabokleqvro3u2awqrdmaxdl7h/
'/+xu0esgsdw7hrf7vwwwpo0uw5acjou6xmde79ex1+f+ggpacfcqzepxdvvvvts7iv6rx56ou
'8FNYkgyZx9xzZ3TVHfg7VEHdR+o6ZsWV54O/yDvUQj2KzYyH5wof5f14fR97xdPrmjy1ArVQ
'55yteMYzEqma5B2qoM5VBK+OuxurhutJ8C59L4Z/vV6Vv15PbOjiFRunB/rOcYgIz1jEPek
'nnh+rBPsiYbOaRu/DipzKrqkqNOJdgEIF3mNVLGa7jM9YSReg+t6U/UvFTYqmn13gGeUr9C1
'UL85RLCVGVTHNGEO2xGIDNT3PRR3VBUYHJAJQXXRHXTTSLFSRXOE8AZWDLNAUKRVGMUX9P
'KnG0y9Wjv+71IPF8JEMIZEXEP9ZHDKVO0Z6XOXMLF1APTMIPR38R5QD8ZAA7GC76JAML+ZwR4
'N0vdn6hRf89+ZwRIXZy/E473BKS9SD9UTERRVMBKP4END6CVLFRHT2N9MXTN9B3PTZFICO5
'4Ip9mGd1ud8bUriS3Oh6RuC318GofwHqKhl/NN0DHQAAABJRU5ERKJGG==
这是一个不使用ADODB对象的解码示例
option explicit
dim inobj,outobj,infile,myname,state,rec,outfile,content,table(256),bits,c,x,outword
state = 0
const r64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
myname = wscript.scriptfullname
set inobj = createobject("Scripting.FileSystemObject")
set outobj = createobject("Scripting.FileSystemObject")
set infile = inobj.opentextfile(myname,1)
set outfile = outobj.createtextfile("q.png")
for x = 1 to 256 step 1
table(x) = -1
next
for x = 1 to 64 step 1
table(1+asc(mid(r64,x,1))) = x - 1
next
bits = 0
do until(infile.atendofstream)
dim size
rec = infile.readline
if (state = 1) then
content = mid(rec,2)
size = len(content)
for x = 1 to size step 1
c = table(1+asc(mid(content,x,1)))
if (c <> -1) then
if (bits = 0) then
outword = c*4
bits = 6
elseif (bits = 2) then
outword = c+outword
outfile.write(chr(clng("&H" & hex(outword mod 256))))
bits = 0
elseif (bits = 4) then
outword = outword + int(c/4)
outfile.write(chr(clng("&H" & hex(outword mod 256))))
outword = c*64
bits = 2
else
outword = outword + int(c/16)
outfile.write(chr(clng("&H" & hex(outword mod 256))))
outword = c*16
bits = 4
end if
end if
next
end if
if (rec = "'PAYLOAD") then
state = 1
end if
loop
infile.close
outfile.close
wscript.echo "q.png created"
wscript.quit
'PAYLOAD
'iVBORw0KGgoAAAANSUhEUgAAAD4AAAA+CAIAAAD8oz8TAAABoklEQVRo3u2awQrDMAxDl7H/
'/+Xu0EsgSDw7hRF7vWywpO0UW5acjOu6Xmde79ex1+f+GGPACfcqzePXdVvvts7iv6rx56Ou
'8FNYkgyZx9xzZ3TVHfg7VEHdR+o6ZsWV54O/yDvUQj2KzYyH5wof5f14fR97xdPrmjy1ArVQ
'55yteMYzEqma5B2qoM5VBK+OuXUrHutjJ8c59l4z/vV6Vv15PbOjiFRunB/rOcYgIz1jEPek
'nnh+rBPsiYbOaRu/DipzKrqkqNOJdgEIF3mNVLGa7jM9YSReg+t6U/UvFTYqmn13gGeUr9C1
'ul85rlCVgVTHnGeo2xGIdnT3PRR3vbUYhjAJqXxRHxTtslfsrxOe8aziWdlnAukRVPGmuX9P
'KnG0y9Wjv+71IPf8JEMIZxeP9ZHDkvO0z6XoXmlF1APTMIpR38R5qd8ZAa7gc76JaMl+ZwR4
'N0vdn6hRf89+ZwRIXZy/e473bks9sd9uterERvmbKP4end6cVlFRHt2n9mxTN9b3PTzfIco5
'4Ip9mGd1ud8bUriS3Oh6RuC318GofwHqKhl/Nn0DHQAAAABJRU5ErkJggg==
选项显式
dim inobj、outobj、INFLE、myname、state、rec、outfile、content、table(256)、bits、c、x、outword
状态=0
const r64=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz012456789+/”
myname=wscript.scriptfullname
set inobj=createobject(“Scripting.FileSystemObject”)
set-outobj=createobject(“Scripting.FileSystemObject”)
set infle=inobj.opentextfile(myname,1)
set outfile=outobj.createtextfile(“q.png”)
对于x=1到256,步骤1
表(x)=-1
下一个
对于x=1到64,步骤1
表(1+asc(mid(r64,x,1))=x-1
下一个
位=0
直到(填充atendofstream)
暗淡的尺寸
rec=infle.readline
如果(状态=1),则
内容=中间(记录,2)
大小=长度(内容)
对于x=1,调整步骤1的大小
c=表(1+asc(mid(内容,x,1)))
如果(c-1)那么
如果(位=0),则
向外=c*4
Function Base64Encode(sText)
Set oNode = CreateObject("Msxml2.DOMDocument.3.0").CreateElement("base64")
oNode.dataType = "bin.base64"
oNode.nodeTypedValue =Stream_StringToBinary(sText)
Base64Encode = oNode.text
Set oNode = Nothing
End Function
Function Base64Decode(ByVal vCode)
Set oNode = CreateObject("Msxml2.DOMDocument.3.0").CreateElement("base64")
oNode.dataType = "bin.base64"
oNode.text = vCode
Base64Decode = Stream_BinaryToString(oNode.nodeTypedValue)
Set oNode = Nothing
End Function
Function Stream_StringToBinary(Text)
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = 2
' All Format => utf-16le - utf-8 - utf-16le
BinaryStream.CharSet = "us-ascii"
BinaryStream.Open
BinaryStream.WriteText Text
BinaryStream.Position = 0
BinaryStream.Type = 1
BinaryStream.Position = 0
Stream_StringToBinary = BinaryStream.Read
Set BinaryStream = Nothing
End Function
Function Stream_BinaryToString(Binary)
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = 1
BinaryStream.Open
BinaryStream.Write Binary
BinaryStream.Position = 0
BinaryStream.Type = 2
' All Format => utf-16le - utf-8 - utf-16le
BinaryStream.CharSet = "utf-8"
Stream_BinaryToString = BinaryStream.ReadText
Set BinaryStream = Nothing
End Function
''''''''''''''''''''''''''''''''''''''''''''''Testing'''''''''''''''''''''''''''''''''''''''''
arr=array("Hello","&Welcome","To My Program")
For Each Endcode In arr
WSH.Echo Base64Encode(Endcode)
Next
arr=array("2LPZhNin2YU==","R29vZA==","QnkhIQ==")
For Each Decode In arr
WSH.Echo Base64Decode(Decode)
Next