ASP自定义escape加密和unescape解密函数

ASP没有内置的加密解密函数,导致我们在做AJAX传递参数的时候容易出现乱码,下面跟大家分享:ASP自定义escape加密和unescape解密函数。

模拟escape函数进行加密

Function vbsEscape(str)'加密<br>
     dim i,s,c,a<br>
     s=&quot;&quot;<br>
     For i=1 to Len(str)<br>
         c=Mid(str,i,1)<br>
         a=ASCW(c)<br>
         If (a&gt;=48 and a&lt;=57) or (a&gt;=65 and a&lt;=90) or (a&gt;=97 and a&lt;=122) Then<br>
             s = s &amp; c<br>
         ElseIf InStr(&quot;@*_+-./&quot;,c)&gt;0 Then<br>
             s = s &amp; c<br>
         ElseIf a&gt;0 and a&lt;16 Then<br>
             s = s &amp; &quot;%0&quot; &amp; Hex(a)<br>
         ElseIf a&gt;=16 and a&lt;256 Then<br>
             s = s &amp; &quot;%&quot; &amp; Hex(a)<br>
         Else<br>
             s = s &amp; &quot;%u&quot; &amp; Hex(a)<br>
         End If<br>
     Next<br>
     vbsEscape = s<br>
End Function

模拟unescape函数进行解

Function vbsUnEscape(str)'解密<br>
     dim i,s,c<br>
     s=&quot;&quot;<br>
     For i=1 to Len(str)<br>
         c=Mid(str,i,1)<br>
         If Mid(str,i,2)=&quot;%u&quot; and i&lt;=Len(str)-5 Then<br>
             If IsNumeric(&quot;&amp;H&quot; &amp; Mid(str,i+2,4)) Then<br>
                 s = s &amp; CHRW(CInt(&quot;&amp;H&quot; &amp; Mid(str,i+2,4)))<br>
                 i = i+5<br>
             Else<br>
                 s = s &amp; c<br>
             End If<br>
         ElseIf c=&quot;%&quot; and i&lt;=Len(str)-2 Then<br>
             If IsNumeric(&quot;&amp;H&quot; &amp; Mid(str,i+1,2)) Then<br>
                 s = s &amp; CHRW(CInt(&quot;&amp;H&quot; &amp; Mid(str,i+1,2)))<br>
                 i = i+2<br>
             Else<br>
                 s = s &amp; c<br>
             End If<br>
         Else<br>
             s = s &amp; c<br>
         End If<br>
     Next<br>
     vbsUnEscape = s<br>
End Function