AES 通过 VBA 加密 Microsoft Access 字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3038504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:43:42  来源:igfitidea点击:

AES Encrypting a Microsoft Access Field Via VBA

ms-accessvbaaesaccess-vba

提问by Jason

I need to create a Microsoft Access database, but have a need, in one of my tables, for a single field to be strongly encrypted.

我需要创建一个 Microsoft Access 数据库,但需要在我的一个表中对单个字段进行高度加密。

Since AES requires both a key and an initialization vector, I've decided to solve this problem by requiring a password to access the database (as the key), and a field in the table to hold a SHA1 hash of the plaintext of the encrypted field.

由于 AES 需要一个密钥和一个初始化向量,我决定通过需要一个密码来访问数据库(作为密钥)和表中的一个字段来保存加密明文的 SHA1 哈希值来解决这个问题场地。

Does anyone know where I can find VBA-compatible code to actually do the encryption?

有谁知道我在哪里可以找到与 VBA 兼容的代码来实际进行加密?

回答by Alex K.

Some alternatives to writing it from scratch;

从头开始编写的一些替代方法;

  • You can do it with the native CryptoAPI (the root API is CryptAquireContext)
  • You can use Microsoft's CAPICOMwhich is a COM wrapper to the CryptoAPI and supports AES.
  • You can use a 3rd party library, the one from ebCryptis excellent, compact and free.
  • 您可以使用本机 CryptoAPI(根 API 是CryptAquireContext
  • 您可以使用 Microsoft 的CAPICOM,它是 CryptoAPI 的 COM 包装器并支持 AES
  • 您可以使用第 3 方库,来自ebCrypt 的库非常出色、紧凑且免费。

回答by JustJohn

Below is an example using RC4 encryption.

以下是使用 RC4 加密的示例。

SOURCE: http://bytes.com/topic/access/insights/906671-rc4-encryption-algorithm-vba-vbscript

来源:http: //bytes.com/topic/access/insights/906671-rc4-encryption-algorithm-vba-vbscript

Function fRunRC4(sMessage, strKey) As String
    Dim kLen, x, y, i, j, temp
    Dim s(256), k(256)

    'Init keystream
    kLen = Len(strKey)
    For i = 0 To 255
        s(i) = i
        k(i) = Asc(Mid(strKey, (i Mod kLen) + 1, 1))
    Next

    j = 0
    For i = 0 To 255
        j = (j + k(i) + s(i)) Mod 255
        temp = s(i)
        s(i) = s(j)
        s(j) = temp
    Next

    'Drop n bytes from keystream
    x = 0
    y = 0
    For i = 1 To 3072
        x = (x + 1) Mod 255
        y = (y + s(x)) Mod 255
        temp = s(x)
        s(x) = s(y)
        s(y) = temp
    Next

    'Encode/Decode
    For i = 1 To Len(sMessage)
        x = (x + 1) Mod 255
        y = (y + s(x)) Mod 255
        temp = s(x)
        s(x) = s(y)
        s(y) = temp

        fRunRC4 = fRunRC4 & Chr(s((s(x) + s(y)) Mod 255) Xor Asc(Mid(sMessage, i, 1)))
    Next


End Function

回答by Cheeso

You can do it with SlowAES, a Javascript implemnentation of AES, wrapped as a COM object.

您可以使用 SlowAES 来实现,它是一种 AES 的 Javascript 实现,封装为 COM 对象。

more info in this other answer.
How to encrypt in VBScript using AES?

其他答案中的更多信息。
如何使用 AES 在 VBScript 中加密?

working source code here:
http://cheeso.members.winisp.net/srcview.aspx?dir=AES-example

这里的工作源代码:http:
//cheeso.members.winisp.net/srcview.aspx?dir=AES-example

回答by David-W-Fenton

The MS Knowledge Base provides VB code for using the CryptoAPI. Note that the example explains how to encrypt but not how to decrypt the results. But it's quite easy to do it, as the decrypt API declaration has almost the same arguments. Note, however, that the string-to-byte conversion routine in the example code will incorrectly strip trailing spaces during the decryption process, so you have to alter that code to fix that.

MS 知识库提供了使用 CryptoAPI 的 VB 代码。请注意,该示例说明了如何加密,但未说明如何解密结果。但是这很容易做到,因为解密 API 声明具有几乎相同的参数。但是请注意,示例代码中的字符串到字节转换例程会在解密过程中错误地去除尾随空格,因此您必须更改该代码以解决该问题。