Need help to encrypt and decrypt password in VB

Thread Solved

Join Date: Mar 2006
Posts: 10
Reputation: laifa is an unknown quantity at this point 
Solved Threads: 0
laifa laifa is offline Offline
Newbie Poster

Need help to encrypt and decrypt password in VB

 
0
  #1
Mar 3rd, 2006
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, 127 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Need help to encrypt and decrypt password in VB

 
0
  #2
Mar 3rd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 10
Reputation: laifa is an unknown quantity at this point 
Solved Threads: 0
laifa laifa is offline Offline
Newbie Poster

Re: Need help to encrypt and decrypt password in VB

 
0
  #3
Mar 3rd, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Need help to encrypt and decrypt password in VB

 
0
  #4
Mar 4th, 2006
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, 149 views)
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 10
Reputation: laifa is an unknown quantity at this point 
Solved Threads: 0
laifa laifa is offline Offline
Newbie Poster

Re: Need help to encrypt and decrypt password in VB

 
0
  #5
Mar 4th, 2006
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,
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: Need help to encrypt and decrypt password in VB

 
0
  #6
Mar 5th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 10
Reputation: laifa is an unknown quantity at this point 
Solved Threads: 0
laifa laifa is offline Offline
Newbie Poster

Re: Need help to encrypt and decrypt password in VB

 
0
  #7
Mar 6th, 2006
[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, 53 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Need help to encrypt and decrypt password in VB

 
0
  #8
Mar 6th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: Need help to encrypt and decrypt password in VB

 
0
  #9
Mar 6th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Need help to encrypt and decrypt password in VB

 
0
  #10
Mar 6th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC