RSA_ReadPrivateKeyInfo reads from an (unencrypted) PKCS-8 private key info file into an "internal" private key string.
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
String to receive private key data in "internal" encoded format.Long specifying the maximum number of characters to be received.String specifying the filename of a
PrivateKeyInfo file (or a string containing the data in PEM format).Long option flags: not used in this release. Specify zero.
long _stdcall RSA_ReadPrivateKeyInfo(char *szOutput, long nOutChars, const char *szKeyFile, long nOptions);
Long: If successful, the return value is the number of characters in the output string;
otherwise it returns a negative error code.
Only PKCS-8 PrivateKeyInfo files
specifying the rsaEncryption algorithm are supported.
The file must be either in a binary BER-encoded format or PEM format.
Call the function with an empty or NULL strPrivateKey string to find out the required length of
the string. C/C++ users should add one to this value before allocating memory.
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 = "C:\Test\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 = "C:\Test\BobPrivRSAEncrypt.epk" 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
RSA_ReadEncPrivateKey RSA_KeyHashCode