CryptoSys PKI Pro Manual

RSA_ReadAnyPublicKey

Reads from a file or string containing a public key into an "internal" public key string.

VBA/VB6 Syntax

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

C/C++ Syntax

long __stdcall RSA_ReadAnyPublicKey(char *szOutput, long nOutChars, const char *szKeyFileOrString, long nOptions);

Parameters

szOutput
[out] to receive encoded public key data in "internal" format.
nOutChars
[in] specifying the maximum number of characters to be received.
szKeyFileOrString
[in] specifying the name of file containing the key or a string containing the key in PEM format or XML format.
nOptions
[in] not used in this release. Specify zero.

Returns (VBA/C)

If successful, the return value is the number of characters in or required for the output string; otherwise it returns a negative error code.

VBA Wrapper Syntax

Public Function rsaReadAnyPublicKey (szKeyFileOrString As String, Optional nOptions As Long = 0) As String

.NET Equivalent

Rsa.ReadPublicKey Method

C++ (STL) Equivalent

static std::string dipki::Rsa::ReadPublicKey (const std::string &keyFileOrString)

Python Equivalent

static Rsa.read_public_key(keyfileorstr)

Remarks

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:

To read in a public key from a PFX/p12 file, use RSA_ReadAnyPrivateKey then RSA_PublicKeyFromPrivate.

This supersedes the following functions:

Example (VBA core function)

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

Example (VBA wrapper function)

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))

See Also

RSA_ReadAnyPublicKey

[Contents] [Index]

[PREV: RSA_ReadAnyPrivateKey...]   [Contents]   [Index]   
   [NEXT: RSA_ReadEncPrivateKey...]

Copyright © 2004-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-09-23T07:52:09Z.