Hi everybody
I recently created a small vb prog and used the vb login form to open the prog via a password that is predefined in the code. What i'm after is a way of letting the user enter and set their own password. Can anyone help?
Thanks
Printable View
Hi everybody
I recently created a small vb prog and used the vb login form to open the prog via a password that is predefined in the code. What i'm after is a way of letting the user enter and set their own password. Can anyone help?
Thanks
The best way, is by not hardcoding the password. Store it somewhere in a file.
To write the password to a file:
To read the password from a file:Code:Dim Password As String 'the pw
Password = "top-secret"
'open the file
Open "c:\myfile.txt" For Output As #1
'write it
Print #1,,Password
'close the file
Close #1
But now, the password is readable for everybody who opens the file. In a few minutes, I'll post a way to encrypt the password a little.Code:Dim Password As String 'the pw
'open the file
Open "c:\myfile.txt" For Input As #1
'read it
Line Input #1,,Password
'close the file
Close #1
This code is for encryption/decryption.
If you give it a string, it will encrypt it. If you give it an encrypted string, it will decrypt it.Code:Function Crypt(Data As String) As String
Dim oneByte As String ' a swap variable
Dim Output As String ' the output
For i = 1 To Len(Data) 'loop trough all the bytes
oneByte = Right(Left(Data, i), 1) ' get a byte
oneByte = Chr((Int(Asc(oneByte)) Xor 152)) ' encrypt the byte
Output = Output & oneByte ' add it to the output
Next
Crypt = Output ' return the encrypted or decrypted data
End Function
Note that this way of encryption is not very secure. Everybody with some HEX-editor experience can crack it, but the average user can't crack it.