Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

password encryption - vb6

Status
Not open for further replies.

StayC

Programmer
May 13, 2009
2
0
0
US
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
 
A complicated example can be found on microsoft site,

alternatively use


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).





"I'm living so far beyond my income that we may almost be said to be living apart
 
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.
 
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
 
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. 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:
[blue]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" [green]' what we want to encrypt[/green]
    result = Secret.Encrypt
    MsgBox result
    Secret.Content = "random" [green]' just so we know that this is being reset by decryption[/green]
    Secret.Decrypt result
    MsgBox Secret.Content    
End Sub[/blue]

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:
[blue]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[/blue]
Or, again using CAPICOM:
Code:
[blue]Public Sub Example2()
    Dim myHash As HashedData
    
    Set myHash = New HashedData
    myHash.Algorithm = CAPICOM_HASH_ALGORITHM_MD5
    myHash.Hash StrConv("Hello", vbFromUnicode) [green]' for the sake of this example we are converting the string so we match earlier API solution[/green]
    MsgBox myHash.Value
End Sub[/blue]

 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top