AES Encrypting a Microsoft Access Field Via VBA

痞子三分冷 提交于 2019-12-10 10:19:37

问题


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 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.

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


回答1:


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.



回答2:


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



回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/3038504/aes-encrypting-a-microsoft-access-field-via-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!