1. 程式人生 > >基於VBS、ASP環境下的BASE64 Encode 和 Decode

基於VBS、ASP環境下的BASE64 Encode 和 Decode

網路上有各種Base64編碼、解碼的例子原始碼,編碼後雖然也可以通過大部分解碼程式解碼,但編碼後的結果卻各有不同。

轉載請說明來源於:http://blog.csdn.net/aminfo/article/details/70478053

由於工作需要,在呼叫某API時,網路上的Base64編碼結果不符合要求,於是自寫一個基於VBS的BASE64 Encode和Decode,已經過初次測試。程式碼如下供參考:

<%'基於VBS、ASP的Base64 Encode 和 Base64 Decode
Dim sc
Set sc = CreateObject("MSScriptControl.ScriptControl")
sc.Language = "JScript"

Dim tmpStr, tmpResult
tmpStr = "··ASCII第···一次以規範標準的型態發表是在1967年,1234567890-=`~!·#¥%……—*()——
[email protected]
#$%^&*()_+[]{};'\:""|,./<>?《》?:“|{},。/;‘、][ABCDEFGHIJKLMNOPQRSTUVWXYZ最後一次更新則是在1986年,至今為止共定義了128個字元,其中33個字元無法顯示(這是以現今作業系統為依歸,但在DOS模式··" tmpResult = vbsBase64Encode(StringToByteArray(tmpStr)) Response.write "編碼: " & tmpResult & "<BR>" tmpResult = ByteArrayToString(vbsBase64Decode(tmpResult)) Response.write "解碼:" & tmpResult & "<BR>" Response.write "原串:" & tmpStr & "<BR>" If tmpStr <> tmpResult Then Response.write "結果不同" Else Response.write "結果相同" End If '編碼 Function vbsBase64Encode(byteArray) Dim last2byte : last2byte = 3 Dim last4byte : last4byte = 15 Dim last6byte : last6byte = 63 Dim lead6byte : lead6byte = 252 Dim lead4byte : lead4byte = 240 Dim lead2byte : lead2byte = 192 Dim encodeTable : encodeTable = Split("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,+,/", ",") Dim objectStr : objectStr = "" Dim num num = 0 Dim currentByte currentByte = 0 Dim i For i = 0 to ubound(byteArray) num = num mod 8 do while(num < 8) Select Case num Case 0 currentByte = byteArray(i) and lead6byte currentByte = uRShift(currentByte, 2) Case 2 currentByte = byteArray(i) and last6byte Case 4 currentByte = byteArray(i) and last4byte currentByte = LShift(currentByte, 2) If ((i + 1) <= ubound(byteArray)) Then currentByte = currentByte or uRShift(byteArray(i + 1) and lead2byte, 6) End If Case 6 currentByte = byteArray(i) and last2byte currentByte = LShift(currentByte, 4) If ((i + 1) <= ubound(byteArray)) Then currentByte = currentByte or uRShift(byteArray(i + 1) and lead4byte, 4) End If End Select objectStr = objectStr & encodeTable(currentByte) num = num + 6 Loop Next If (Len(objectStr) mod 4 <> 0) Then For i = (4 - Len(objectStr) mod 4) to 1 step - 1 objectStr = objectStr & "=" Next End If vbsBase64Encode = objectStr End Function '轉載請說明來源於:http://blog.csdn.net/aminfo/article/details/70478053 '解碼 Function vbsBase64Decode(str) Dim delta Dim ALPHABET : ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" If Right(str, 2) = "==" Then delta = 2 ElseIf Right(str, 1) = "=" Then delta = 1 Else delta = 0 End If Redim buffer(Len(str) * 3 / 4 - delta) Dim mask : mask = &HFF Dim index : index =0 Dim i, c0, c1, c2, c3 For i = 1 to Len(str) step 4 c0 = Instr(ALPHABET, mid(str, i, 1)) - 1 c1 = Instr(ALPHABET, mid(str, i + 1, 1)) - 1 buffer(index) = (LShift(c0, 2) or RShift(c1, 4)) and mask If buffer(index)>127 Then buffer(index) = buffer(index) - 256 End If index = index + 1 If(index >= ubound(buffer)) Then Exit For End If c2 = Instr(ALPHABET, mid(str, i + 2, 1)) - 1 buffer(index) = (LShift(c1, 4) or RShift(c2, 2)) and mask If buffer(index)>127 Then buffer(index) = buffer(index) - 256 End If index = index + 1 If(index >= ubound(buffer)) Then Exit For End If c3 = Instr(ALPHABET, mid(str, i + 3, 1)) - 1 buffer(index) = (LShift(c2, 6) or c3) and mask If buffer(index)>127 Then buffer(index) = buffer(index) - 256 End If index = index + 1 Next vbsBase64Decode = buffer End Function Function LShift(Value, Shift) LShift = sc.Eval(Value & "<<" & Shift) End Function Function RShift(Value, Shift) RShift = sc.Eval(Value & ">>" & Shift) End Function Function uLShift(Value, Shift) uLShift = sc.Eval(Value & "<<<" & Shift) End Function Function uRShift(Value, Shift) uRShift = sc.Eval(Value & ">>>" & Shift) End Function '轉載請說明來源於:http://blog.csdn.net/aminfo/article/details/70478053 '------------------------------------------ Function ArrayAdd(byteArray1, byteArray2) Dim i, tmpStr tmpStr = "" For i = 0 to ubound(byteArray1) If i>0 Then tmpStr = tmpStr & "," End If tmpStr = tmpStr & byteArray1(i) Next For i = 0 to ubound(byteArray2) tmpStr = tmpStr & "," & byteArray2(i) Next ArrayAdd = Split(tmpStr, ",") End Function 'Bin字串轉陣列 Function BinToByteArray(szInput) Dim i, byteArray, wch, nAsc byteArray = "" For i=1 To Len(szInput) wch = Mid(szInput, i, 1) nAsc = AscW(wch) 'Response.write "<BR>wch = " & nAsc If nAsc>127 Then byteArray = byteArray & "," & (nAsc - 256) Else byteArray = byteArray & "," & nAsc End If Next If Left(byteArray, 1) = "," Then byteArray = Right(byteArray, Len(byteArray) - 1) End If BinToByteArray = Split(byteArray, ",") End Function '字串轉陣列 Function StringToByteArray(szInput) Dim i, byteArray, wch, nAsc byteArray = "" For i=1 To Len(szInput) wch = Mid(szInput, i, 1) nAsc = AscW(wch) If nAsc < 0 Then nAsc = nAsc + 65536 End If If (nAsc And &HFF80) = 0 Then byteArray = byteArray & "," & AscW(wch) Else If (nAsc And &HF000) = 0 Then byteArray = byteArray & "," & Cint("&H" & Hex(((nAsc \ 2 ^ 6)) Or &HC0)) - 256 & "," & Cint("&H" & Hex(nAsc And &H3F Or &H80))-256 Else byteArray = byteArray & "," & Cint("&H" & Hex((nAsc \ 2 ^ 12) Or &HE0)) - 256 & "," & Cint("&H" & Hex((nAsc \ 2 ^ 6) And &H3F Or &H80)) - 256 & "," & Cint("&H" & Hex(nAsc And &H3F Or &H80)) - 256 End If End If Next If Left(byteArray, 1) = "," Then byteArray = Right(byteArray, Len(byteArray) - 1) End If StringToByteArray = Split(byteArray, ",") End Function '轉載請說明來源於:http://blog.csdn.net/aminfo/article/details/70478053 '陣列轉字串 Function ByteArrayToString(sArray) Dim i, tStr, byte1, byte2, byte3 tStr = "" For i = 0 to ubound(sArray) If sArray(i)>0 and sArray(i)<128 Then tStr = tStr & Chr(sArray(i)) Else If i < ubound(sArray) - 1 Then byte1 = ((sArray(i) + 256) And &H3F) * &H40 If byte1<2048 Then byte1 = ((sArray(i) + 256) And &H3F) * &H40 byte2 = (sArray(i + 1) + 256) And &H3F tStr = tStr & chrW(byte1 or byte2) i = i + 1 Else byte1 = ((sArray(i) + 256) And &H0F) * &H1000 byte2 = ((sArray(i + 1) + 256) And &H3F) * &H40 byte3 = (sArray(i + 2) + 256) And &H3F tStr = tStr & chrW(byte1 or byte2 or byte3) i = i + 2 End If End If End If Next ByteArrayToString = tStr End Function%>


