MbahAgis

Base64 Encoding/Decoding (VB .Net)

Aug 23rd, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.18 KB | None | 0 0
  1. 'http://www.webtoolkit.info/javascript_base64.html#.YSOruI6A6Uk
  2. 'https://humanwhocodes.com/blog/2009/12/08/computer-science-in-javascript-base64-encoding/
  3.  
  4. Imports System.Text
  5. Imports System.Text.RegularExpressions
  6.  
  7. Public Class Base64
  8.  
  9.     'Private Property
  10.     Const _keyStr As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  11.  
  12.     ' public method for encoding
  13.     Public Function Encode(input As String, isBinaryData As Boolean) As String
  14.         If String.IsNullOrEmpty(input) Then Return ""
  15.  
  16.         Dim output As New StringBuilder
  17.         Dim cur, prev, byteNum As Double
  18.         Dim i As Integer = 0
  19.  
  20.         If Not isBinaryData Then input = _utf8_encode(input)
  21.  
  22.         While i < input.Length
  23.             cur = AscW(input(i)) 'charCodeAt
  24.             byteNum = i Mod 3
  25.  
  26.             Select Case byteNum
  27.                 Case 0
  28.                     output.Append(_keyStr(cur >> 2)) 'charAt
  29.                 Case 1
  30.                     output.Append(_keyStr(((prev And 3) << 4) Or (cur >> 4)))
  31.  
  32.                 Case 2
  33.                     output.Append(_keyStr(((prev And 15) << 2) Or (cur >> 6)))
  34.                     output.Append(_keyStr(cur And 63))
  35.             End Select
  36.  
  37.             prev = cur
  38.             i += 1
  39.  
  40.         End While
  41.  
  42.         If byteNum = 0 Then
  43.             output.Append(_keyStr((prev And 3) << 4))
  44.             output.Append("==")
  45.         ElseIf byteNum = 1 Then
  46.             output.Append(_keyStr((prev And 15) << 2))
  47.             output.Append("=")
  48.         End If
  49.  
  50.         Return output.ToString
  51.     End Function
  52.  
  53.     ' public method for decoding
  54.     Public Function Decode(input As String, isBinaryData As Boolean) As String
  55.  
  56.         If String.IsNullOrEmpty(input) Then Return ""
  57.  
  58.         Dim output As New StringBuilder
  59.         Dim cur, prev, digitNum As Double
  60.         Dim i As Integer = 0
  61.  
  62.         input = input.Replace("=", "")
  63.  
  64.         While i < input.Length
  65.             cur = _keyStr.IndexOf(input(i))
  66.             digitNum = i Mod 4
  67.             Select Case digitNum
  68.  
  69.                 Case 1
  70.                     output.Append(ChrW((prev << 2) Or (cur >> 4)))
  71.                 Case 2
  72.                     output.Append(ChrW(((prev And 15) << 4) Or (cur >> 2)))
  73.                 Case 3
  74.                     output.Append(ChrW(((prev And 3) << 6) Or cur))
  75.             End Select
  76.  
  77.             prev = cur
  78.             i += 1
  79.         End While
  80.  
  81.         If Not isBinaryData Then
  82.             Return _utf8_decode(output.ToString)
  83.         Else
  84.             Return output.ToString
  85.         End If
  86.  
  87.     End Function
  88.  
  89.     ' private method for UTF-8 encoding
  90.     Private Function _utf8_encode(_string As String) As String
  91.  
  92.         _string = Regex.Replace(_string, "/\r\n/g", "\n")
  93.  
  94.         Dim utftext As String = ""
  95.  
  96.         For n = 0 To _string.Length - 1
  97.             Dim c As Integer = AscW(_string(n))
  98.  
  99.             If c < 128 Then
  100.                 utftext &= ChrW(c)
  101.  
  102.             ElseIf (c > 127) AndAlso (c < 2048) Then
  103.                 utftext &= ChrW((c >> 6) Or 192)
  104.                 utftext &= ChrW((c And 63) Or 128)
  105.             Else
  106.                 utftext &= ChrW((c >> 12) Or 224)
  107.                 utftext &= ChrW(((c >> 6) And 63) Or 128)
  108.                 utftext &= ChrW((c And 63) Or 128)
  109.             End If
  110.         Next
  111.  
  112.         Return utftext
  113.     End Function
  114.  
  115.     ' private method for UTF-8 decoding
  116.     Private Function _utf8_decode(utftext As String)
  117.         Dim _String As String = ""
  118.         Dim i As Integer = 0
  119.         Dim c, c3, c2 As Integer
  120.  
  121.         While (i < utftext.Length)
  122.             c = AscW(utftext(i))
  123.  
  124.             If (c < 128) Then
  125.                 _String &= ChrW(c)
  126.  
  127.                 i += 1
  128.             ElseIf ((c > 191) And (c < 224)) Then
  129.                 c2 = AscW(utftext(i + 1))
  130.                 _String &= ChrW(((c And 31) << 6) Or (c2 And 63))
  131.                 i += 2
  132.             Else
  133.                 c2 = AscW(utftext(i + 1))
  134.                 c3 = AscW(utftext(i + 2))
  135.                 _String &= ChrW(((c And 15) << 12) Or ((c2 And 63) << 6) Or (c3 And 63))
  136.                 i += 3
  137.             End If
  138.         End While
  139.  
  140.         Return _String
  141.  
  142.     End Function
  143.  
  144. End Class
  145.  
Add Comment
Please, Sign In to add comment