DreamDancer

Mime 64 encoder in VBS

Aug 7th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Attribute VB_Name = "mime64"
  2. Option Explicit
  3.  
  4. Function Base64Encode(InputStream As String) As String
  5.  
  6. Dim Base64Alphabet As String
  7.  Base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  8.  
  9. Dim OutputStream As String
  10. Dim ByteDecVal As Integer
  11. Dim BitBinVal As Integer
  12. Dim ByteGroup As String
  13. Dim ByteGroupBinStream As String
  14. Dim Padding As String
  15. Dim BytePtr As Long
  16.  
  17. Dim Index As Integer
  18.  
  19. Do While InputStream <> ""
  20.     ByteGroup = Left$(InputStream, 3)
  21.     Do While ByteGroup <> ""
  22.         For Index = 7 To 0 Step -1
  23.             ByteDecVal = Asc(Left$(ByteGroup, 1))
  24.             BitBinVal = BIT(ByteDecVal, Index)
  25.             ByteGroupBinStream = ByteGroupBinStream & Trim$(Str$(BitBinVal))
  26.         Next Index
  27.         ByteGroup = Mid$(ByteGroup, 2)
  28.     Loop
  29.     If Len(ByteGroupBinStream) < 24 Then
  30.         Padding = String$((((Len(ByteGroupBinStream) / 8) - 3) * -1), "=")
  31.         ByteGroupBinStream = ByteGroupBinStream & String$(Int(24 / Len(ByteGroupBinStream)) + 1, "0")
  32.     End If
  33.     Do While ByteGroupBinStream <> ""
  34.       BytePtr = Val(BitToDec("00" & Left$(ByteGroupBinStream, 6))) + 1
  35.         OutputStream = OutputStream & Mid$(Base64Alphabet, BytePtr, 1)
  36.         ByteGroupBinStream = Mid$(ByteGroupBinStream, 7)
  37.     Loop
  38.     OutputStream = OutputStream & Padding
  39.     ByteGroupBinStream = ""
  40.     InputStream = Mid$(InputStream, 4)
  41. Loop
  42. Base64Encode = OutputStream
  43. End Function
  44.  
  45.  
  46. Function BIT(ByteDecVal As Integer, Index As Integer) As Integer
  47. Dim bIndex As Integer
  48. bIndex = 2 ^ Index
  49. BIT = IIf((ByteDecVal And bIndex) > 0, 1, 0)
  50. End Function
  51.  
  52.  
  53. Function BitToDec(BitStr As String) As Long
  54. Dim Results As Long, CiDx As Long
  55.   For CiDx = 8 To 1 Step -1
  56.     Results = Results + (Val(Mid$(BitStr, CiDx, 1)) * (2 ^ (8 - CiDx)))
  57.   Next CiDx
  58.   BitToDec = Results
  59. End Function
Advertisement
Add Comment
Please, Sign In to add comment