Created
March 7, 2012 02:34
-
-
Save wangye/1990522 to your computer and use it in GitHub Desktop.
VBScript Base64 encode and decode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' | |
' Description: Base64 encode and decode | |
' Author: wangye <pcn88 at hotmail dot com> | |
' Website: http://wangye.org | |
' | |
Class CBase64 | |
Private objXmlDom | |
Private objXmlNode | |
' GetObjectParam() 这个函数实现参考了开源项目PJBlog | |
Private Function GetObjectParam() | |
On Error Resume Next | |
Dim Temp | |
GetObjectParam = "Microsoft.XMLDOM" | |
Err = 0 | |
Dim TmpObj | |
Set TmpObj = Server.CreateObject(GetObjectParam) | |
Temp = Err.Number | |
If Temp = 1 Or Temp = -2147221005 Then | |
GetObjectParam = "Msxml2.DOMDocument.5.0" | |
End If | |
Err.Clear | |
Set TmpObj = Nothing | |
Err = 0 | |
End Function | |
Private Sub Class_Initialize() | |
Set objXmlDom = Server.CreateObject(GetObjectParam()) | |
End Sub | |
Private Sub Class_Terminate() | |
Set objXmlDom = Nothing | |
End Sub | |
Public Function encode(AnsiCode) | |
encode = "" | |
Set objXmlNode = objXmlDom.createElement("tmp") | |
objXmlNode.datatype = "bin.base64" | |
objXmlNode.nodeTypedvalue = AnsiCode | |
encode = objXmlNode.Text | |
Set objXmlNode = Nothing | |
End Function | |
Public Function decode(base64Code) | |
On Error Resume Next | |
decode = "" | |
Set objXmlNode = objXmlDom.createElement("tmp") | |
objXmlNode.datatype = "bin.base64" | |
objXmlNode.Text = base64Code | |
decode = objXmlNode.nodeTypedvalue | |
Set objXmlNode = Nothing | |
If Err Then | |
Err.Clear | |
End If | |
End Function | |
' 以下函数编码字符串 | |
Public Function encodeText(ByVal str) | |
On Error Resume Next | |
Dim ado, r: r = "" | |
If str <> "" Then | |
Set ado = Server.CreateObject("ADODB.Stream") | |
With ado | |
.Charset = "gb2312" | |
.Type = 2 | |
If .State = 0 Then .Open | |
.WriteText str | |
.Position = 0 | |
.Type = 1 | |
r = encode(.Read(-1)) | |
.Close | |
End With | |
Set ado = Nothing | |
End If | |
If Err Then Err.Clear: r = "" | |
encodeText = r | |
End Function | |
' 以下函数解码字符串 | |
Public Function decodeText(ByVal str) | |
On Error Resume Next | |
Dim ado, r: r = "" | |
If str <> "" Then | |
Set ado = Server.CreateObject("ADODB.Stream") | |
With ado | |
.Charset = "gb2312" | |
.Type = 1 | |
If .State = 0 Then .Open | |
.Write (decode(str)) | |
.Position = 0 | |
.Type = 2 | |
r = .ReadText(-1) | |
.Close | |
End With | |
Set ado = Nothing | |
End If | |
If Err Then Err.Clear: r = "" | |
decodeText = r | |
End Function | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment