ASP BASE64 跨防火墙
2019獨角獸企業重金招聘Python工程師標準>>>
Base64編碼的作用:由于某些系統中只能使用ASCII字符。Base64就是用來將非ASCII字符的數據轉換成ASCII字符的一種方法。它使用下面表中所使用的字符與編碼。 使用Base64的起因:因為安全問題,服務器安裝了360防火墻或者安全狗。導致經常把編輯器的內容給攔截了,導致客服N多的意見,所以才想到了使用加密的方法,跨過安全狗和防火墻。
asp是個很舊的語言,因此不想php那樣有封裝好的base64函數。因此我們百度了一個別人寫的函數。
<% Dim sBASE_64_CHARACTERS sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" sBASE_64_CHARACTERS = strUnicode2Ansi(sBASE_64_CHARACTERS) Function strUnicodeLen(asContents)asContents1 = "a"&asContentslen1 = Len(asContents1)k = 0For i = 1 To len1asc1 = Asc(Mid(asContents1, i, 1))If asc1<0 Then asc1 = 65536 + asc1If asc1>255 Thenk = k + 2Elsek = k + 1End IfNextstrUnicodeLen = k -1 End FunctionFunction strUnicode2Ansi(asContents)Dim len1,varasc,varHex,varlow,varhigh,varchar,istrUnicode2Ansi = ""len1 = Len(asContents)For i = 1 To len1varchar = Mid(asContents, i, 1)varasc = Asc(varchar)If varasc<0 Then varasc = varasc + 65536If varasc>255 ThenvarHex = Hex(varasc)varlow = Left(varHex, 2)varhigh = Right(varHex, 2)strUnicode2Ansi = strUnicode2Ansi & chrb("&H" & varlow ) & chrb("&H" & varhigh )ElsestrUnicode2Ansi = strUnicode2Ansi & chrb(varasc)End IfNext End FunctionFunction strAnsi2Unicode(asContents)Dim len1,i,varasc,varcharstrAnsi2Unicode = ""len1 = lenb(asContents)If len1 = 0 Then Exit FunctionFor i = 1 To len1varchar = midb(asContents, i, 1)varasc = ascb(varchar)If varasc > 127 ThenstrAnsi2Unicode = strAnsi2Unicode & Chr(ascw(midb(asContents, i + 1, 1) & varchar))i = i + 1ElsestrAnsi2Unicode = strAnsi2Unicode & Chr(varasc)End IfNext End FunctionFunction Base64encode(asContents)Dim lnPositionDim lsResultDim Char1,Char2,Char3,Char4Dim Byte1,Byte2,Byte3Dim SaveBits1,SaveBits2Dim lsGroupBinaryDim lsGroup64Dim m3,m4, len1, len2len1 = Lenb(asContents)If len1<1 ThenBase64encode = ""Exit FunctionEnd Ifm3 = Len1 Mod 3If M3 > 0 Then asContents = asContents & String(3 - M3, chrb(0))If m3 > 0 Thenlen1 = len1 + (3 - m3)len2 = len1 -3Elselen2 = len1End IflsResult = ""For lnPosition = 1 To len2 Step 3lsGroup64 = ""lsGroupBinary = Midb(asContents, lnPosition, 3)Byte1 = Ascb(Midb(lsGroupBinary, 1, 1))SaveBits1 = Byte1 And 3Byte2 = Ascb(Midb(lsGroupBinary, 2, 1))SaveBits2 = Byte2 And 15Byte3 = Ascb(Midb(lsGroupBinary, 3, 1))Char1 = Midb(sBASE_64_CHARACTERS, ((Byte1 And 252) \ 4) + 1, 1)Char2 = Midb(sBASE_64_CHARACTERS, (((Byte2 And 240) \ 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)Char3 = Midb(sBASE_64_CHARACTERS, (((Byte3 And 192) \ 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)Char4 = Midb(sBASE_64_CHARACTERS, (Byte3 And 63) + 1, 1)lsGroup64 = Char1 & Char2 & Char3 & Char4lsResult = lsResult & lsGroup64NextIf M3 > 0 ThenlsGroup64 = ""lsGroupBinary = Midb(asContents, len2 + 1, 3)Byte1 = Ascb(Midb(lsGroupBinary, 1, 1))SaveBits1 = Byte1 And 3Byte2 = Ascb(Midb(lsGroupBinary, 2, 1))SaveBits2 = Byte2 And 15Byte3 = Ascb(Midb(lsGroupBinary, 3, 1))Char1 = Midb(sBASE_64_CHARACTERS, ((Byte1 And 252) \ 4) + 1, 1)Char2 = Midb(sBASE_64_CHARACTERS, (((Byte2 And 240) \ 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)Char3 = Midb(sBASE_64_CHARACTERS, (((Byte3 And 192) \ 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)If M3 = 1 ThenlsGroup64 = Char1 & Char2 & ChrB(61) & ChrB(61) '用=號補足位數ElselsGroup64 = Char1 & Char2 & Char3 & ChrB(61) '用=號補足位數End IflsResult = lsResult & lsGroup64End IfBase64encode = lsResult End Function'將Base64編碼字符串轉換成Ansi編碼的字符串 Function Base64decode(asContents)Dim lsResultDim lnPositionDim lsGroup64, lsGroupBinaryDim Char1, Char2, Char3, Char4Dim Byte1, Byte2, Byte3Dim M4, len1, len2len1 = Lenb(asContents)M4 = len1 Mod 4If len1 < 1 Or M4 > 0 ThenBase64decode = ""Exit FunctionEnd IfIf midb(asContents, len1, 1) = chrb(61) Then m4 = 3If midb(asContents, len1 -1, 1) = chrb(61) Then m4 = 2If m4 = 0 Thenlen2 = len1Elselen2 = len1 -4End IfFor lnPosition = 1 To Len2 Step 4lsGroupBinary = ""lsGroup64 = Midb(asContents, lnPosition, 4)Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 1, 1)) - 1Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 2, 1)) - 1Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 3, 1)) - 1Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 4, 1)) - 1Byte1 = Chrb(((Char2 And 48) \ 16) Or (Char1 * 4) And &HFF)Byte2 = lsGroupBinary & Chrb(((Char3 And 60) \ 4) Or (Char2 * 16) And &HFF)Byte3 = Chrb((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))lsGroupBinary = Byte1 & Byte2 & Byte3lsResult = lsResult & lsGroupBinaryNextIf M4 > 0 ThenlsGroupBinary = ""lsGroup64 = Midb(asContents, len2 + 1, m4) & chrB(65) 'chr(65)=A,轉換成值為0If M4 = 2 Then '補足4位,是為了便于計算lsGroup64 = lsGroup64 & chrB(65)End IfChar1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 1, 1)) - 1Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 2, 1)) - 1Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 3, 1)) - 1Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 4, 1)) - 1Byte1 = Chrb(((Char2 And 48) \ 16) Or (Char1 * 4) And &HFF)Byte2 = lsGroupBinary & Chrb(((Char3 And 60) \ 4) Or (Char2 * 16) And &HFF)Byte3 = Chrb((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))If M4 = 2 ThenlsGroupBinary = Byte1ElseIf M4 = 3 ThenlsGroupBinary = Byte1 & Byte2End IflsResult = lsResult & lsGroupBinaryEnd IfBase64decode = lsResultEnd FunctionFunction Encode64(byval Str)Encode64 = strAnsi2Unicode(Base64encode(strUnicode2Ansi(Str))) End FunctionFunction Decode64(byval Str)Decode64 = strAnsi2Unicode(Base64decode(strUnicode2Ansi(Str))) End Functionfunction CodeConvert(fileContent)dim stmset stm=Server.CreateObject("adodb.stream") stm.Type=2stm.Mode=3stm.Charset="GB2312"stm.Open stm.WriteText fileContentstm.Position=0stm.Charset="UTF-8"CodeConvert = stm.ReadTextstm.Close set stm=nothing End function%>以上這段代碼就是別人封裝好的asp的base64加密解密方法?,F在我們還需要一個前端的js的base64加密函數。這個百度就有,很多。尤其是jq,有一個封裝的不錯的,下載后直接調用就好。
現在來說說構思,首先我們用js將百度編輯器的內容加密為base64,這里面包含了文章和圖片,提交后,就跨過了防火墻。然后,由asp接收base64加密的字符串內容,然后調用asp的解碼函數。將正確的內容插入數據庫里面
這里有個很腦殘的問題(解碼亂碼)!!!開始的時候,我以為是js的加密和asp的解密方法對不上。換了N多個的測試。最后發現,原來是編碼的問題!!! http://www.mytju.com/classCode/tools/messyCodeRecover.as 在這個網址里面,找到了核心的原因。
最后附上部分代碼圖
轉載于:https://my.oschina.net/u/2413228/blog/811169
總結
以上是生活随笔為你收集整理的ASP BASE64 跨防火墙的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vscode使用汇总——常用插件、常用配
- 下一篇: [I2C]I2C总线协议图解