Saves a private key string to a PKCS-8 EncryptedPrivateKeyInfo file.
VB6/VBA
Debug.Print "Testing RSA_SaveEncPrivateKey ..." Dim strPriFile As String Dim strEPKFile As String Dim strPrivateKey As String Dim strPK1 As String Dim nChars As String Dim nRet As Long strPriFile = "CarlPrivRSASign.pri" ' Read in Carl's unencrypted PrivateKeyInfo data nChars = RSA_ReadPrivateKeyInfo("", 0, strPriFile, 0) If nChars <= 0 Then MsgBox "Failed to read Private Key file" Exit Sub End If ' Dimension the string to receive it - IMPORTANT strPrivateKey = String(nChars, " ") ' Read in as an "internal" key string nRet = RSA_ReadPrivateKeyInfo(strPrivateKey, nChars, strPriFile, 0) If nRet <= 0 Then MsgBox "Failed to read Private Key file" Exit Sub End If Debug.Print "Private key length is " & RSA_KeyBits(strPrivateKey) & " bits" ' Now save it in PKCS#8 encrypted form with a password strEPKFile = "CarlPrivRSASign.epk" nRet = RSA_SaveEncPrivateKey(strEPKFile, strPrivateKey, 1000, "password", 0) Debug.Print "RSA_SaveEncPrivateKey returns " & nRet & " (expected 0)" ' Check we can read it (note easier wrapper function) strPK1 = rsaReadPrivateKey(strEPKFile, "password") If Len(strPK1) > 0 Then Debug.Print "Encrypted private key is " & RSA_KeyBits(strPK1) & " bits" Else MsgBox "Unable to read encrypted private key" End If ' To compare these strings, use the RSA_KeyHashCode function Debug.Print "HashCode(original prikeyinfo) =" & Hex(RSA_KeyHashCode(strPrivateKey)) Debug.Print "HashCode(encrypted prikeyinfo)=" & Hex(RSA_KeyHashCode(strPK1)) If RSA_KeyHashCode(strPK1) = RSA_KeyHashCode(strPrivateKey) Then Debug.Print "OK, Key string values match." Else Debug.Print "ERROR: key strings do not match." End If
Output
Testing RSA_SaveEncPrivateKey ... Private key length is 1024 bits RSA_SaveEncPrivateKey returns 0 (expected 0) Encrypted private key is 1024 bits HashCode(original prikeyinfo) =A937B1B5 HashCode(encrypted prikeyinfo)=A937B1B5 OK, Key string values match.
VB.NET
Console.WriteLine("Testing RSA_SaveEncPrivateKey ...")
Dim strPriFile As String
Dim strEPKFile As String
Dim strPrivateKey As String
Dim strPK1 As String
Dim nRet As Integer
strPriFile = "CarlPrivRSASign.pri"
' Read in Carl's unencrypted PrivateKeyInfo data
strPrivateKey = Rsa.ReadPrivateKeyInfo(strPriFile).ToString()
If strPrivateKey.Length = 0 Then
Console.WriteLine("Failed to read Private Key file")
Exit Sub
End If
Console.WriteLine("Private key length is " & Rsa.KeyBits(strPrivateKey) & " bits")
' Now save it in PKCS#8 encrypted form with a password
strEPKFile = "CarlPrivRSASign.epk"
nRet = Rsa.SaveEncPrivateKey(strEPKFile, strPrivateKey, 1000, "password", 0, 0)
Console.WriteLine("Rsa.SaveEncPrivateKey returns " & nRet & " (expected 0)")
' Check we can read it
strPK1 = Rsa.ReadEncPrivateKey(strEPKFile, "password").ToString()
If strPK1.Length > 0 Then
Console.WriteLine("Encrypted private key is " & Rsa.KeyBits(strPK1) & " bits")
Else
Console.WriteLine("Unable to read encrypted private key")
End If
' To compare these strings, use the RSA_KeyHashCode function
Console.WriteLine("HashCode(original prikeyinfo) ={0,8:X}", Rsa.KeyHashCode(strPrivateKey))
Console.WriteLine("HashCode(encrypted prikeyinfo)={0,8:X}", Rsa.KeyHashCode(strPK1))
If Rsa.KeyHashCode(strPK1) = Rsa.KeyHashCode(strPrivateKey) Then
Console.WriteLine("OK, Key string values match.")
Else
Console.WriteLine("ERROR: key strings do not match.")
End If
[Contents]