CryptoSys Home > PKI > VBA/VB6 wrapper functions introduced v20.0.1

VBA/VB6 wrapper functions introduced v20.0.1


2021-10-03: New New in [v20.2] All wrapper functions now included in the one module basCrPKI.bas. Do not use any old version of basCrPKIWrappers.bas.

VBA wrapper functions

These safe wrapper functions extend the existing wrapper functions like rsaReadPrivateKey to all the other relevant core functions. See the basCrPKI.bas File Reference.

So, instead of having to do two passes to find the required output length, dimension the output buffer to receive the output, then call again (a potentially dangerous sequence of events!), we can do things in one go.

Dim curveName As String
Dim alicePrivateKeyHex As String
Dim ourPrivateKey As String
Dim nChars As Long

curveName = "X25519"
alicePrivateKeyHex = "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a"

' The old long way...
nChars = ECC_ReadKeyByCurve(vbNullString, 0, alicePrivateKeyHex, curveName, PKI_ECC_PRIVATE_KEY)
Debug.Assert nChars > 0
ourPrivateKey = String(nChars, " ")
nChars = ECC_ReadKeyByCurve(ourPrivateKey, nChars, alicePrivateKeyHex, curveName, PKI_ECC_PRIVATE_KEY)

' The new short way with a wrapper function
ourPrivateKey = eccReadKeyByCurve(alicePrivateKeyHex, curveName, PKI_ECC_PRIVATE_KEY)

Debug.Assert Len(ourPrivateKey) > 0

If the core VBA function name is FOO_FuncName(), then the wrapper function will be fooFuncName(). The function fooFuncName() will return the output as a string or byte array directly.

If an error occurs, the result will be a zero-length string or array. Use PKI_ErrorCode and pkiGetLastError() to get more information.

In general, there are no wrappers for functions that just return an integer value and do not require dimensioning an output buffer, for example, SIG_VerifyData. Just continue to use the original core function.

More examples

Compute hash digest

Dim strDigest As String
Dim lpMessage() As Byte
Dim lpDigest() As Byte

' Hex <-- Hex
strDigest = hashHexFromHex("616263", PKI_HASH_SHA256)   ' "abc" in hex
Debug.Print strDigest
lpMessage = StrConv("abc", vbFromUnicode)   ' "abc" in a byte array
' Hex <-- Bytes
strDigest = hashHexFromBytes(lpMessage, PKI_HASH_SHA256)
Debug.Print strDigest
' Bytes <-- Bytes
lpDigest = hashBytes(lpMessage, PKI_HASH_SHA256)
Debug.Print cnvHexStrFromBytes(lpDigest)

Encrypt data encoded in hex

Dim strKeyHex As String
Dim strIVHex As String
Dim strPlainHex As String
Dim strCipherHex As String
strKeyHex = "0123456789ABCDEFF0E1D2C3B4A59687"
strIVHex = "FEDCBA9876543210FEDCBA9876543210"
strPlainHex = "4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E20746F"

' Get encrypted output directly in hex
strCipherHex = cipherEncryptHex(strPlainHex, strKeyHex, strIVHex, "Aes128/CBC/OneAndZeroes", 0)
Debug.Print strCipherHex

Same again but using byte arrays

' Same again with bytes
Dim lpKey() As Byte
Dim lpIV() As Byte
Dim lpPlain() As Byte
Dim lpCipher() As Byte
lpPlain = StrConv("Now is the time for all good men to", vbFromUnicode)
lpKey = cnvBytesFromHexStr("0123456789ABCDEFF0E1D2C3B4A59687")
lpIV = cnvBytesFromHexStr("FEDCBA9876543210FEDCBA9876543210")

' Get encrypted output
lpCipher = cipherEncryptBytes(lpPlain, lpKey, lpIV, "Aes128/CBC/OneAndZeroes", 0)
Debug.Print cnvHexStrFromBytes(lpCipher)

Note we pass the Byte array arguments without the (0) required by the more primitive core function, and we don't need to find their lengths or ReDim them. By contrast, here's the "old" way:

' The old way - we need all the byte array lengths
Dim nBytes As Long
Dim nDataLen As Long
Dim nKeyLen As Long
Dim nIvLen As Long
nDataLen = cnvBytesLen(lpPlain)
nKeyLen = cnvBytesLen(lpKey)
nIvLen = cnvBytesLen(lpIV)
nBytes = CIPHER_EncryptBytes2(ByVal 0&, 0, lpPlain(0), nDataLen, lpKey(0), nKeyLen, lpIV(0), nIvLen, "Aes128/CBC/OneAndZeroes", 0)
Debug.Assert nBytes > 0
ReDim lpCipher(nBytes - 1)
nBytes = CIPHER_EncryptBytes2(lpCipher(0), nBytes, lpPlain(0), nDataLen, lpKey(0), nKeyLen, lpIV(0), nIvLen, "Aes128/CBC/OneAndZeroes", 0)
Debug.Print cnvHexStrFromBytes(lpCipher)

That's 4 extra variables we don't need, and seven lines of code is reduced to one.

lpCipher = cipherEncryptBytes(lpPlain, lpKey, lpIV, "Aes128/CBC/OneAndZeroes", 0)

How to pass empty byte array and string arguments

To create an empty Byte array, assign vbNullString to the variable. Then pass the variable itself.

' Same again using ECB mode with default PKCS#5 padding
lpIV = vbNullString   ' Set IV as empty array
lpCipher = cipherEncryptBytes(lpPlain, lpKey, lpIV, "Aes128/ECB", 0)

To pass an empty String variable you can simply use either the empty string "" or vbNullString.

' To pass a "null" IV in hex, just use the empty string
strCipherHex = cipherEncryptHex(strPlainHex, strKeyHex, "", "Aes128/ECB", 0)
' Or vbNullString
strCipherHex = cipherEncryptHex(strPlainHex, strKeyHex, vbNullString, "Aes128/ECB", 0)

* The function cnvBytesLen is a generic failsafe function to find the length of a byte array.

Contact us

To contact us or comment on this page, please send us a message.

[Go to top]

This page last updated 10 September 2025