INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

password encryption - vb6

password encryption - vb6

(OP)
Hello.  I have old VB6 code that has the user name and password hardcoded.  

I would like to encrypt the password in an INI file.
Then have the program read the ini file to retrieve the encrypted password.  

I know how to read the ini file.  I need help with the encryption / decrypt.

How do I encrypt the password in the ini?

In VB6, how do I decrypt the password once I retrieve it from the ini?


Thanks!
StayC

RE: password encryption - vb6

A complicated example can be found on microsoft site,
http://support.microsoft.com/kb/821762

alternatively use

http://www.freevbcode.com/ShowCode.Asp?ID=1367

A point though, its probably better practice never to allow the password to be viewed once written to the ini file.
Your encryption algorithm will always allow you to compare what the user enters to ensure it is correct, but the once the password is encrypted it should stay encrypted (unless you plan to provide a method of viewing passwords).


http://www.freevbcode.com/ShowCode.Asp?ID=972

 

"I'm living so far beyond my income that we may almost be said to be living apart"

RE: password encryption - vb6

(OP)
I user does not enter the password.  It's in the program so the program has access to data on another system.  The user never knows the user name or the password.

RE: password encryption - vb6

You definitely then shouldnt need access to the password then so link 3 should be ok.

"I'm living so far beyond my income that we may almost be said to be living apart"

RE: password encryption - vb6

I'm afraid that I have to disagree with hmckillop's links almost entirely.

I would never bother using any 'amateur' encryption or hashing algorithms. They are generally fallable and, given that we have good, rigorous, tested algorithms for doing both built-in to Windows why not use them?

Because it is too complicated? Well, yes, the Microsoft example is enough to scare you off. However, if you insist on using the API, then I provided a simpler example in thread222-535644: MD5 encryption. ANd if you add a reference to CAPICOM to your project (if it isn't already on your PC download it from Microsoft - and if you haven't got CAPICOM 2.1.0.2 then download it from Microsoft), and then full AES encryption/decryption becomes about this easy:

CODE

Public Sub Example()
    Dim result As String
    Dim Secret As EncryptedData
   
    Set Secret = New EncryptedData    
    Secret.Algorithm.Name = CAPICOM_ENCRYPTION_ALGORITHM_AES
    Secret.Algorithm.KeyLength = CAPICOM_ENCRYPTION_KEY_LENGTH_256_BITS
    Secret.SetSecret "My Secret Encryption Key"
    Secret.Content = "Hello" ' what we want to encrypt
    result = Secret.Encrypt
    MsgBox result
    Secret.Content = "random" ' just so we know that this is being reset by decryption
    Secret.Decrypt result
    MsgBox Secret.Content    
End Sub

which is pretty straightforward (although, of course, you now have the problem about managing the Key for the encryption/decryption ...)

And if you want a one-way hash (which in the OP's case I don't think you do) then here's a pretty simple API solution that I've provided in the past:

CODE

Private Type MD5_CTX
  i(1 To 2) As Long
  buf(1 To 4) As Long
  inp(1 To 64) As Byte
  digest(1 To 16) As Byte
End Type

Private Declare Sub MD5Init Lib "cryptdll" (Context As MD5_CTX)
Private Declare Sub MD5Update Lib "cryptdll" (Context As MD5_CTX, ByVal strInput As String, ByVal lLen As Long)
Private Declare Sub MD5Final Lib "cryptdll" (Context As MD5_CTX)

Public Sub Example3()
    Dim mybytes(15) As Byte
    MsgBox HashVersion1("Hello")
End Sub

Private Function HashVersion1(ByVal strPassword As String) As String
    Dim myContext As MD5_CTX
    Dim result As String
    Dim lp As Long
    Dim char As Variant
    
    MD5Init myContext
    MD5Update myContext, strPassword, Len(strPassword)
    MD5Final myContext
    result = StrConv(myContext.digest, vbUnicode)
    For lp = 1 To Len(result)
            HashVersion1 = HashVersion1 & Right("00" & Hex(Asc(Mid(result, lp, 1))), 2)
    Next
End Function
Or, again using CAPICOM:

CODE

Public Sub Example2()
    Dim myHash As HashedData
    
    Set myHash = New HashedData
    myHash.Algorithm = CAPICOM_HASH_ALGORITHM_MD5
    myHash.Hash StrConv("Hello", vbFromUnicode) ' for the sake of this example we are converting the string so we match earlier API solution
    MsgBox myHash.Value
End Sub
 

RE: password encryption - vb6

strongm,

Fair enough criticism, I did add the link for all them, including the microsoft reference.
The overall point of the post though was that there are multiple methods to do this already available on the web.

Your points acknowledged and good post.
 

"I'm living so far beyond my income that we may almost be said to be living apart"

RE: password encryption - vb6

StayC, did any of this help?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close