·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[asp]asp语言中的base64编码与解码函数范例

时间:2016-05-16 10:52酷播
[asp]asp语言中的base64编码与解码函数范例

[asp]asp语言中的base64编码与解码函数范例

  1. <
  2. Option Explicit 
  3.  
  4. Dim sBASE_64_CHARACTERS 
  5. sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 
  6. sBASE_64_CHARACTERS = strUnicode2Ansi(sBASE_64_CHARACTERS) 
  7. Function strUnicodeLen(asContents) 
  8.     asContents1 = "a"&asContents 
  9.     len1 = Len(asContents1) 
  10.     k = 0 
  11.     For i = 1 To len1 
  12.         asc1 = Asc(Mid(asContents1, i, 1)) 
  13.         If asc1<0 Then asc1 = 65536 + asc1 
  14.         If asc1>255 Then 
  15.             kk = k + 2 
  16.         Else 
  17.             kk = k + 1 
  18.         End If 
  19.     Next 
  20.     strUnicodeLen = k -1 
  21. End Function 
  22.  
  23. Function strUnicode2Ansi(asContents) 
  24.  Dim len1,varasc,varHex,varlow,varhigh,varchar,i 
  25.     strUnicode2Ansi = "" 
  26.     len1 = Len(asContents) 
  27.     For i = 1 To len1 
  28.         varchar = Mid(asContents, i, 1) 
  29.         varasc = Asc(varchar) 
  30.         If varasc<0 Then varascvarasc = varasc + 65536 
  31.         If varasc>255 Then 
  32.             varHex = Hex(varasc) 
  33.             varlow = Left(varHex, 2) 
  34.             varhigh = Right(varHex, 2) 
  35.             strUnicode2AnsistrUnicode2Ansi = strUnicode2Ansi & chrb("&H" & varlow ) & chrb("&H" & varhigh ) 
  36.         Else 
  37.             strUnicode2AnsistrUnicode2Ansi = strUnicode2Ansi & chrb(varasc) 
  38.         End If 
  39.     Next 
  40. End Function 
  41.  
  42. Function strAnsi2Unicode(asContents) 
  43.  Dim len1,i,varasc,varchar 
  44.     strAnsi2Unicode = "" 
  45.     len1 = lenb(asContents) 
  46.     If len1 = 0 Then Exit Function 
  47.     For i = 1 To len1 
  48.         varchar = midb(asContents, i, 1) 
  49.         varasc = ascb(varchar) 
  50.         If varasc > 127 Then 
  51.             strAnsi2UnicodestrAnsi2Unicode = strAnsi2Unicode & Chr(ascw(midb(asContents, i + 1, 1) & varchar)) 
  52.             ii = i + 1 
  53.         Else 
  54.             strAnsi2UnicodestrAnsi2Unicode = strAnsi2Unicode & Chr(varasc) 
  55.         End If 
  56.     Next 
  57. End Function 
  58.  
  59. Function Base64encode(asContents) 
  60.     Dim lnPosition 
  61.     Dim lsResult 
  62.     Dim Char1,Char2,Char3,Char4 
  63.     Dim Byte1,Byte2,Byte3 
  64.     Dim SaveBits1,SaveBits2 
  65.     Dim lsGroupBinary 
  66.     Dim lsGroup64 
  67.     Dim m3,m4, len1, len2 
  68.  
  69.     len1 = Lenb(asContents) 
  70.     If len1<1 Then 
  71.         Base64encode = "" 
  72.         Exit Function 
  73.     End If 
  74.  
  75.     m3 = Len1 Mod 3 
  76.     If M3 > 0 Then asContentsasContents = asContents & String(3 - M3, chrb(0)) 
  77.     If m3 > 0 Then 
  78.         len1len1 = len1 + (3 - m3) 
  79.         len2 = len1 -3 
  80.     Else 
  81.         len2 = len1 
  82.     End If 
  83.     lsResult = "" 
  84.     For lnPosition = 1 To len2 Step 3 
  85.         lsGroup64 = "" 
  86.         lsGroupBinary = Midb(asContents, lnPosition, 3) 
  87.  
  88.         Byte1 = Ascb(Midb(lsGroupBinary, 1, 1)) 
  89.         SaveBits1 = Byte1 And 3 
  90.         Byte2 = Ascb(Midb(lsGroupBinary, 2, 1)) 
  91.         SaveBits2 = Byte2 And 15 
  92.         Byte3 = Ascb(Midb(lsGroupBinary, 3, 1)) 
  93.  
  94.         Char1 = Midb(sBASE_64_CHARACTERS, ((Byte1 And 252) \ 4) + 1, 1) 
  95.         Char2 = Midb(sBASE_64_CHARACTERS, (((Byte2 And 240) \ 16) Or (SaveBits1 * 16) And &HFF) + 1, 1) 
  96.         Char3 = Midb(sBASE_64_CHARACTERS, (((Byte3 And 192) \ 64) Or (SaveBits2 * 4) And &HFF) + 1, 1) 
  97.         Char4 = Midb(sBASE_64_CHARACTERS, (Byte3 And 63) + 1, 1) 
  98.         lsGroup64 = Char1 & Char2 & Char3 & Char4 
  99.  
  100.         lsResultlsResult = lsResult & lsGroup64 
  101.     Next 
  102.     If M3 > 0 Then 
  103.         lsGroup64 = "" 
  104.         lsGroupBinary = Midb(asContents, len2 + 1, 3) 
  105.  
  106.         Byte1 = Ascb(Midb(lsGroupBinary, 1, 1)) 
  107.         SaveBits1 = Byte1 And 3 
  108.         Byte2 = Ascb(Midb(lsGroupBinary, 2, 1)) 
  109.         SaveBits2 = Byte2 And 15 
  110.         Byte3 = Ascb(Midb(lsGroupBinary, 3, 1)) 
  111.  
  112.         Char1 = Midb(sBASE_64_CHARACTERS, ((Byte1 And 252) \ 4) + 1, 1) 
  113.         Char2 = Midb(sBASE_64_CHARACTERS, (((Byte2 And 240) \ 16) Or (SaveBits1 * 16) And &HFF) + 1, 1) 
  114.         Char3 = Midb(sBASE_64_CHARACTERS, (((Byte3 And 192) \ 64) Or (SaveBits2 * 4) And &HFF) + 1, 1) 
  115.  
  116.         If M3 = 1 Then 
  117.             lsGroup64 = Char1 & Char2 & ChrB(61) & ChrB(61) '用=号补足位数 
  118.         Else 
  119.             lsGroup64 = Char1 & Char2 & Char3 & ChrB(61) '用=号补足位数 
  120.         End If 
  121.  
  122.         lsResultlsResult = lsResult & lsGroup64 
  123.     End If 
  124.  
  125.     Base64encode = lsResult 
  126. End Function 
  127.  
  128. '将Base64编码字符串转换成Ansi编码的字符串 
  129. Function Base64decode(asContents) 
  130.     Dim lsResult 
  131.     Dim lnPosition 
  132.     Dim lsGroup64, lsGroupBinary 
  133.     Dim Char1, Char2, Char3, Char4 
  134.     Dim Byte1, Byte2, Byte3 
  135.     Dim M4, len1, len2 
  136.  
  137.     len1 = Lenb(asContents) 
  138.     M4 = len1 Mod 4 
  139.  
  140.     If len1 < 1 Or M4 > 0 Then 
  141.         Base64decode = "" 
  142.         Exit Function 
  143.     End If 
  144.  
  145.     If midb(asContents, len1, 1) = chrb(61) Then m4 = 3 
  146.     If midb(asContents, len1 -1, 1) = chrb(61) Then m4 = 2 
  147.  
  148.     If m4 = 0 Then 
  149.         len2 = len1 
  150.     Else 
  151.         len2 = len1 -4 
  152.     End If 
  153.  
  154.     For lnPosition = 1 To Len2 Step 4 
  155.         lsGroupBinary = "" 
  156.         lsGroup64 = Midb(asContents, lnPosition, 4) 
  157.         Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 1, 1)) - 1 
  158.         Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 2, 1)) - 1 
  159.         Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 3, 1)) - 1 
  160.         Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 4, 1)) - 1 
  161.         Byte1 = Chrb(((Char2 And 48) \ 16) Or (Char1 * 4) And &HFF) 
  162.         Byte2 = lsGroupBinary & Chrb(((Char3 And 60) \ 4) Or (Char2 * 16) And &HFF) 
  163.         Byte3 = Chrb((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63)) 
  164.         lsGroupBinary = Byte1 & Byte2 & Byte3 
  165.  
  166.         lsResultlsResult = lsResult & lsGroupBinary 
  167.     Next 
  168.  
  169.     If M4 > 0 Then 
  170.         lsGroupBinary = "" 
  171.         lsGroup64 = Midb(asContents, len2 + 1, m4) & chrB(65) 'chr(65)=A,转换成值为0 
  172.         If M4 = 2 Then '补足4位,是为了便于计算 
  173.             lsGroup64lsGroup64 = lsGroup64 & chrB(65) 
  174.         End If 
  175.         Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 1, 1)) - 1 
  176.         Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 2, 1)) - 1 
  177.         Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 3, 1)) - 1 
  178.         Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 4, 1)) - 1 
  179.         Byte1 = Chrb(((Char2 And 48) \ 16) Or (Char1 * 4) And &HFF) 
  180.         Byte2 = lsGroupBinary & Chrb(((Char3 And 60) \ 4) Or (Char2 * 16) And &HFF) 
  181.         Byte3 = Chrb((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63)) 
  182.  
  183.         If M4 = 2 Then 
  184.             lsGroupBinary = Byte1 
  185.         ElseIf M4 = 3 Then 
  186.             lsGroupBinary = Byte1 & Byte2 
  187.         End If 
  188.  
  189.         lsResultlsResult = lsResult & lsGroupBinary 
  190.     End If 
  191.  
  192.     Base64decode = lsResult 
  193.  
  194. End Function 
  195.  
  196. Function Encode64(byval Str) 
  197.     Encode64 = strAnsi2Unicode(Base64encode(strUnicode2Ansi(Str))) 
  198. End Function 
  199.  
  200. Function Decode64(byval Str) 
  201.     Decode64 = strAnsi2Unicode(Base64decode(strUnicode2Ansi(Str))) 
  202. End Function 
  203. %> 
  204.  
  205. <
  206. '调用测试 
  207.  
  208.  Dim str,encodestr 
  209.  str="酷播官方 http://www.cuplayer.com" 
  210.  encodestr=Encode64(str) '编码 
  211.  
  212.  response.write encodestr & "<hr>
  213.  response.write Decode64(encodestr) & "<hr>" '解码 
  214. %> 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器