Reads a private key directly from an encrypted PFX/PKCS-12 file into an "internal" private key string. [New in v3.8]
Public Declare Function RSA_ReadPrivateKeyFromPFX Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strPfxFile As String, ByVal strPassword As String, ByVal nOptions As Long) As Long
nRet = RSA_ReadPrivateKeyFromPFX(strOutput, nOutChars, strPfxFile, strPassword, nOptions) As Long
String to receive private key data in "internal" encoded format.Long specifying the maximum number of characters to be received.String containing the PFX filename
(or a string containing the data in PEM format)
String containing the password.Long option flags: not used in this release. Specify zero.
long _stdcall RSA_ReadPrivateKeyFromPFX(char *szOutput, long nOutChars, const char *szPfxFile, const char *szPassword, long nOptions);
Long: If successful, the return value is the number of characters in the output string;
otherwise it returns a negative error code.
Rsa.ReadPrivateKeyFromPFX Method
This will read the private key from
the first pkcs8ShroudedKeyBag object it finds and can decrypt in the PFX file.
The result is a string in "internal" key string format valid only for the current session.
Call the function with an empty or NULL strOutput string or zero nOutChars parameter to find out the required length
of the output string. C/C++ users should add one to this value when allocating memory.
If you need the public key instead from a PFX file, then use this function followed by
RSA_PublicKeyFromPrivate.
If you just want to extract the encrypted private key and save directly as a pkcs-8 file, then use the
RSA_GetPrivateKeyFromPFX function.
(We've tried to use the convention "Get" to mean extract-and-save-as-a-file and "Read" to mean read-into-internal-string,
which we've been consistent with
except for the RSA_GetPublicKeyFromCert function which should have been RSA_ReadPublicKeyFromCert. Sorry.)
Dim strPfxFile As String Dim strPrivateKey As String Dim strPassword As String Dim nChars As Long Dim nCode As Long Dim nRet As Long strPfxFile = "bob.pfx" strPassword = "password" ' Read private key from PFX file into internal string form nChars = RSA_ReadPrivateKeyFromPFX("", 0, strPfxFile, strPassword, 0) If nChars <= 0 Then Exit Sub ' Catch error here strPrivateKey = String(nChars, " ") nChars = RSA_ReadPrivateKeyFromPFX(strPrivateKey, Len(strPrivateKey), strPfxFile, strPassword, 0) ' 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 & ")" ' Clean up strPrivateKey = wipeString(strPrivateKey) strPassword = wipeString(strPassword)
Private key length = 1024 bits KeyHashCode=6BCC120C RSA_CheckKey returns 0: (PKI_VALID_PRIVATEKEY=0)
RSA_GetPrivateKeyFromPFX RSA_ReadEncPrivateKey RSA_PublicKeyFromPrivate