Reads from a file or string containing a public key into an "internal" public key string.
Public Declare Function RSA_ReadAnyPublicKey Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strKeyFileOrString As String, ByVal nOptions As Long) As Long
nRet = RSA_ReadAnyPublicKey(strOutput, nOutChars, strKeyFileOrString, nOptions) As Long
long __stdcall RSA_ReadAnyPublicKey(char *szOutput, long nOutChars, const char *szKeyFileOrString, long nOptions);
If successful, the return value is the number of characters in or required for the output string; otherwise it returns a negative error code.
Public Function rsaReadAnyPublicKey
(szKeyFileOrString As String, Optional nOptions As Long = 0) As String
static std::string dipki::Rsa::ReadPublicKey (const std::string &keyFileOrString)
static Rsa.read_public_key(keyfileorstr)
For the "raw" VBA/C function, the user must allocate an output string buffer szOutput of the required length. Specify a zero nOutChars or an empty string for szOutput to find the required length. ANSI C users must add one to this value when allocating memory.
This function will attempt to read the public key from "any" supported format. The output will be an ephemeral "internal" key string suitable for the current session only.
Supported formats are:
RSAKeyValue
or RSAKeyPair
element
To read in a public key from a PFX/p12 file, use RSA_ReadAnyPrivateKey
then RSA_PublicKeyFromPrivate
.
This supersedes the following functions:
RSA_ReadPublicKey
RSA_GetPublicKeyFromCert
This VB6/VBA wrapper function returns the "internal" public key string from the specified file.
Public Function rsaReadPublicKey(strKeyFile As String) As String ' Reads the public key from any supported public key file or PEM string ' Returns the key as an ephemeral base64 string or an empty string on error Dim nChars As Long ' How long is key string? nChars = RSA_ReadAnyPublicKey("", 0, strKeyFile, 0) If nChars <= 0 Then Exit Function End If ' Pre-dimension the string to receive data rsaReadPublicKey = String(nChars, " ") ' Read in the Public Key nChars = RSA_ReadAnyPublicKey(rsaReadPublicKey, nChars, strKeyFile, 0) End Function
This example reads in an RSA public key in JSON JWK format.
Dim strJson As String Dim strPublicKey As String Dim nChars As Long ' RSA public key as a JSON string ' Ref: RFC 7517 JSON Web Key (JWK) Appendix A.1 ' NB quotation marks (") inside a VBA string are escaped as "". strJson = "{""kty"":""RSA""," & _ """n"": ""0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx" & _ "4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMs" & _ "tn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2" & _ "QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbI" & _ "SD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqb" & _ "w0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw""," & _ """e"":""AQAB""," & _ """alg"":""RS256""," & _ """kid"":""2011-04-29""}" Debug.Print strJson ' Read in the public key as an internal string ' How many characters? nChars = RSA_ReadAnyPublicKey(ByVal 0&, 0, strJson, 0) Debug.Print "RSA_ReadAnyPublicKey returns " & nChars strPublicKey = String(nChars, " ") nChars = RSA_ReadAnyPublicKey(strPublicKey, nChars, strJson, 0) ' Examine the key's properties Debug.Print "RSA key size = " & RSA_KeyBits(strPublicKey) & " bits" Debug.Print "Hash code = 0x" & Hex(RSA_KeyHashCode(strPublicKey))
{"kty":"RSA","n": "0vx7agoebGcQSuuPiLJXZptN9nndr [cut] JzKnqDKgw","e":"AQAB","alg":"RS256","kid":"2011-04-29"} RSA_ReadAnyPublicKey returns 400 RSA key size = 2048 bits Hash code = 0xDFC04E17
Dim strIntKey As String strIntKey = rsaReadAnyPublicKey("AlicePubRSA.pub") Debug.Print strIntKey Debug.Print "KeyHashCode=0x" & Hex(RSA_KeyHashCode(strIntKey)) ' X.509 certificate strIntKey = rsaReadAnyPublicKey("AliceRSASignByCarl.cer") Debug.Print strIntKey Debug.Print "KeyHashCode=0x" & Hex(RSA_KeyHashCode(strIntKey)) ' Key in XML form strIntKey = rsaReadAnyPublicKey("alice-pub.xml") Debug.Print strIntKey Debug.Print "KeyHashCode=0x" & Hex(RSA_KeyHashCode(strIntKey))