I've seen a lot of questions about how to make secure database passwords. I'm going to go over a method of encrypting a password using the MD5 encryption algorithm for maximum security.
First, you will need to download the attached class module (clsMD5.txt) and import it into your database. This class module is the core of what we're about to go over. Thanks to Robert Hubley for writing it - you're my hero!
Next, your database needs a users table. We'll use this one as an example.
Now, you will need some type of "edit user options" form for a user to create a password in. Build your form, and add an UNBOUND text box control for the password. We'll call it txtTempPassword . Make sure you set the Input Mask to Password so no one can spy on the password.
So, we have the following:
Now, on the AfterUpdate Event of txtTempPassword , use the following code:
The DigestStrToHexS tr() function does the work of turning your plain-text password into a secure, MD5 encrypted mess of hexadecimal gibbersih.
Now, what do you do when the user tries to log into your custom login form?
Your login form will probably follow this basic structure:
In the OnClick event of your button, you'll have the code that checks the text of the entered username and password against your table to see if there's a match. Obviously, with the password now encrypted, there won't be. The solution is to encrypt the search string using the same DigestStrToHexS tr() function. Consider the following:
You will obviously need to personalize this code to your use, but that's the idea. Have fun making databases with secure MD5 encrypted passwords!
First, you will need to download the attached class module (clsMD5.txt) and import it into your database. This class module is the core of what we're about to go over. Thanks to Robert Hubley for writing it - you're my hero!
Next, your database needs a users table. We'll use this one as an example.
Code:
tblUsers Field Type UserID AutoNum PK strLastName Text strFirstName Text strMI Text strUserName Text (no duplicates) strPassword Text (encrypted) strPermissions Text (access permissions)
So, we have the following:
Code:
Object Name Bound To Form frmUserOptions tblUsers Text Box txtTempPassword Unbound
Code:
strPassword = DigestStrToHexStr(Me.txtTempPassword)
Now, what do you do when the user tries to log into your custom login form?
Your login form will probably follow this basic structure:
Code:
Object Name Bound To --------------------------------------------------- Form frmLogin Unbound Text Box txtUsername Unbound Text Box txtPassword Unbound Button cmdLogin
Code:
Private Sub cmdLogin_Click()
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT UserID, strUsername, strPassword FROM tblUsers", dbOpenSnapshot)
'first, see if the username is valid
rs.FindFirst "strUsername = '" & Me.txtUsername & "'"
If rs.NoMatch Then GoTo ErrorOut
'next, check the password
If rs!strPassword = DigestStrToHexStr(Me.txtPassword) Then
DoCmd.Close
'open switchboard and run any other code
End If
Exit Sub
'close the app if the login was incorrect
ErrorOut:
MsgBox "Username/password combination is invalid." & _
vbCrLf & vbCrLf & "Exiting application.", vbCritical
Application.Quit
End Sub
Comment