Reads a private key directly from an encrypted PFX/PKCS-12 file into an "internal" private key string. [Superseded by RSA_ReadAnyPrivateKey
.]
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
long __stdcall RSA_ReadPrivateKeyFromPFX(char *szOutput, long nOutChars, const char *szPfxFile, const char *szPassword, 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.
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 szOutput 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.
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