Decodes a base64-encoded string into an array of Bytes.
Public Declare Function CNV_BytesFromB64Str Lib "diCrPKI.dll" (ByRef lpOutput As Byte, ByVal nOutBytes As Long, ByVal strInput As String) As Long
nLen = CNV_BytesFromB64Str(lpOutput(0), nOutBytes, strInput)
long __stdcall CNV_BytesFromB64Str(unsigned char *lpOutput, long nOutBytes, const char *szInput);
If successful, the return value is the number of bytes in the decoded array; otherwise it returns a negative error code.
Public Function cnvBytesFromB64Str
(szB64 As String) As Byte()
Public Function cnvFromBase64
(strBase64 As String) As Byte()
static bvec_t dipki::Cnv::FromBase64 (const std::string &s)
static Cnv.frombase64(s)
This uses the base64 encoding scheme from [RFC4648]. Pass a zero value for nOutBytes to find the required maximum possible number of bytes in the output array. The final array may be shorter.
@warning [Changed in v11.1] This function now returns an error if it finds an illegal character in the input string (previously any non-base64 character was just ignored). Whitespace characters (space, TAB, LF, CR, VT, FF) are still allowed and ignored but any other non-base64 characters will cause an error.
The following wrapper function will return the decoded bytes directly, with a default return value that won't cause a run-time error in the calling code.
Public Function cnvBytesFromB64Str(strB64 As String) As Byte() ' Returns byte array decoded from a base64 string Dim abData() As Byte Dim nDataLen As Long ' Set default return value that won't cause a run-time error cnvBytesFromB64Str = vbNullString nDataLen = CNV_BytesFromB64Str(0, 0, strB64) If nDataLen <= 0 Then Exit Function End If ReDim abData(nDataLen - 1) nDataLen = CNV_BytesFromB64Str(abData(0), nDataLen, strB64) If nDataLen <= 0 Then Exit Function End If ReDim Preserve abData(nDataLen - 1) cnvBytesFromB64Str = abData End Function
Old behaviour in the VBA immediate window:
? cnvHexStrFromBytes(cnvBytesFromB64Str("---BEGIN---/ty6mHZUMhA=")) 04418837FB72EA61D950C840
Result is 12 bytes long. The non-base64 character '-' is ignored. The valid but probably unintended characters "BEGIN" are decoded.
New behaviour:
? cnvHexStrFromBytes(cnvBytesFromB64Str("---BEGIN---/ty6mHZUMhA=")) ? PKI_ErrorCode() 8
Result is 0 bytes long. Error code 8 (INVALID_DATA_ERROR) is set.
CNV_B64StrFromBytes CNV_B64Filter