开发者

AES Encrypting a Microsoft Access Field Via VBA

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.

Since AES requ开发者_StackOverflow中文版ires 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.

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


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 CAPICOM which is a COM wrapper to the CryptoAPI and supports AES.
  • You can use a 3rd party library, the one from ebCrypt is excellent, compact and free.


Below is an example using RC4 encryption.

SOURCE: 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


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

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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