CryptoSys PKI Toolkit Manual

RSA_PublicKeyFromPrivate

Converts an internal private key string into a public one. [New in v3.8]

VB6/VBA 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

Parameters

strOutput
[out] String to receive public key data in encoded "internal" format.
nOutChars
[in] Long specifying the maximum number of characters to be received.
strKeyString
[in] String containing a private key in "internal" format.
nOptions
[in] Long option flags: not used in this release. Specify zero.

C/C++ Syntax

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

Returns (VB6/C)

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

.NET Equivalent

Rsa.PublicKeyFromPrivate Method

Remarks

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. Call the function with an empty or NULL strOutput string or zero nOutChars parameter to find out the required length of the output string (Hint: in this case it will be shorter than the input key string). C/C++ users should add one to this value when allocating memory. Note that internal key strings are only valid for the current session.

Example

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.epk"
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)

See Also

RSA_SavePublicKey RSA_ReadEncPrivateKey

[Contents] [Index]

[HOME]   [NEXT: RSA_RawPrivate...]

Copyright © 2004-12 D.I. Management Services Pty Ltd. All rights reserved.