hard code a password in a vba code

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I need to hard code a password in a vba code.
Is there a way to make that password not decipherable/accessible to someone
looking at the password.
Just locking the VBA code from viewing is not good enough.
Many thanks,
Dan
 
You could obfuscate it simply with a rotating algorithm like so

Public Function Encrypt(ByRef InputVal As String, _
Optional ByVal NumBase As Long = 13)
Dim i As Long

For i = 1 To Len(InputVal)

Mid(InputVal, i, 1) = Chr(Asc(Mid(InputVal, i, 1)) + NumBase)
Next i

Encrypt = InputVal
End Function

Public Function decrypt(ByRef InputVal As String, _
Optional ByVal NumBase As Long = 13)
Dim i As Long

For i = 1 To Len(InputVal)

Mid(InputVal, i, 1) = Chr(Asc(Mid(InputVal, i, 1)) - NumBase)
Next i

decrypt = InputVal
End Function


Sub Test()

MsgBox "Encrypted: " & Encrypt("A test value")
MsgBox "Decrypted: " & decrypt(Encrypt("A test value"))
End Sub
 
Anyone who can unlock the VBA could see the password no problem.
Even if you stored the PW outside of the VBA in (eg) a compiled dll, you
still have to get it into the VBA at some point.

Tim
 
Back
Top