ASP on the base64 encoding decoding process

  Return Letter mail address, the contents of decoding!    Online collection, the specific provenance unknown! 

  <form Name="form1" method="post" action=""> 
  <table Width="95%" border="0" align="center" cellpadding="0" cellspacing="0"> 
<tr>
  <td> <div Align="center"> 
  <textarea Name="content" cols="77" rows="15" id="content"> <% = request.form ( "content ")%></ textarea> 
  </ Div> </ td> 
  </ Tr> 
<tr>
  <td> <div Align="center"> 
  <input Name="jm" type="submit" id="jm" value="提交"> 
  </ Div> </ td> 
  </ Tr> 
  </ Table> 
  </ Form> 
<%

  SBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + /" 
  SBASE_64_CHARACTERS = strUnicode2Ansi (sBASE_64_CHARACTERS) 

  Function strUnicodeLen (asContents) 
  'Unicode strings of the length of the Ansi code 
  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) 
  'Will be encoded Unicode string into Ansi encoded string 
  StrUnicode2Ansi = "" 
  Len1 = len (asContents) 
  For i = 1 to len1 
  Mid = varchar (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) 
  'Ansi will be encoded string, converted into Unicode encoded string 
  StrAnsi2Unicode = "" 
  Len1 = lenb (asContents) 
  If len1 = 0 then exit function 
  For i = 1 to len1 
  = Midb varchar (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) 
  'Ansi will be coded strings Base64 encoding 
  'AsContents should be encoded string ANSI (binary string can) 
  Dim lnPosition 
  Dim lsResult 
  Dim Char1 
  Dim Char2 
  Dim Char3 
  Dim Char4 
  Dim Byte1 
  Dim Byte2 
  Dim Byte3 
  Dim SaveBits1 
  Dim SaveBits2 
  Dim lsGroupBinary 
  Dim lsGroup64 
  Dim 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)) 
  'Supplement is to facilitate calculation of the median 

  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 = "" 
  Midb lsGroupBinary = (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

  'Dealing with the last remaining few characters 
  If M3> 0 then 
  LsGroup64 = "" 
  Midb lsGroupBinary = (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) 

  Then if M3 = 1 
  LsGroup64 = Char1 & Char2 & ChrB (61) & ChrB (61) 'use of complementary median = 
  Else 
  LsGroup64 = Char1 & Char2 & Char3 & ChrB (61) 'use of complementary median = 
  End if 

  LsResult = lsResult & lsGroup64 
  End if 

  Base64encode = lsResult 

  End Function 

  Function Base64decode (asContents) 
  'Base64 encoding strings will be converted into a string encoding Ansi 
  'AsContents should be encoded string is ANSI (binary string can) 
  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 
  'String length should be a multiple of 4 
  Base64decode = "" 
  Exit Function 
  End if 

  'Is not a final judgement, = 
  'Judgement is not the second-worst, = 
  'M4 said here last remaining separate deal with the needs of the number of characters 
  If midb (asContents, len1, 1) = chrb (61) then m4 = 3 
  If midb (asContents, len1-1, 1) = chrb (61) then m4 = 2 

  Then if m4 = 0 
  Len2 = len1 
  Else 
  Len2 = len1-4 
  End if 

  For lnPosition = 1 To Len2 Step 4 
  LsGroupBinary = "" 
  Midb lsGroup64 = (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 
  Chrb Byte1 = (((Char2 And 48) \ 16) Or (Char1 * 4) And & HFF) 
  Byte2 lsGroupBinary & Chrb = (((Char3 And 60) \ 4) Or (Char2 * 16) And & HFF) 
  Chrb Byte3 = ((((Char3 And 3) * 64) And & HFF) Or (Char4 And 63)) 
  LsGroupBinary = Byte1 & Byte2 & Byte3 

  LsResult = lsResult & lsGroupBinary 
Next

  'Dealing with the last remaining few characters 
  If M4> 0 then 
  LsGroupBinary = "" 
  Midb lsGroup64 = (asContents, len2 +1, m4) & chrB (65) 'chr (65) = A, converted into value 0 
  If M4 = 2 then 'fill the four, is to facilitate calculation 
  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 
  Chrb Byte1 = (((Char2 And 48) \ 16) Or (Char1 * 4) And & HFF) 
  Byte2 lsGroupBinary & Chrb = (((Char3 And 60) \ 4) Or (Char2 * 16) And & HFF) 
  Chrb Byte3 = ((((Char3 And 3) * 64) And & HFF) Or (Char4 And 63)) 

  Then if M4 = 2 
  LsGroupBinary = Byte1 
  Elseif M4 = 3 then 
  LsGroupBinary = Byte1 & Byte2 
  End if 

  LsResult = lsResult & lsGroupBinary 
  End if 

  Base64decode = lsResult 

  End Function 

  '————————————————- ——————————– 

  If request.Form ( "")<>"" then jm 
  Sdecode = request.Form ( "content") 

  Response.write replace (strAnsi2Unicode (Base64decode (strUnicode2Ansi (sdecode))), chr (13) & chr (10), "<br>") & "<hr>" 

  Response.end 
  End if 

%>

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags:

Releated Articles


0 Comments to “ASP on the base64 encoding decoding process”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.