CryptoSys PKI Pro Manual

RSA_ReadPrivateKeyFromPFX

Reads a private key directly from an encrypted PFX/PKCS-12 file into an "internal" private key string. [Superseded by RSA_ReadAnyPrivateKey.]

VBA/VB6 Syntax

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

C/C++ Syntax

long __stdcall RSA_ReadPrivateKeyFromPFX(char *szOutput, long nOutChars, const char *szPfxFile, const char *szPassword, long nOptions);

Parameters

szOutput
[out] to receive private key data in "internal" encoded format.
nOutChars
[in] specifying the maximum number of characters to be received.
szPfxFile
[in] containing the PFX filename (or a string containing the PFX data in PEM format)
szPassword
[in] containing the password.
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.

.NET Equivalent

Rsa.ReadPrivateKeyFromPFX Method

Remarks

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.

Example

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)

See Also

RSA_GetPrivateKeyFromPFX RSA_ReadEncPrivateKey RSA_PublicKeyFromPrivate

[Contents] [Index]

[PREV: RSA_ReadEncPrivateKey...]   [Contents]   [Index]   
   [NEXT: RSA_ReadPrivateKeyInfo...]

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