CryptoSys PKI Pro Manual

RSA_ReadPrivateKeyInfo

Reads from an (unencrypted) PKCS-8 private key info file into an "internal" private key string. [Superseded by RSA_ReadAnyPrivateKey.]

VBA/VB6 Syntax

Public Declare Function RSA_ReadPrivateKeyInfo Lib "diCrPKI.dll" (ByVal strPrivateKey As String, ByVal nOutChars As Long, ByVal strPriFileName As String, ByVal nOptions As Long) As Long

nRet = RSA_ReadPrivateKeyInfo(strPrivateKey, nOutChars, strPriFileName, nOptions) As Long

C/C++ Syntax

long __stdcall RSA_ReadPrivateKeyInfo(char *szOutput, long nOutChars, const char *szKeyFile, 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.
szKeyFile
[in] specifying the filename of a PrivateKeyInfo file (or a string containing the key in PEM format).
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.ReadPrivateKeyInfo Method

Remarks

Only PKCS-8 PrivateKeyInfo files specifying the rsaEncryption algorithm are supported together with OpenSSL-compatible RSA PRIVATE KEY files. Files may be in either binary BER/DER-encoded format or PEM format. 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.

Example

This example reads in Bob's unencrypted private key from the file BobPrivRSAEncrypt.pri from [SMIME-EX] and saves in encrypted form using the password "password". The default PBE algorithm pbeWithSHAAnd3-KeyTripleDES-CBC is used with an iteration count of 1000.

Dim strPRIFile As String
Dim strEPKFile As String
Dim strPrivateKey As String
Dim strPK1 As String
Dim nKeyLen As String
Dim nRet As Long

' Read in Bob's unencrypted PrivateKeyInfo data
strPRIFile = "BobPrivRSAEncrypt.pri"
nKeyLen = RSA_ReadPrivateKeyInfo("", 0, strPRIFile, 0)
If nKeyLen <= 0 Then
    MsgBox "Failed to read Private Key file"
    Exit Sub
End If
strPrivateKey = String(nKeyLen, " ")
nRet = RSA_ReadPrivateKeyInfo(strPrivateKey, nKeyLen, strPRIFile, 0)
If nRet <= 0 Then
    MsgBox "Failed to read Private Key file"
    Exit Sub
End If
' Now we save it with a password
strEPKFile = "BobPrivRSAEncrypt.p8e"
nRet = RSA_SaveEncPrivateKey(strEPKFile, strPrivateKey, 1000, "password", 0)
Debug.Print "RSA_SaveEncPrivateKey returns " & nRet

' Check we can read it
strPK1 = rsaReadPrivateKey(strEPKFile, "password")

' Sneak a look at the two key strings.
' CAUTION: _Never_ print these in a production system!
Debug.Print strPK1
Debug.Print strPrivateKey

' To compare these strings, use the RSA_KeyHashCode function
Debug.Print Hex(RSA_KeyHashCode(strPK1))
Debug.Print Hex(RSA_KeyHashCode(strPrivateKey))
If RSA_KeyHashCode(strPK1) = RSA_KeyHashCode(strPrivateKey) Then
    Debug.Print "Key string values match."
Else
    Debug.Print "ERROR: key strings do not match."
End If

See Also

RSA_ReadEncPrivateKey RSA_KeyHashCode

[Contents] [Index]

[PREV: RSA_ReadPrivateKeyFromPFX...]   [Contents]   [Index]   
   [NEXT: RSA_ReadPublicKey...]

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