相關推薦

基於VBSASP環境BASE64 Encode Decode

網路上有各種Base64編碼、解碼的例子原始碼,編碼後雖然也可以通過大部分解碼程式解碼,但編碼後的結果卻各有不同。 轉載請說明來源於:http://blog.csdn.net/aminfo/article/details/70478053 由於工作需要,在呼叫某API時,網

資料探勘機器學習之Ubantu14.04Centos7環境安裝LightGBM

xgboost的出現,讓大資料分析民工們告別了傳統的機器學習演算法們:RF、GBM、SVM、LASSO........。現在,微軟推出了一個新的boosting框架,想要挑戰xgboost的江湖地

EclipseIDEA環境設定jvm預設編碼

轉載: https://blog.csdn.net/u014424628/article/details/49429393這這有個小程式,大家可以在自己機器上跑一下:import java.nio.charset.Charset; public class Encoding

win7win10環境的maven的環境變數配置

(一)win7   maven環境變數配置 第一步:http://maven.apache.org/download.cgi官網下載 第二步:把壓縮包解壓縮到不含中文和空格的目錄下 第三步:新建MA

Linux AIX環境檢視oracle配置資訊(service_nameSIDtnsname)。

SID: echo $ORACLE_SID service_name: sqlplus / as sysdba; show parameter instance_name;   show parameter service_names;   show parameter service; select

