Solved

HTTP 401 via Winsock

Posted on 2002-06-24
2
283 Views
Last Modified: 2013-11-13
Hi, i'm sort of writing a web server program, but its not going to be a full web server its just going to serve some files. Here is my problem I'm trying to do authentication BUT I dont know what to do with the data i get back from the client...

Dim authdata As String
authdata = "HTTP/1.1 401 Authorization Required" & vbCrLf & _
"Date: " & Format(Date, "DDD") & ", " & Format(Date, "DD") & " " & Format(Date, "MMM") & " " & Format(Date, "YYYY") & " " & Format(Time, "H:MM:SS GMT") & vbCrLf & _
"Server: CarbosoftFileServ/1.0.0 (x86)" & vbCrLf & _
"WWW-Authenticate: Basic realm = ""Please enter your Username and Password.""" & vbCrLf & _
"Connection: Close" & vbCrLf & _
"Content-Type: text/html; charset=iso-8859-1" & vbCrLf & vbCrLf & _
"Please enter your Username and Password!" & vbCrLf & vbCrLf
Winsock1.SendData authdata

Thats what I'm sending to the client to ask for the password...below is what I get back, where it says "Basic" and it has a bunch of charecters...what is that? is that base64 encoding or what??? Cuz I'm not sure what to do with it, once i can decode it ill have it from there...thanks

GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90)
Host: localhost
Connection: Keep-Alive
Authorization: Basic YnJpYW46MTIzNA==

see that last line, that really looks like Base64...but what is it?
0
Comment
Question by:BrianGEFF719
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
PNJ earned 50 total points
ID: 7104689
YnJpYW46MTIzNA== is Base64 encoding and says

Brian: 1234

You will need to decode that into "english" using a Base64 decoder. The one I've just to get the above runs to 300 lines.

You are more than welcome to the code. I found it out by researching using Google.

The form in VB5 is as follows. Just cut and paste it. sorry it so long and it's "non-production" code (ie the form's a bit rough around edges with not much comments) The FRX file simply contained an icon, so don't worry about any error on load)

VERSION 5.00
Begin VB.Form frmBase64
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "Decode / Encode Base64"
   ClientHeight    =   2085
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   6090
   Icon            =   "Base64.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   2085
   ScaleWidth      =   6090
   ShowInTaskbar   =   0   'False
   StartUpPosition =   2  'CenterScreen
   Begin VB.CommandButton cmdDecode
      Caption         =   "&Decode"
      Height          =   345
      Left            =   1320
      TabIndex        =   5
      Top             =   1590
      Width           =   1785
   End
   Begin VB.TextBox txtDecode
      Height          =   300
      Left            =   1320
      TabIndex        =   3
      Top             =   1170
      Width           =   4545
   End
   Begin VB.TextBox txtEncode
      Height          =   300
      Left            =   1320
      TabIndex        =   1
      Top             =   240
      Width           =   4545
   End
   Begin VB.CommandButton cmdEncode
      Caption         =   "&Encode"
      Height          =   345
      Left            =   1320
      TabIndex        =   0
      Top             =   660
      Width           =   1785
   End
   Begin VB.Label Label2
      AutoSize        =   -1  'True
      BackStyle       =   0  'Transparent
      Caption         =   "Text to Decode:"
      Height          =   195
      Left            =   120
      TabIndex        =   4
      Top             =   1200
      Width           =   1155
   End
   Begin VB.Label Label1
      AutoSize        =   -1  'True
      BackStyle       =   0  'Transparent
      Caption         =   "Text to Encode:"
      Height          =   195
      Left            =   120
      TabIndex        =   2
      Top             =   270
      Width           =   1140
   End
End
Attribute VB_Name = "frmBase64"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

   ' Research on the internet:
   
   ' RFC 2617
   ' http://ftp.ics.uci.edu/pub/ietf/http/rfc2617.txt
   ' http://www.marcsweb.com/mnweb_ccppbase64.shtml
   
   Private Base64Tab(63)      As Byte
   Private DecodeTable(233)   As Byte

Sub InitTable()
   
   Dim i As Integer
   
   Dim tDecodeTable As Variant
   
   tDecodeTable = Array( _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "62", "255", "255", "255", "63", "52", "53", "54", _
      "55", "56", "57", "58", "59", "60", "61", "255", "255", "255", "64", _
      "255", "255", "255", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", _
      "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", _
      "22", "23", "24", "25", "255", "255", "255", "255", "255", "255", "26", _
      "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", _
      "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", _
      "51", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", "255", _
      "255", "255")

   For i = LBound(tDecodeTable) To UBound(tDecodeTable)
      DecodeTable(i) = tDecodeTable(i)
   Next
         
   For i = 65 To 90
      Base64Tab(i - 65) = i
   Next
   
   For i = 97 To 122
      Base64Tab(i - 71) = i
   Next
   
   For i = 0 To 9
      Base64Tab(i + 52) = 48 + i
   Next
   
   Base64Tab(62) = 43
   Base64Tab(63) = 47

End Sub


Private Sub cmdDecode_Click()

   Dim InPass As String, OutPass As String
   
   ' Aladdin:open sesame         is:
   ' QWxhZGRpbjpvcGVuIHNlc2FtZQ==
   
   InitTable
   
   DecodeB64 txtDecode, OutPass
   
   txtEncode = OutPass
   
End Sub

