excel - 我可以在 VBA 中使用 DPAPI(或类似的东西)吗?

标签 excel dpapi vba

我关于 VBA 的初学者系列问题中的另一个。

我正在用 VBA 编写 Excel 插件,插件使用本地配置文件。
该文件需要包含远程服务的密码。
显然,将此密码存储为明文并不理想。但我正在寻找一种可以编码/解码文本的算法,因此它至少看起来不像配置文件中的纯文本。

我遇到了对 Windows 的引用DPAPI 但我不确定这是否是 Excel VBA 的合适解决方案。我也不确定如何在 VBA 中使用这个 API,因为我只找到了在 .NET 中使用它的引用资料。此项目无法使用 Visual Studio。

所以两部分的问题是这样的:
1) 如果可以在 VBA 中使用 DPAPI,我可以举个例子吗?
2)如果无法在 VBA 中使用 DPAPI,您对如何以某种可逆的编码方式存储文本有什么建议吗?

如果重要,该解决方案必须在 Excel 2003 及更高版本中运行。

再一次感谢你。

最佳答案

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



对于 Excel VBA,我建议使用 CAPICOM 库。

here 下载文件.安装后,请按照这些说明注册 Dll。

32 位操作系统

C:\Program Files\Microsoft CAPICOM 2.1.0.2 SDK\Lib 复制文件 Capicom.dll至C:\Windows\System32
开始菜单上的下一步 |运行,输入这个
Regsvr32 C:\Windows\System32\Capicom.dll

64 位操作系统

C:\Program Files (x86)\Microsoft CAPICOM 2.1.0.2 SDK\Lib\X86 复制文件 Capicom.dll至C:\Windows\SysWOW64
开始菜单上的下一步 |运行,输入这个
Regsvr32 C:\Windows\SysWOW64\Capicom.dll

现在我们准备在我们的 VBA 项目中使用它

将此代码粘贴到模块中
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

函数EncryptString加密字符串和函数DecryptString解密字符串。运行上述 Sub Sample 时查看结果快照

enter image description here

关于excel - 我可以在 VBA 中使用 DPAPI(或类似的东西)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10984256/

相关文章:

PHP Excel 在单击启用编辑之前为公式单元格提供空白输出

Excel/电子表格 : How to calculate a count between the dates

asp.net-core - ASP.NET Core DPAPI PersistKeyToFileSystem 加密 key

python - 在 Python 中使用 DPAPI?

vba - 在 vba excel 中使用 COUNTIF 的 DATE 条件的字符串值

java - 使用 Apache POI 计算 Excel 文件中的非空行

java - 如何解决在java中读取Excel xls文件时出现的错误

c# - ProtectData 类使用哪种加密算法?

excel - 设置这些属性对 Excel 宏的速度有多大影响 : Application. ScreenUpdating、Application.DisplayAlerts

excel - 在每个第 n 个单词之后如何将单元格中的字符串拆分为行