Converts an internal RSA private key string into an internal public key string.
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
long __stdcall RSA_PublicKeyFromPrivate(char *szOutput, long nOutChars, const char *szKeyString, 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 rsaPublicKeyFromPrivate
(szKeyString As String, Optional nOptions As Long = 0) As String
Rsa.PublicKeyFromPrivate Method
static std::string dipki::Rsa::PublicKeyFromPrivate (const std::string &keyStr)
static Rsa.publickey_from_private(intkeystr)
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.
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)
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))
RSA_SavePublicKey RSA_ReadEncPrivateKey