Question prob while trying to use MD5 for password

rebelme23

Member
Joined
Mar 23, 2013
Messages
8
Programming Experience
Beginner
Iam getting 3 :frown::frown::frown:errors while trying the below code for storing the password of registered user as md5 hash and performing hash check at time of login...

please help me out.... thanks in advance !!!



VB.NET:
Option Strict On
Imports System.IO


Public Class LoginForm1

#Region "Functions"

    Private Function stringtomd4(ByRef content As String) As String
        Dim MS As System.Security.Cryptography.MD5CryptoServiceProvider
        Dim bytestring As Byte = System.Text.Encoding.ASCII.GetBytes(content)     ' ERROR 1 IS ON THIS LINE
        bytestring = MS.ComputeHash(bytestring)                                               ' ERROR 2 IS ON THIS LINE
        Dim finalstring As String = Nothing

        For Each bt As Byte In bytestring                                                          ' ERROR 3 IS ON THIS LINE
            finalstring &= bt.ToString("x2")

        Next

        Return finalstring.ToUpper()

    End Function

#End Region

    Private Function createuser(ByVal user As String, ByVal pass As String) As String

        If File.Exists(stringtomd4(user) & ",txt") = True Then
            Try
                My.Computer.FileSystem.WriteAllText(stringtomd4(user) & ".txt", stringtomd4(user) & _
                                                    Chr(32) & stringtomd4(pass), False)
                File.SetAttributes(stringtomd4(user) & ".txt", FileAttributes.Hidden)
                MsgBox("new user created")

            Catch ex As Exception
                MsgBox("something went wrong" & ex.Message)

            End Try
        Else
            MsgBox("sorry !!! this username has already been taken..")

        End If
        Return user
        Return pass

    End Function
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Try
            If stringtomd4(usernameTxtbox.Text) & Chr(32) & stringtomd4(passwordTxtbox.Text) = _
                IO.File.ReadAllText(stringtomd4(usernameTxtbox.Text) & ".txt") Then
                MsgBox("welcome")
            Else
                MsgBox("Wrong username/password")

            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub register_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles register.Click
        createuser(usernameTxtbox.Text, passwordTxtbox.Text)

    End Sub
End Class


ERRORS WHICH I AM GETTING:

1. Value of type '1-dimensional array of Byte' cannot be converted to 'Byte'.

2. Overload resolution failed because no accessible 'ComputeHash' can be called with these arguments:
'Public Function ComputeHash(buffer() As Byte) As Byte()': Value of type 'Byte' cannot be converted to '1-dimensional array of Byte'.
'Public Function ComputeHash(inputStream As System.IO.Stream) As Byte()': Value of type 'Byte' cannot be converted to 'System.IO.Stream'.

3.Expression is of type 'Byte', which is not a collection type.


 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,548
Location
Norway
Programming Experience
10+
Dim bytestring() As Byte =

or to just let compiler infer the type from result:
Dim bytestring =

Arrays in Visual Basic
 

rebelme23

Member
Joined
Mar 23, 2013
Messages
8
Programming Experience
Beginner
now i am getting a run-time error i changed bytestring to bytestring() as u said... and tried to enter a username and password but as soon as i click on "register" button an exception/error occurs and stops the program..

i was getting a warning message also after building the project....which said-----Variable 'M5' is used before it has been assigned a value. A null reference exception could result at runtime.

please see the below image also...

[img=http://s12.postimg.org/o7hp3ruqh/keyinj.jpg]
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,548
Location
Norway
Programming Experience
10+

rebelme23

Member
Joined
Mar 23, 2013
Messages
8
Programming Experience
Beginner
i tried this but its not working....

Option Strict On
Imports System.IO


Public Class LoginForm1

#Region "Functions"
Dim M5 As System.Security.Cryptography.MD5CryptoServiceProvider
Private Function stringtomd4(ByRef content As String) As String
Dim M5 As New System.Security.Cryptography.MD5CryptoServiceProvider()

Dim bytestring() As Byte = System.Text.Encoding.ASCII.GetBytes(content)
bytestring = M5.ComputeHash(bytestring)
Dim finalstring As String = Nothing

For Each bt As Byte In bytestring
finalstring &= bt.ToString("x2")

Next

Return finalstring.ToUpper()

End Function

#End Region

Private Function createuser(ByVal user As String, ByVal pass As String) As String

If File.Exists(stringtomd4(user) & ",txt") = True Then
Try
My.Computer.FileSystem.WriteAllText(stringtomd4(user) & ".txt", stringtomd4(user) & _
Chr(32) & stringtomd4(pass), False)
File.SetAttributes(stringtomd4(user) & ".txt", FileAttributes.Hidden)
MsgBox("new user created")

Catch ex As Exception
MsgBox("something went wrong" & ex.Message)

End Try
Else
MsgBox("sorry !!! this username has already been taken..")

End If
Return user
Return pass

End Function
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Try
If stringtomd4(usernameTxtbox.Text) & Chr(32) & stringtomd4(passwordTxtbox.Text) = _
IO.File.ReadAllText(stringtomd4(usernameTxtbox.Text) & ".txt") Then
MsgBox("welcome")
Else
MsgBox("Wrong username/password")

End If
Catch ex As Exception
MsgBox(ex.Message)

End Try
End Sub

Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub

Private Sub register_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles register.Click
createuser(usernameTxtbox.Text, passwordTxtbox.Text)

End Sub
End Class

please help me ..... i actually want to just put a login form in my project that has md5 encryption enabled for storing the passwords
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

Firstly, when posting code, please use the Code tags on the Advanced menu and not Quote tags.

A few things for you here:-

1) You followed JohnH's instruction, which is great, but you left the initial declaration at the class level being:-

VB.NET:
Dim M5 As System.Security.Cryptography.MD5CryptoServiceProvider

Get rid of it.

2) Your actual errors now are in the function CreateUser, you have:-

VB.NET:
If File.Exists(stringtomd4(user) & ",txt") = True Then

This should be:-

VB.NET:
If Not File.Exists(stringtomd4(user) & ".txt") = True Then

You are supposed to be checking that the file does NOT exist so you need to change your logic to use NOT. Then there is the syntax error in the file name being a comma which should be a period

3) My only other comment would be to declare string variables for the UserName and Password so that you only need to call the Hash routine once each for both variables. As it is, you are making unnecessary calls to the Hash routine which will slow your project down (even though you would never notice it).

Hope that helps.

Cheers,

Ian
 
Top Bottom