Use the VBA wrapper functions to avoid the pre-dimensioning issues for functions that output to a string or byte array. See VBA Wrapper Function List.
No need to make a first pass to find the required length, or pre-dimension the output variable using String(nChars, " ")
or ReDim lpOutput(nBytes-1)
,
and then call again (a potentially dangerous sequence of events).
The wrapper functions do it all in one line.
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 the PKI_ErrorCode
and pkiGetLastError()
functions to get more information.
Wrapper functions that output to a byte array return the array directly. No need to ReDim. Input byte arrays are passed directly without needing the (0)
and without needing to compute the length beforehand.
' The old way - we need all the byte array lengths Dim lpKey() As Byte Dim lpIV() As Byte Dim lpPlain() As Byte Dim lpCipher() As Byte Dim nBytes As Long Dim nDataLen As Long Dim nKeyLen As Long Dim nIvLen As Long ' Set values for lpPlain, lpKey, lpIV [cut] nDataLen = cnvBytesLen(lpPlain) nKeyLen = cnvBytesLen(lpKey) nIvLen = cnvBytesLen(lpIV) nBytes = CIPHER_EncryptBytes(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_EncryptBytes(lpCipher(0), nBytes, lpPlain(0), nDataLen, lpKey(0), nKeyLen, lpIV(0), nIvLen, "Aes128/CBC/OneAndZeroes", 0)
With wrapper function:
lpCipher = cipherEncryptBytes(lpPlain, lpKey, lpIV, "Aes128/CBC/OneAndZeroes")
That's 4 extra variables we don't need, and seven lines of code is reduced to one.
Note the (0)
needed in the raw function, e.g. lpPlain(0)
, is not required with the wrapper function.
' To pass a "null" (empty) byte array, assign `vbNullString` to the variable. Dim lpIV() As Byte lpIV = vbNullString ' Set to empty array lpCipher = cipherEncryptBytes(lpPlain, lpKey, lpIV, "Aes128/ECB")Note that you must pass a proper variable that has been Dim'd, you cannot pass
vbNullString
directly for a byte array parameter.
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)