-
用于Asp的base64编码函数encode64/解码函数decode64 - [学习资料]
2010-11-11
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://zhanz.blogbus.com/logs/83571632.html
网上很多关于base64编/解码的代码,找到的这个是最好的了,有个不尽人意的地方是--编/解码调用一堆函数,我给增加了2个函数用来编/解码,调用方法:
将第二段代码保存到base64.inc,在需要编码的页面将base64.inc包含进来,调用相应函数即可,例如<!--#include file="base64.inc"-->
<%
'---------------------------------------------------------------------------------
str="我爱你tandy"
encodestr=base64Encode(str) '编码
response.write encodestr & "<hr>"
response.write base64Decode(encodestr) & "<hr>" '解码
%>
代码段二:
<%Function base64Encode(sString)
If sString = "" Or IsNull(sString) Then
base64Encode = ""
Exit Function
End If
Dim xml_dom, Node
Set xml_dom = Server.CreateObject("Microsoft.XMLDOM")With xml_dom
.loadXML ("<?xml version='1.0' ?> <root/>")
Set Node = xml_dom.createElement("MyText")
With Node
.dataType = "bin.base64"
.nodeTypedValue = Gb2312_Stream(sString)
base64Encode = .Text
End With
xml_dom.documentElement.appendChild Node
End With
Set xml_dom = Nothing
End FunctionFunction base64Decode(sString, filename)
If sString = "" Or IsNull(sString) Then
base64Decode = ""
Exit Function
End If
Dim xml_dom, Node
Set xml_dom = Server.CreateObject("Microsoft.XMLDOM")
With xml_dom
.loadXML ("<?xml version='1.0' ?> <root/>")
Set Node = xml_dom.createElement("MyText")
With Node
.dataType = "bin.base64"
.Text = sString
base64Decode = Stream_GB2312(.nodeTypedValue, filename)
End With
xml_dom.documentElement.appendChild Node
End With
Set xml_dom = Nothing
End FunctionFunction Gb2312_Stream(sString)
Dim dr
Set dr = CreateObject("ADODB.Stream")
With dr
.Mode = 3
.Type = 2
.open
.Charset = "gb2312"
.WriteText sString
.position = 0
.Type = 1
Gb2312_Stream = .Read
.Close
End With
Set dr = Nothing
End FunctionFunction Stream_GB2312(sStream, filename)
Dim dr
Set dr = CreateObject("ADODB.Stream")
With dr
.Mode = 3
.Type = 1
.open
.Write sStream
.SaveToFile Server.MapPath(filename),2
.position = 0
.Type = 2
.Charset = "gb2312"
Stream_GB2312 = .ReadText
.Close
End With
Set dr = Nothing
End Function%>







