AES Encrypting a Microsoft Access Field Via VBA

吃可爱长大的小学妹 提交于 2019-12-05 18:27:32

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

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
Cheeso

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.

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