Private Sub cmdEncode_Click()

   Dim InPass As String, OutPass As String
   
   ' Aladdin:open sesame         is:
   ' QWxhZGRpbjpvcGVuIHNlc2FtZQ==
   
   InitTable
   
   EncodeB64 txtEncode, OutPass
   
   txtDecode = OutPass
   
End Sub

Public Sub DecodeB64(ByRef FileIn As String, ByRef Out As String)

   Dim Inp(3) As Byte
   Dim iTemp As Long
   Dim i As Long
   
   Out = ""
   
   For i = 1 To Len(FileIn) Step 4
   
      Inp(0) = DecodeTable(Asc(Mid$(FileIn, i + 0, 1)))
      Inp(1) = DecodeTable(Asc(Mid$(FileIn, i + 1, 1)))
      Inp(2) = DecodeTable(Asc(Mid$(FileIn, i + 2, 1)))
      Inp(3) = DecodeTable(Asc(Mid$(FileIn, i + 3, 1)))
     
      If Inp(3) = 64 Or Inp(2) = 64 Then
     
         If Inp(3) = 64 And Not (Inp(2) = 64) Then
         
            Inp(0) = DecodeTable(Asc(Mid$(FileIn, i + 0, 1)))
            Inp(1) = DecodeTable(Asc(Mid$(FileIn, i + 1, 1)))
            Inp(2) = DecodeTable(Asc(Mid$(FileIn, i + 2, 1)))
           
            ' 2 bytes out
            Out = Out & Chr$((Inp(0) * 4) Or ((Inp(1) \ 16) And &H3))
            Out = Out & Chr$(((Inp(1) And &HF) * 16) Or ((Inp(2) \ 4) And &HF))
            Exit Sub
           
         ElseIf Inp(2) = 64 Then
     
            Inp(0) = DecodeTable(Asc(Mid$(FileIn, i + 0, 1)))
            Inp(1) = DecodeTable(Asc(Mid$(FileIn, i + 1, 1)))
           
            ' 1 byte out
            Out = Out & Chr$((Inp(0) * 4) Or ((Inp(1) \ 16) And &H3))
            Exit Sub
           
         End If
      End If
     
      ' 3 bytes out
      Out = Out & Chr$((Inp(0) * 4) Or ((Inp(1) \ 16) And &H3))
      Out = Out & Chr$(((Inp(1) And &HF) * 16) Or ((Inp(2) \ 4) And &HF))
      Out = Out & Chr$(((Inp(2) And &H3) * 64) Or Inp(3))
     
   Next i
   
End Sub
Public Sub EncodeB64(ByRef FileIn As String, ByRef Out As String)

    Dim Bin(2) As Byte
    Dim i As Long
    Dim Length As Long
    Dim Remaining As Integer
   
    Length = Len(FileIn)
    Remaining = ((Length) Mod 3)
   
    Out = ""
   
    For i = 1 To Length - Remaining Step 3
   
        ' 3 bytes in
        Bin(0) = Asc(Mid$(FileIn, i, 1))
        Bin(1) = Asc(Mid$(FileIn, i + 1, 1))
        Bin(2) = Asc(Mid$(FileIn, i + 2, 1))
       
        ' 4 bytes out
        Out = Out & Chr$(Base64Tab((Bin(0) \ 4) And &H3F))
        Out = Out & Chr$(Base64Tab((Bin(0) And &H3) * 16 Or (Bin(1) \ 16) And &HF))
        Out = Out & Chr$(Base64Tab((Bin(1) And &HF) * 4 Or (Bin(2) \ 64) And &H3))
        Out = Out & Chr$(Base64Tab(Bin(2) And &H3F))
       
    Next
   
    If Remaining = 1 Then    ' if there is 1 byte remaining
   
        ' Read 1 byte, the second in 0
        Bin(0) = Asc(Right$(FileIn, 1))
        Bin(1) = 0

        Out = Out & Chr$(Base64Tab((Bin(0) \ 4) And &H3F))
        Out = Out & Chr$(Base64Tab((Bin(0) And &H3) * 16 Or (Bin(1) \ 16) And &HF))
        Out = Out & Chr$(61)
        Out = Out & Chr$(61)
       
    ElseIf Remaining = 2 Then    'if there are 2 bytes remaining
   
        ' Read 2 bytes, the third is 0
        Bin(0) = Asc(Mid$(FileIn, Len(FileIn) - 1, 1))
        Bin(1) = Asc(Right$(FileIn, 1))
        Bin(2) = 0

        Out = Out & Chr$(Base64Tab((Bin(0) \ 4) And &H3F))
        Out = Out & Chr$(Base64Tab((Bin(0) And &H3) * 16 Or (Bin(1) \ 16) And &HF))
        Out = Out & Chr$(Base64Tab((Bin(1) And &HF) * 4 Or (Bin(2) \ 64) And &H3))
        Out = Out & Chr$(61)
       
    End If
   
End Sub

The VBP:
Type=Exe
Form=Base64.frm
Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\WINDOWS\System32\stdole2.tlb#OLE Automation
IconForm="frmBase64"
Startup="frmBase64"
HelpFile=""
Title="Base64"
ExeName32="Base64.exe"
Command32=""
Name="Base64"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=1
AutoIncrementVer=1
ServerSupportFiles=0
VersionCompanyName="My Company"
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
ThreadPerObject=0
MaxNumberOfThreads=1

[MS Transaction Server]
AutoRefresh=1


0
 
LVL 19

Author Comment

by:BrianGEFF719
ID: 7106640
Yah, thats what I thought, I figured it was base64...Thanks for the decodeing function.

-Brian
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question