Saves a private key string to an (unencrypted) PKCS-8 PrivateKeyInfo file.
VB6/VBA
Debug.Print "Testing RSA_SavePrivateKeyInfo ..." Dim strEPKFile As String Dim strPriFile As String Dim strPEMFile As String Dim strPassword As String Dim strPrivateKey As String Dim nRet As Long strEPKFile = "rsa508.epk" strPriFile = "rsa508.pri" strPEMFile = "rsa508.pem" strPassword = "password" ' Read in the deciphered private key string strPrivateKey = rsaReadPrivateKey(strEPKFile, strPassword) If Len(strPrivateKey) = 0 Then MsgBox "Unable to retrieve private key" Exit Sub End If Debug.Print "Key size=" & RSA_KeyBits(strPrivateKey) & " bits" ' Save as unencrypted PrivateKeyInfo file nRet = RSA_SavePrivateKeyInfo(strPriFile, strPrivateKey, 0) Debug.Print "RSA_SavePrivateKeyInfo returns " & nRet ' Save as unencrypted PEM-format file nRet = RSA_SavePrivateKeyInfo(strPEMFile, strPrivateKey, PKI_KEY_FORMAT_PEM) Debug.Print "RSA_SavePrivateKeyInfo returns " & nRet
Output
Testing RSA_SavePrivateKeyInfo ... Key size=508 bits RSA_SavePrivateKeyInfo returns 0 RSA_SavePrivateKeyInfo returns 0
VB.NET
Console.WriteLine("Testing RSA_SavePrivateKeyInfo ...")
Dim strEPKFile As String
Dim strPriFile As String
Dim strPEMFile As String
Dim strPassword As String
Dim strPrivateKey As String
Dim nRet As Integer
strEPKFile = "rsa508.epk"
strPriFile = "rsa508.pri"
strPEMFile = "rsa508.pem"
strPassword = "password"
' Read in the deciphered private key string
strPrivateKey = Rsa.ReadEncPrivateKey(strEPKFile, strPassword).ToString()
If strPrivateKey.Length = 0 Then
Console.WriteLine("Unable to retrieve private key")
Exit Sub
End If
Console.WriteLine("Key size=" & Rsa.KeyBits(strPrivateKey) & " bits")
' Save as unencrypted PrivateKeyInfo file
nRet = Rsa.SavePrivateKeyInfo(strPriFile, strPrivateKey, Rsa.Format.Binary)
Console.WriteLine("Rsa.SavePrivateKeyInfo returns " & nRet)
' Save as unencrypted PEM-format file
nRet = Rsa.SavePrivateKeyInfo(strPriFile, strPrivateKey, Rsa.Format.PEM)
Console.WriteLine("Rsa.SavePrivateKeyInfo returns " & nRet)
[Contents]