[VB6 equivalent: 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
See Also:
Rsa.SaveEncPrivateKey Method (String, String, Int32, String, CipherAlgorithm, HashAlgorithm, Rsa.Format)
Rsa.SaveEncPrivateKey Method (String, String, Int32, String, Rsa.PbeOptions, Rsa.Format)