asp Base64 编码和解码

<%
' 2024年6月26日 14:54:17
' asp Base64 编码解码
' 使用方法:
' myString = "用户输入开灯指令,控制器判断开灯条件,符合条件,则开灯;不符合条件,则关灯。把自然语言写到注释中,c语言给个示例"
' encodedString = Base64Encode(myString)
' DecodeString = Base64Decode(encodedString)
' Response.Write "encodedString=======" &  encodedString & "<br>"
' Response.Write "DecodeString=======" &  DecodeString & "<br>"

' 使用 Split 函数按空格分隔字符串
' wordsArray = Split(encodedString, Mid(encodedString, 73, 1))
' encodedStringJoin = join(wordsArray,"")
' Response.Write "encodedStringJoin=======" &  encodedStringJoin & "<br>"
' 输出分隔后的数组内容
' Response.Write "分隔后的数组内容:<br>"
' For Each word In wordsArray
'     Response.Write "word===="& word & "<br>"
' Next


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
		.async = False
		.loadXML "<?xml version='1.0' ?><root />"
		Set Node = xml_dom.createElement("MyText")
		With Node
			.dataType = "bin.base64"
			.nodeTypedValue = Stream_StringToBinary(sString)
			NodeText = Node.text
			NodeText = Trim(NodeText)
			NodeText=Replace(NodeText,vbcrlf,"")
			NodeText=Replace(NodeText,vbtab,"")
			If InStr(NodeText, Chr(10)) > 0 Then
				NodeText=Replace(NodeText,Chr(10),"")
			End If
			Base64Encode = NodeText
		End With
		xml_dom.documentElement.appendChild Node
		
	End With
	set xml_dom = Nothing
    
End Function

function Stream_StringToBinary(sString)
	Dim Stream, BinaryStream
	Set Stream = Server.CreateObject("ADODB.Stream")
	Stream.Type = 2 ' adTypeText
	Stream.Charset = "utf-8"
	Stream.Open
	Stream.WriteText sString
	Stream.Position = 0
	Stream.Type = 1 ' adTypeBinary
	Stream.Position = 0
	Stream_StringToBinary = Stream.Read
	Stream.Close
	Set Stream = Nothing
End Function

Function Base64Decode(sString)
	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
		.async = False
		.loadXML "<?xml version='1.0' ?><root />"
		Set Node = xml_dom.createElement("MyText")
		With Node
			.dataType = "bin.base64"
			.text = Trim(sString)
			Base64Decode = Stream_BinaryToString(.nodeTypedValue)
		End With
		xml_dom.documentElement.appendChild Node
	End With
	set xml_dom = Nothing
End Function

function Stream_BinaryToString(Binary)
	Dim Stream
	Set Stream = Server.CreateObject("ADODB.Stream")
	Stream.Type = 1 ' adTypeBinary
	Stream.Open
	Stream.Write Binary
	Stream.Position = 0
	Stream.Type = 2 ' adTypeText
	Stream.Charset = "utf-8"
	Stream_BinaryToString = Stream.ReadText
	Stream.Close
	Set Stream = Nothing
End Function


%>

Base64 Encode
asp+base64

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值