再談應用環境的 TIME_WAIT CLOSE_WAIT

ech 防範 生效 場景 closed 防止 減少 進入 top 轉自:http://blog.csdn.net/shootyou/article/details/6622226 昨天解決了一個HttpClient調用錯誤導致的服務器異常,具體過程如下: http://

springboot環境配置過濾器攔截器

name public spring doc true 直接 war pattern long 以前我們在配置過濾器和攔截器的時候,都是一個類繼承一個接口,然後在xml中配置一下就ok 但是,但是,這是springboot的環境,沒有xml的配置。所以我們還要繼續學習啊啊

windows環境zookeeper安裝使用

retain ID sync zookeepe hat net can which ble 一.簡介 zooKeeper是一個分布式的,開放源碼的分布式應用程序協調服務,是Google的Chubby一個開源的實現,是Hadoop和Hbase的重要組件。它是一個

Ubuntu環境安裝nodejsnpm

mir proxy 上下 oba 及其 安裝軟件包 比較 number 需要 Node.js 是一個基於 Chrome V8 引擎的 JavaScript 運行環境,其使用了一個事件驅動、非阻塞式 I/O 的模型,使其輕量又高效。 Node.js 的包管理器 npm,是全球

Python 爬蟲框架 Scrapy 在 Windows10 系統環境的安裝配置

環境: Windows10 系統、python3.4.3 版本 安裝 Scrapy : 使用 pip 安裝 Scrapy,在命令列視窗中輸入命令 “pip install Scrapy”,若不報錯即可安裝成功。 注意:可在命令列視窗中輸入命令“pip -h”測試 pip 模組是否已安

PHP7.x環境安裝memcachememcached擴充套件

504 0 1 如果使用php操作memcached伺服器最方便的方式就是安裝memcache擴充套件了,其實php還有另外一個擴充套件那就是memcached,和memcached服務同名,並且擴充套件提供的方法更便於使用,下面分別介紹一下兩種擴充套件在PHP7環境下的安裝

windows環境curl 安裝使用

原文:https://blog.csdn.net/qq_21126979/article/details/78690960?locationNum=10&fps=1 一、curl 安裝 curl下載地址:https://curl.haxx.se/download.html,如下圖所示: &nbs

docker環境的zookeeperkafka部署

kafka簡單介紹 Kafka 是 LinkedIn 開源的一種高吞吐量的分散式釋出訂閱訊息系統,kafka的誕生就是為了處理海量日誌資料,所以kafka處理訊息的效率非常高,即使是非常普通的硬體也可以支援每秒數百萬的訊息。kafka 天然支援叢集負載均衡,使用 zookeeper 進行分散式協

Linux環境如何編譯執行c程式

1 單個檔案的編譯和執行 建立main.c檔案,內容如下:  編譯: 執行: 2 多個檔案的編譯和執行建立sum.c檔案,內容如下: 建立main.c檔案,內容如下:   編譯:    生成可執行檔案,檔名為main: 執行程式:

Linux環境如何編譯執行c程序

文件的 ron 當前 cto directory 變量 沒有 執行文件 nbsp 1 單個文件的編譯和執行 創建main.c文件,內容如下: 編譯: 執行: 2 多個文件的編譯和執行創建sum.c文件,內容如下: 創建main.c文件,內容如下: 編譯:

微信小程式 BASE64 encode 加密 decode 解密

base64.js檔案 var Base64 = { // private property _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", // public method

Redis在linux環境的安裝部署

官網:http://redis.io          windows版本下載地址https://github.com/MicrosoftArchive/redis/releases   1Redis建議安

window環境python3.xpython2.x共存以及安裝tensorflow

背景介紹: 專案需要跑一些python2.7寫的程式碼,目前有的機器是win10環境,python3.5,tensorflow-gpu1.10.0,CUDA9.0。一開始想的就是在現有基礎上,直接安裝python2.7和tensorflow。 結論: Windows環境

Windows10環境安裝Anacondatensorflow-gpu,然後在jupyter notebook上使用

  一、進行Anaconda的下載和安裝 在官方網站下載Anaconda的Windows版本,下載的網址是https://www.anaconda.com/download/,根據自己電腦的位數下載對應的客戶端,推薦下載Python 3.7 version *版本。

Windows環境hadoop安裝配置

1.下載Hadoop http://www.apache.org/dyn/closer.cgi/hadoop/common 2.解壓 hadoop-2.7.3.tar.gz 點右鍵“解壓到hadoop-2.7.3” 資料夾路徑 xxx/xxxx/hadoop-2.7.3 複製