944,116 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Mar 3rd, 2006
0

Need help to encrypt and decrypt password in VB

Expand Post »
Hi,
As far as encryption goes, I am a newbie. Right now when I login, it will compare the user and password stored in my database. If both match, I will be able to login, otherwise I will be refused. The user and password are now stored in plaintext. How do I encrypt the password and stored it into the database and when I login, it will decrypt the password again. Would appreciate if anyone of you can tell me how to do the coding in order to achieve this.
Please take a look at my attached file. Thanking you all in advance.
Attached Files
File Type: zip test.zip (12.8 KB, 871 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
laifa is offline Offline
10 posts
since Mar 2006
Mar 3rd, 2006
0

Re: Need help to encrypt and decrypt password in VB

I don't personally suggest the method you are inquiring about.... most password systems as of late, have been using an MD5 (or some other algorithm) to "Hash" the password. Just a little defination here, a Hash is a string that can NOT be decrypted. It's gone through some crazy algorithms that make the string permanently encrypted. Under normal circumstances, this doesn't seem like a very good plan huh? What we do next, though, is when we want to see if the password is correct, is we use the exact same algorithm to Hash what the user types in for the password, and compare the two Hashes with each other. Naturally, if both Hashes are identical, then the password must clearly be the same too. This increases the workload of a password cracker significantly, and adds countless more attempts to a bruteforce attack.

There are pretty good encryption algorithms out there that are pretty darn secure, and at the same time, are decryptable (such as blowfish and triple des) and most of these require a key pair, that gets generated, and only the partner key of something encrypted can decrypt it.

If you aren't THAT worried about security, and only want to keep prying eyes from seeing the password in plain text, then you could do something as simple as an XOR encryption, which is nothing more than an exclusive OR of bits.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 3rd, 2006
0

Re: Need help to encrypt and decrypt password in VB

Quote originally posted by Comatose ...
I don't personally suggest the method you are inquiring about.... most password systems as of late, have been using an MD5 (or some other algorithm) to "Hash" the password. Just a little defination here, a Hash is a string that can NOT be decrypted. It's gone through some crazy algorithms that make the string permanently encrypted. Under normal circumstances, this doesn't seem like a very good plan huh? What we do next, though, is when we want to see if the password is correct, is we use the exact same algorithm to Hash what the user types in for the password, and compare the two Hashes with each other. Naturally, if both Hashes are identical, then the password must clearly be the same too. This increases the workload of a password cracker significantly, and adds countless more attempts to a bruteforce attack.

There are pretty good encryption algorithms out there that are pretty darn secure, and at the same time, are decryptable (such as blowfish and triple des) and most of these require a key pair, that gets generated, and only the partner key of something encrypted can decrypt it.

If you aren't THAT worried about security, and only want to keep prying eyes from seeing the password in plain text, then you could do something as simple as an XOR encryption, which is nothing more than an exclusive OR of bits.
Thanking you for your reply. I know what you mean. The problem lies in the codng. How shall I get it done?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
laifa is offline Offline
10 posts
since Mar 2006
Mar 4th, 2006
0

Re: Need help to encrypt and decrypt password in VB

Ok, This code was taken from http://www.frez.co.uk/freecode.htm#md5. You can use the class module to instantiate an instance and use the md5 call of it. Like so:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim oMD5 As CMD5
  2. Set oMD5 = New CMD5
  3. Hash = oMD5.MD5("Your Password Here")

Attached is the test project appended to the site I previously mentioned.
Attached Files
File Type: zip MD5.zip (16.0 KB, 1145 views)
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 4th, 2006
0

Re: Need help to encrypt and decrypt password in VB

Hi,
Thank you for your time. I studied only for a brief term in VB
3 years ago and my skill is not good enough to understand the coding. What I actually want is just to write a simple code - e.g. if my input text(password) is "abcd", the output text(encrypted password) should be "defg" if I want to shift 3 keys and then save it into my database file. When I next login into the database with the input "abcd" it will then convert this
string back to "defg", checked it against the database. If it is correct, then it will allowed me to login.

Regards,
Reputation Points: 10
Solved Threads: 0
Newbie Poster
laifa is offline Offline
10 posts
since Mar 2006
Mar 5th, 2006
0

Re: Need help to encrypt and decrypt password in VB

Hi Iafia,

If all you want to do is shift the letters three steps you can easily do that by taking the ASCII value of each letter add three to it and then convert it back to a character, i.e.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim Password As String
  2. Dim i As Integer
  3. Dim Return As String
  4.  
  5. Return = ""
  6. For i = 1 To Len(Password)
  7. Return = Return & Chr(Asc(Mid(Password, i, 1)) + 3)
  8. Next

This will work for all text input as long as you don't use the last three characters of the standard 256 ASCII character set, which I sincerely doubt will be used since they are special characters.

Hope this helps

Happy coding

Yomet
Reputation Points: 16
Solved Threads: 10
Junior Poster
Yomet is offline Offline
134 posts
since Nov 2005
Mar 6th, 2006
0

Re: Need help to encrypt and decrypt password in VB

[QUOTE=Yomet]Hi Iafia,

If all you want to do is shift the letters three steps you can easily do that by taking the ASCII value of each letter add three to it and then convert it back to a character, i.e.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim Password As String
  2. Dim i As Integer
  3. Dim Return As String
  4.  
  5. Return = ""
  6. For i = 1 To Len(Password)
  7. Return = Return & Chr(Asc(Mid(Password, i, 1)) + 3)
  8. Next

This will work for all text input as long as you don't use the last three characters of the standard 256 ASCII character set, which I sincerely doubt will be used since they are special characters.

Thank you for your codes. I managed to work out a solution. It seems to work all right. But each time I tried to encrypt, the encrypted password is appended to the first encrypted password.e.g. if the first password is abc, the encrypted password would be def. However the second time I encrypt with mno, the encrypt password become defpqr instead of just pqr.
What is wrong with my coding? Please see the attached zip. Would appreciate if you could tell
me why this happen.
Regards,
Attached Files
File Type: zip caesar_test.zip (1.7 KB, 393 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
laifa is offline Offline
10 posts
since Mar 2006
Mar 6th, 2006
0

Re: Need help to encrypt and decrypt password in VB

You never clear the results variable. The results variable always gets added to, because results = results & Chr(Asc(Mid(Password, i, 1)) + 3), which means, results is equal to results and the return of these nested functions, every time it's called (I'm not going to go into variable scope here). When the button is clicked, you need to have it set results = "" before the for loop.

A few pointers and critique here (if you are one of those people who can't stand criticism, stop reading, your answer is above).

Point 1
Indent Your Code
I know that on small projects, it's no big deal... but when you get into larger bigger projects, if you don't indent your code for clarity, YOU WILL get lost in it. Indenting is a necessary part of programming and debugging, and regardless of project size, should still be adhered to. Ideally, The code should be:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. For i = 1 To Len(Password)
  2. Result = Result & Chr(Asc(Mid(Password, i, 1)) + 3)
  3. Next
  4.  
  5. If Text1.Text = "" Then
  6. MsgBox "Please key in your password"
  7. Else
  8. Text2.Text = Result
  9. End If

Point 2
Code Placement
You test if the textbox is empty, After you perform the operation on the textbox...(you encrypt the data in the textbox, AND THEN, you test if it's empty or not...You should test it first, because even though the for loop doesn't care if the textbox is empty, if it was different scenario, where it wrote it to a database or used it in some other means, you could encounter some problems.

Point 3
vbnullstring
You'll make your VB code run a LOT faster by replacing "" with vbnullstring where applicable. "" is still considered a sting (an empty string), where vbnullstring is a special character. 1 or 2 times probably won't make a difference, but again, on big applications you can see some serious speed increase on timestamps of running code if you make this minor adjustment, and stick to it now. It operates the same for the most part, with the exception that it's a lot faster. I don't always do it, but it's something that's really good to know and a practice that's really good to be in.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 6th, 2006
0

Re: Need help to encrypt and decrypt password in VB

Iafa,

The answer to your problem is quite simple, even if not obvious. You declared all your variables in the beginning of the module. This means that they will retain their value for the whole run of the program, therefore, when you encrypt a new password the variable Result already contain the current encrypted password and hence the new password is appended.

You should take the habit of having as few public variables as possible in your programs, using parameters to pass values to functions and subs or letting each sub in a form read the values from the controls instead. This makes for better code, easier debugging (since you don't have to worry about what has been done to the vaiable somewhere else) and more variables available (since you can use the same names is different subs).

So your solutions are the following:
1) Move the variable declarations inside the Click event handler, i.e.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command1_Click()
  2. Dim Password As String
  3. Dim i As Integer
  4. Dim Result As String
  5. ....
2) Reset the value of Result each time, i.e.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. ...
  2. Result = ""
  3. For i = 1 To Len(Password)
  4. ...

I strongly suggest the first for the reasons stated above but the second option will work as well.

Happy coding

Yomet
Reputation Points: 16
Solved Threads: 10
Junior Poster
Yomet is offline Offline
134 posts
since Nov 2005
Mar 6th, 2006
0

Re: Need help to encrypt and decrypt password in VB

In truth I agree with Yomet on this.... I didn't want to delve into variable scope, because some people have a real hard time understanding scope... the best solution is to put the variables in the proper scope.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: please for maths student
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: A couple of things





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC