Question

another in my beginnerish series of questions about VBA.

I am in the process of writing an Excel add-in in VBA, and the add-in uses a local configuration file.
This file needs to contain a password for a remote service.
Obviously, it is less than ideal to store this password as plaintext. But I am looking for an algorithm that can encode/decode text so it at least doesn't look like plaintext in the configuration file.

I came across a reference to Windows DPAPI but I'm not sure whether this is an appropriate solution for Excel VBA. I also am not sure how I can use this API from within VBA, as I've only found references to using it with .NET. Visual Studio is unavailable to this project.

So the 2-part question is this:
1) If it is possible to use DPAPI from within VBA, can I have an example of its use?
2) If it is not possible to use DPAPI in VBA, do you have any suggestions for how to store text in some encoded fashion that is reversible?

The solution must work in Excel 2003 and later, if it matters.

Thank you once again.

Was it helpful?

Solution

The solution must work in Excel 2003 and later, if it matters.

For Excel VBA, I suggest using the CAPICOM Library.

Download the file from here. Once it is installed, follow these instructions for registering the Dll.

32 bit OS

Copy the file Capicom.dll from the C:\Program Files\Microsoft CAPICOM 2.1.0.2 SDK\Lib to C:\Windows\System32

Next on Start Menu | Run , type this

Regsvr32 C:\Windows\System32\Capicom.dll

64 bit OS

Copy the file Capicom.dll from the C:\Program Files (x86)\Microsoft CAPICOM 2.1.0.2 SDK\Lib\X86 to C:\Windows\SysWOW64

Next on Start Menu | Run , type this

Regsvr32 C:\Windows\SysWOW64\Capicom.dll

Now we are set to use it in our VBA Project

Paste this code in a module

Option Explicit

Sub Sample()
    Dim TextToEncrypt As String, EncryptedText As String
    Dim TextToDeCrypt As String, DeCryptedText As String
    Dim KeyToEncrypt As String

    TextToEncrypt = "Hello World"
    KeyToEncrypt = "JoshMagicWord"

    EncryptedText = EncryptString(TextToEncrypt, KeyToEncrypt)
    DeCryptedText = DecryptString(EncryptedText, KeyToEncrypt)

    Debug.Print "The string " & TextToEncrypt & " after encryption looks like this"
    Debug.Print "-----------------------------------------------------------------"
    Debug.Print EncryptedText
    Debug.Print "-----------------------------------------------------------------"
    Debug.Print "The above string after decrypting looks like this"
    Debug.Print "-----------------------------------------------------------------"
    Debug.Print DeCryptedText

End Sub

Public Function EncryptString(strText As String, ky As String) As String
    Dim Cap As Object
    Dim cryptIt

    Set Cap = CreateObject("CAPICOM.EncryptedData")

    Cap.Algorithm = 3
    Cap.SetSecret ky
    Cap.Content = strText
    EncryptString = Cap.Encrypt
End Function

Public Function DecryptString(strText As String, ky As String) As String
    Dim Cap As Object
    Dim cryptIt

    Set Cap = CreateObject("CAPICOM.EncryptedData")

    Cap.Algorithm = 3
    Cap.SetSecret ky
    Cap.Decrypt strText

    DecryptString = Cap.Content
End Function

The function EncryptString encrypts the string and the function DecryptString decrypts the string. See snapshot of results when you run the above Sub Sample

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top