ASP BASE64 跨防火牆

Base64編碼的做用:因爲某些系統中只能使用ASCII字符。Base64就是用來將非ASCII字符的數據轉換成ASCII字符的一種方法。它使用下面表中所使用的字符與編碼。 使用Base64的原由:由於安全問題,服務器安裝了360防火牆或者安全狗。致使常常把編輯器的內容給攔截了,致使客服N多的意見,因此纔想到了使用加密的方法,跨過安全狗和防火牆。php

asp是個很舊的語言,所以不想php那樣有封裝好的base64函數。所以咱們百度了一個別人寫的函數。前端

<% 

Dim sBASE_64_CHARACTERS
sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
sBASE_64_CHARACTERS = strUnicode2Ansi(sBASE_64_CHARACTERS)
Function strUnicodeLen(asContents)
    asContents1 = "a"&asContents
    len1 = Len(asContents1)
    k = 0
    For i = 1 To len1
        asc1 = Asc(Mid(asContents1, i, 1))
        If asc1<0 Then asc1 = 65536 + asc1
        If asc1>255 Then
            k = k + 2
        Else
            k = k + 1
        End If
    Next
    strUnicodeLen = k -1
End Function

Function strUnicode2Ansi(asContents)
 Dim len1,varasc,varHex,varlow,varhigh,varchar,i
    strUnicode2Ansi = ""
    len1 = Len(asContents)
    For i = 1 To len1
        varchar = Mid(asContents, i, 1)
        varasc = Asc(varchar)
        If varasc<0 Then varasc = varasc + 65536
        If varasc>255 Then
            varHex = Hex(varasc)
            varlow = Left(varHex, 2)
            varhigh = Right(varHex, 2)
            strUnicode2Ansi = strUnicode2Ansi & chrb("&H" & varlow ) & chrb("&H" & varhigh )
        Else
            strUnicode2Ansi = strUnicode2Ansi & chrb(varasc)
        End If
    Next
End Function

Function strAnsi2Unicode(asContents)
 Dim len1,i,varasc,varchar
    strAnsi2Unicode = ""
    len1 = lenb(asContents)
    If len1 = 0 Then Exit Function
    For i = 1 To len1
        varchar = midb(asContents, i, 1)
        varasc = ascb(varchar)
        If varasc > 127 Then
            strAnsi2Unicode = strAnsi2Unicode & Chr(ascw(midb(asContents, i + 1, 1) & varchar))
            i = i + 1
        Else
            strAnsi2Unicode = strAnsi2Unicode & Chr(varasc)
        End If
    Next
End Function

Function Base64encode(asContents)
    Dim lnPosition
    Dim lsResult
    Dim Char1,Char2,Char3,Char4
    Dim Byte1,Byte2,Byte3
    Dim SaveBits1,SaveBits2
    Dim lsGroupBinary
    Dim lsGroup64
    Dim m3,m4, len1, len2

    len1 = Lenb(asContents)
    If len1<1 Then
        Base64encode = ""
        Exit Function
    End If

    m3 = Len1 Mod 3
    If M3 > 0 Then asContents = asContents & String(3 - M3, chrb(0))
    If m3 > 0 Then
        len1 = len1 + (3 - m3)
        len2 = len1 -3
    Else
        len2 = len1
    End If
    lsResult = ""
    For lnPosition = 1 To len2 Step 3
        lsGroup64 = ""
        lsGroupBinary = Midb(asContents, lnPosition, 3)

        Byte1 = Ascb(Midb(lsGroupBinary, 1, 1))
        SaveBits1 = Byte1 And 3
        Byte2 = Ascb(Midb(lsGroupBinary, 2, 1))
        SaveBits2 = Byte2 And 15
        Byte3 = 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 & Char4

        lsResult = lsResult & lsGroup64
    Next
    If M3 > 0 Then
        lsGroup64 = ""
        lsGroupBinary = Midb(asContents, len2 + 1, 3)

        Byte1 = Ascb(Midb(lsGroupBinary, 1, 1))
        SaveBits1 = Byte1 And 3
        Byte2 = Ascb(Midb(lsGroupBinary, 2, 1))
        SaveBits2 = Byte2 And 15
        Byte3 = 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 Then
            lsGroup64 = Char1 & Char2 & ChrB(61) & ChrB(61) '用=號補足位數
        Else
            lsGroup64 = Char1 & Char2 & Char3 & ChrB(61) '用=號補足位數
        End If

        lsResult = lsResult & lsGroup64
    End If

    Base64encode = lsResult
