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

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

问题描述

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

[旁白;是的,我看过 Jeff 的最新博文.连接发生在一个循环中,跨越 1,000 到 10,000 字节大小的消息.]

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

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

解决方案

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

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

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

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

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

结果如下:

<上一页>迭代次数:10,000消息大小:1,500+-------------+------------++ 方法 |时间(毫秒) ++-------------+------------+|VB脚本 |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)昏暗的oXML,oNode设置 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)昏暗的oXML,oNode设置 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'文本 - 字符串参数转换为二进制数据函数 Stream_StringToBinary(文本)常量 adTypeText = 2常量 adTypeBinary = 1'创建流对象Dim BinaryStream '作为新流设置 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(二进制)常量 adTypeText = 2常量 adTypeBinary = 1'创建流对象Dim BinaryStream '作为新流设置 BinaryStream = CreateObject("ADODB.Stream")'指定流类型 - 我们要保存二进制数据.BinaryStream.Type = adTypeBinary'打开流并将二进制数据写入对象BinaryStream.OpenBinaryStream.Write Binary'将流类型更改为文本/字符串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天全站免登陆