VBScript 中的 Base64 编码字符串 [英] Base64 Encode String in VBScript

查看:24
本文介绍了VBScript 中的 Base64 编码字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 服务加载驱动程序,它是一个 Windows 脚本文件 (WSF),其中包括一些 VBScript 和 JavaScript 文件.我的 Web 服务要求传入消息采用 base64 编码.我目前有一个执行此操作的 VBScript 函数,但它的效率非常低(内存密集,主要是由于 VBScript 糟糕的字符串连接)

[旁白;是的,我看过 Jeff 的最新博文.串联发生在大小为 1,000 到 10,000 字节的消息之间的循环中.]

我尝试过使用一些自定义字符串连接例程;一种使用数组,一种使用 ADODB.Stream.这些有一点帮助,但我认为如果我有一些其他方式来编码消息而不是通过我自己的 VBS 函数会更有帮助.

是否有其他方法可以对我的消息进行编码,最好是使用本机 Windows 方法?

解决方案

我最初使用 Antonin Foller 的一些 VBScript 代码:Base64 编码 VBS 函数Base64 解码 VBS 函数.

搜索 Antonin 的网站,我看到他有一些用于 引用的可打印编码的代码,使用CDO.Message 对象,所以我试过了.

最后,我将 Mark 的回答中提到的代码移植到 VBScript(也使用了来自 this SO 问题),并使用了 Stream___StringToBinaryStream_BinaryToString 函数来自 Antonin 的站点以获取使用 MSXML 编码的函数.

我进行了一个快速测试,以测量所有四种方法中 1,500 个字符的消息(我需要发送到我的网络服务的平均消息大小)的编码时间:

  • 本机 VBScript (VBScript)
  • 引用可打印,使用 CDO.Message (QP)
  • 引用可打印二进制文件,使用 CDO.Message(QP 二进制文件)
  • MSXML/ADODB.Stream (MSXML)

结果如下:

<前>迭代次数:10,000消息大小:1,500+------------+------------++ 方法 |时间(毫秒)++------------+------------+|VBScript |301,391 |+------------+------------+|QP |12,922 |+------------+------------+|QP(二进制) |13,953 |+------------+------------+|MSXML |3,312 |+------------+------------+

我还在测试运行时监控了内存利用率(Windows 任务管理器中 cscript.exe 进程的内存使用情况).我没有任何原始数字,但引用的可打印和 MSXML 解决方案的内存利用率低于 VBScript 解决方案(前者为 7,000K,VBScript 约为 16,000K).

我决定为我的驱动程序使用 MSXML 解决方案.对于那些感兴趣的人,这是我正在使用的代码:

base64.vbs函数 Base64Encode(sText)Dim oXML、oNodeSet oXML = CreateObject("Msxml2.DOMDocument.3.0")设置 oNode = oXML.CreateElement("base64")oNode.dataType = "bin.base64"oNode.nodeTypedValue =Stream_StringToBinary(sText)Base64Encode = oNode.text设置 oNode = 无设置 oXML = 无结束函数函数 Base64Decode(ByVal vCode)Dim oXML、oNodeSet oXML = CreateObject("Msxml2.DOMDocument.3.0")设置 oNode = oXML.CreateElement("base64")oNode.dataType = "bin.base64"oNode.text = vCodeBase64Decode = Stream_BinaryToString(oNode.nodeTypedValue)设置 oNode = 无设置 oXML = 无结束函数'Stream_StringToBinary 函数'2003 安东尼福勒,http://www.motobit.com'Text - 字符串参数转换为二进制数据函数 Stream_StringToBinary(Text)常量 adTypeText = 2常量 adTypeBinary = 1'创建流对象Dim BinaryStream '作为新流Set BinaryStream = CreateObject("ADODB.Stream")'指定流类型 - 我们要保存文本/字符串数据.BinaryStream.Type = adTypeText'为源文本(unicode)数据指定字符集.BinaryStream.CharSet = "us-ascii"'打开流并将文本/字符串数据写入对象BinaryStream.OpenBinaryStream.WriteText 文本'将流类型更改为二进制BinaryStream.Position = 0BinaryStream.Type = adTypeBinary'忽略前两个字节 - 的符号BinaryStream.Position = 0'打开流并从对象中获取二进制数据Stream_StringToBinary = BinaryStream.Read设置 BinaryStream = 无结束函数'Stream_BinaryToString 函数'2003 安东尼福勒,http://www.motobit.com'二进制 - VT_UI1 |VT_ARRAY 数据 转换为字符串函数 Stream_BinaryToString(Binary)常量 adTypeText = 2常量 adTypeBinary = 1'创建流对象Dim BinaryStream '作为新流Set BinaryStream = CreateObject("ADODB.Stream")'指定流类型 - 我们要保存二进制数据.BinaryStream.Type = adTypeBinary'打开流并将二进制数据写入对象BinaryStream.OpenBinaryStream.Write 二进制'将流类型更改为文本/字符串BinaryStream.Position = 0BinaryStream.Type = adTypeText'指定字符集用于输出文本(unicode)数据.BinaryStream.CharSet = "us-ascii"'打开流并从对象中获取文本/字符串数据Stream_BinaryToString = BinaryStream.ReadText设置 BinaryStream = 无结束函数