End Function

'將Base64編碼字符串轉換成Ansi編碼的字符串
Function Base64decode(asContents)
    Dim lsResult
    Dim lnPosition
    Dim lsGroup64, lsGroupBinary
    Dim Char1, Char2, Char3, Char4
    Dim Byte1, Byte2, Byte3
    Dim M4, len1, len2

    len1 = Lenb(asContents)
    M4 = len1 Mod 4

    If len1 < 1 Or M4 > 0 Then
        Base64decode = ""
        Exit Function
    End If

    If midb(asContents, len1, 1) = chrb(61) Then m4 = 3
    If midb(asContents, len1 -1, 1) = chrb(61) Then m4 = 2

    If m4 = 0 Then
        len2 = len1
    Else
        len2 = len1 -4
    End If

    For lnPosition = 1 To Len2 Step 4
        lsGroupBinary = ""
        lsGroup64 = Midb(asContents, lnPosition, 4)
        Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 1, 1)) - 1
        Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 2, 1)) - 1
        Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 3, 1)) - 1
        Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 4, 1)) - 1
        Byte1 = 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 & Byte3

        lsResult = lsResult & lsGroupBinary
    Next

    If M4 > 0 Then
        lsGroupBinary = ""
        lsGroup64 = Midb(asContents, len2 + 1, m4) & chrB(65) 'chr(65)=A,轉換成值爲0
        If M4 = 2 Then '補足4位,是爲了便於計算
            lsGroup64 = lsGroup64 & chrB(65)
        End If
        Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 1, 1)) - 1
        Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 2, 1)) - 1
        Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 3, 1)) - 1
        Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 4, 1)) - 1
        Byte1 = 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 Then
            lsGroupBinary = Byte1
        ElseIf M4 = 3 Then
            lsGroupBinary = Byte1 & Byte2
        End If

        lsResult = lsResult & lsGroupBinary
    End If

    Base64decode = lsResult

End Function

Function Encode64(byval Str)
    Encode64 = strAnsi2Unicode(Base64encode(strUnicode2Ansi(Str)))
End Function

Function Decode64(byval Str)
    Decode64 = strAnsi2Unicode(Base64decode(strUnicode2Ansi(Str)))
End Function

function CodeConvert(fileContent)
    dim stm
    set stm=Server.CreateObject("adodb.stream") 
    stm.Type=2
    stm.Mode=3
    stm.Charset="GB2312"
    stm.Open 
    stm.WriteText fileContent
    stm.Position=0
    stm.Charset="UTF-8"
    CodeConvert = stm.ReadText
    stm.Close 
    set stm=nothing 
End function%>

以上這段代碼就是別人封裝好的asp的base64加密解密方法。如今咱們還須要一個前端的js的base64加密函數。這個百度就有,不少。尤爲是jq,有一個封裝的不錯的,下載後直接調用就好。
如今來講說構思,首先咱們用js將百度編輯器的內容加密爲base64,這裏麪包含了文章和圖片,提交後,就跨過了防火牆。而後,由asp接收base64加密的字符串內容,而後調用asp的解碼函數。將正確的內容插入數據庫裏面
這裏有個很腦殘的問題(解碼亂碼)!!!開始的時候,我覺得是js的加密和asp的解密方法對不上。換了N多個的測試。最後發現,原來是編碼的問題!!!
http://www.mytju.com/classCode/tools/messyCodeRecover.as 在這個網址裏面,找到了核心的緣由。數據庫

輸入圖片說明

最後附上部分代碼圖安全

輸入圖片說明 輸入圖片說明 輸入圖片說明

相關文章
相關標籤/搜索