How do i use a preset password on my visual basic program that will check first the username and and then the password and make sure that if username is this then the password should be that...

Dani AI

Generated

As asked: you want to check a username and then validate a preset password. is right that a conditional check works for a tiny test app, but hardcoding plaintext credentials or using plain If...Then checks is not safe for anything real. For a simple, correct pattern:

  • Keep user credentials outside the UI code (database, encrypted file, or config).
  • Store only salted password hashes, never plaintext.
  • Compare the entered password to the stored hash, then grant access.

A practical VB.NET example (hash + verify) using PBKDF2 follows. It is suitable for desktop apps that use .NET; for web apps use framework membership providers. This example creates a salt, derives a hash, and encodes both for storage.

' Requires: Imports System.Security.Cryptography

Function HashPassword(password As String) As String
    Dim salt(15) As Byte
    Using rng As New RNGCryptoServiceProvider()
        rng.GetBytes(salt)
    End Using
    Using pbkdf2 As New Rfc2898DeriveBytes(password, salt, 10000)
        Dim hash = pbkdf2.GetBytes(20)
        Dim result(salt.Length + hash.Length - 1) As Byte
        Array.Copy(salt, 0, result, 0, salt.Length)
        Array.Copy(hash, 0, result, salt.Length, hash.Length)
        Return Convert.ToBase64String(result)
    End Using
End Function

Function VerifyPassword(stored As String, password As String) As Boolean
    Dim src = Convert.FromBase64String(stored)
    Dim salt(15) As Byte
    Array.Copy(src, 0, salt, 0, salt.Length)
    Using pbkdf2 As New Rfc2898DeriveBytes(password, salt, 10000)
        Dim hash = pbkdf2.GetBytes(20)
        For i = 0 To hash.Length - 1
            If src(salt.Length + i) <> hash(i) Then Return False
        Next
        Return True
    End Using
End Function

Quick tips: trim inputs, decide on case sensitivity, do not reveal whether username or password is wrong (use a generic error), throttle or lock after repeated failures, and protect UI fields with UseSystemPasswordChar = True. For modern guidance on secure password storage see the OWASP Password Storage Cheat Sheet and the .NET Rfc2898DeriveBytes documentation.

Recommended Answers

All 2 Replies

Hi,
U must go with 'if else' conditions-
like

if 'object' = 'username1' then 
 if 'object'='password' then 
 login = true
 else
 login= false
 msgbox"plz enter valid password for this user"
 endif
else
 msgbox"plz enter valid user name"
endif

where 'object' is a filed of your database containig the username and password or may be you can use textbox.

if u r using textbox then u must specify the user name and password in the coding or use a inputbox to modify lateron.

I m not elaborating much hope u understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.