I have a web service load driver that's a Windows Script File (WSF), that includes some VBScript and JavaScript files. My web service requires that the incoming message is base64 encoded. I currently have a VBScript function that does this, but it's very inefficient (memory intensive, mostly due to VBScripts awful string concatenation)

[Aside; Yes, I've seen Jeff's latest blog post. The concatenation is happening in a loop across messages that are 1,000's to 10,000's bytes in size.]

I've tried using some custom string concatenation routines; one using an array and one using ADODB.Stream. These help, a little, but I think it would help more if I had some other way of encoding the message rather than via my own VBS function.

Is there some other way of encoding my message, preferebly using native Windows methods?

解决方案

I was originally using some VBScript code from Antonin Foller: Base64 Encode VBS Function and Base64 Decode VBS Function.

Searching Antonin's site, I saw he had some code for quoted printable encoding, using the CDO.Message object, so I tried that.

Finally, I ported the code mentioned in Mark's answer to VBScript (also used some code from this SO question), and used the Stream___StringToBinary and Stream_BinaryToString functions from Antonin's site to get functions that used MSXML encoding.

I ran a quick test to measure the encoding time for a 1,500 character message (the average message size I need to send to my web service) across all four methods:

  • Native VBScript (VBScript)
  • Quoted Printable, using CDO.Message (QP)
  • Quoted Printable Binary, using CDO.Message (QP Binary)
  • MSXML/ADODB.Stream (MSXML)

Here are the results:

Iterations   : 10,000
Message Size :  1,500

+-------------+-----------+
+ Method      | Time (ms) + 
+-------------+-----------+
| VBScript    |   301,391 |
+-------------+-----------+
| QP          |    12,922 |
+-------------+-----------+
| QP (Binary) |    13,953 |
+-------------+-----------+
| MSXML       |     3,312 |
+-------------+-----------+

I also monitored the memory utilization (Mem Usage for the cscript.exe process in the Windows Task Manager) while the test was running. I don't have any raw numbers, but the memory utilization for both the quoted printable and MSXML solutions were below the VBScript solution (7,000K for the former, around 16,000K for VBScript).

I decided to go with the MSXML solution for my driver. For those interested, here's the code I'm using:

base64.vbs
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

'Stream_StringToBinary Function
'2003 Antonin Foller, http://www.motobit.com
'Text - string parameter To convert To binary data
Function Stream_StringToBinary(Text)
  Const adTypeText = 2
  Const adTypeBinary = 1

  'Create Stream object
  Dim BinaryStream 'As New Stream
  Set BinaryStream = CreateObject("ADODB.Stream")

  'Specify stream type - we want To save text/string data.
  BinaryStream.Type = adTypeText

  'Specify charset For the source text (unicode) data.
  BinaryStream.CharSet = "us-ascii"

  'Open the stream And write text/string data To the object
  BinaryStream.Open
  BinaryStream.WriteText Text

  'Change stream type To binary
  BinaryStream.Position = 0
  BinaryStream.Type = adTypeBinary

  'Ignore first two bytes - sign of
  BinaryStream.Position = 0

  'Open the stream And get binary data from the object
  Stream_StringToBinary = BinaryStream.Read

  Set BinaryStream = Nothing
End Function

'Stream_BinaryToString Function
'2003 Antonin Foller, http://www.motobit.com
'Binary - VT_UI1 | VT_ARRAY data To convert To a string 
Function Stream_BinaryToString(Binary)
  Const adTypeText = 2
  Const adTypeBinary = 1

  'Create Stream object
  Dim BinaryStream 'As New Stream
  Set BinaryStream = CreateObject("ADODB.Stream")

  'Specify stream type - we want To save binary data.
  BinaryStream.Type = adTypeBinary

  'Open the stream And write binary data To the object
  BinaryStream.Open
  BinaryStream.Write Binary

  'Change stream type To text/string
  BinaryStream.Position = 0
  BinaryStream.Type = adTypeText

  'Specify charset For the output text (unicode) data.
  BinaryStream.CharSet = "us-ascii"

  'Open the stream And get text/string data from the object
  Stream_BinaryToString = BinaryStream.ReadText
  Set BinaryStream = Nothing
End Function

这篇关于VBScript 中的 Base64 编码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