CryptoSys PKI Pro Manual

RSA_PublicKeyFromPrivate

Converts an internal RSA private key string into an internal public key string.

VBA/VB6 Syntax

Public Declare Function RSA_PublicKeyFromPrivate Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strKeyString As String, ByVal nOptions As Long) As Long

nRet = RSA_PublicKeyFromPrivate(strOutput, nOutChars, strKeyString, nOptions) As Long

C/C++ Syntax

long __stdcall RSA_PublicKeyFromPrivate(char *szOutput, long nOutChars, const char *szKeyString, long nOptions);

Parameters

szOutput
[out] to receive public key data in encoded "internal" format.
nOutChars
[in] specifying the maximum number of characters to be received.
szKeyString
[in] containing a private key in "internal" format.
nOptions
[in] option flags: 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 rsaPublicKeyFromPrivate (szKeyString As String, Optional nOptions As Long = 0) As String

.NET Equivalent

Rsa.PublicKeyFromPrivate Method

C++ (STL) Equivalent

static std::string dipki::Rsa::PublicKeyFromPrivate (const std::string &keyStr)

Python Equivalent

static Rsa.publickey_from_private(intkeystr)

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.

Use this function if you need a public key string but only have the corresponding private key file. The format used to store RSA private keys contains both the public and private components (that is, n and e are present in both) so we can obtain the public key from its corresponding private key. Note that internal key strings are only valid for the current session.

Example (VBA core function)

This example reads in a private key to an internal private key string, displays some information about the key, then converts it to a public key string and displays its properties.

Dim strPriKeyFile As String
Dim strPrivateKey As String
Dim strPublicKey As String
Dim nChars As Long
Dim nCode As Long
Dim nRet As Long

' Read private key from encrypted private key file into internal string form
strPriKeyFile = "BobPrivRSAEncrypt.p8e"
strPrivateKey = rsaReadPrivateKey(strPriKeyFile, "password")
If Len(strPrivateKey) = 0 Then Exit Sub     'Catch error here
' Display some info about it
Debug.Print "Private key length = " & RSA_KeyBits(strPrivateKey) & " bits"
nCode = RSA_KeyHashCode(strPrivateKey)
Debug.Print "KeyHashCode=" & Hex(nCode)
nRet = RSA_CheckKey(strPrivateKey, 0)
Debug.Print "RSA_CheckKey returns " & nRet & ": (PKI_VALID_PRIVATEKEY=" & PKI_VALID_PRIVATEKEY & ")"

' Convert to public key string
nChars = RSA_PublicKeyFromPrivate("", 0, strPrivateKey, 0)
If nChars <= 0 Then Exit Sub    ' Catch error here
strPublicKey = String(nChars, " ")
nChars = RSA_PublicKeyFromPrivate(strPublicKey, Len(strPublicKey), strPrivateKey, 0)
 ' Display some info about it
Debug.Print "Public key length = " & RSA_KeyBits(strPublicKey) & " bits"
nCode = RSA_KeyHashCode(strPublicKey)
Debug.Print "KeyHashCode=" & Hex(nCode)
nRet = RSA_CheckKey(strPublicKey, 0)
Debug.Print "RSA_CheckKey returns " & nRet & ": (PKI_VALID_PUBLICKEY=" & PKI_VALID_PUBLICKEY & ")"

' Clean up
strPrivateKey = wipeString(strPrivateKey)

Note that the KeyHashCodes and bit lengths are the same.

Private key length = 1024 bits
KeyHashCode=6BCC120C
RSA_CheckKey returns 0: (PKI_VALID_PRIVATEKEY=0)
Public key length = 1024 bits
KeyHashCode=6BCC120C
RSA_CheckKey returns 1: (PKI_VALID_PUBLICKEY=1)

Example (VBA wrapper function)

Dim strPrivateKey As String
Dim strPublicKey As String
' Read in private key to internal string
strPrivateKey = rsaReadPrivateKey("BobPrivRSAEncrypt.p8e", "password")
Debug.Assert Len(strPrivateKey) > 0
Debug.Print "Private key length = " & RSA_KeyBits(strPrivateKey) & " bits"
Debug.Print "KeyHashCode=0x" & Hex(RSA_KeyHashCode(strPrivateKey))
' Convert to public key string
strPublicKey = rsaPublicKeyFromPrivate(strPrivateKey)
Debug.Print "Public key length = " & RSA_KeyBits(strPublicKey) & " bits"
Debug.Print "KeyHashCode=0x" & Hex(RSA_KeyHashCode(strPublicKey))

See Also

RSA_SavePublicKey RSA_ReadEncPrivateKey

[Contents] [Index]

[PREV: RSA_MakeKeysXtd...]   [Contents]   [Index]   
   [NEXT: RSA_RawPrivate...]

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