CryptoSys PKI examples VB6 to VB.NET

RSA_ReadPrivateKeyInfo

Reads private key string from an (unencrypted) PrivateKeyInfo file.

VB6/VBA

Debug.Print "Testing RSA_ReadPrivateKeyInfo ..."
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.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

Output

Testing RSA_ReadPrivateKeyInfo ...
RSA_SaveEncPrivateKey returns 0
6BCC120C
6BCC120C
Key string values match.

VB.NET

Console.WriteLine("Testing RSA_ReadPrivateKeyInfo ...")
Dim strPriFile As String
Dim strEPKFile As String
Dim strPrivateKey As String
Dim strPK1 As String
Dim nRet As Integer

' Read in Bob's unencrypted PrivateKeyInfo data
strPriFile = "BobPrivRSAEncrypt.pri"
strPrivateKey = Rsa.ReadPrivateKeyInfo(strPriFile).ToString()
If strPrivateKey.Length = 0 Then
    Console.WriteLine("Failed to read Private Key file")
    Exit Sub
End If
' Now we save it with a password
strEPKFile = "BobPrivRSAEncrypt.epk"
nRet = Rsa.SaveEncPrivateKey(strEPKFile, strPrivateKey, 1000, "password", 0, 0)
Console.WriteLine("RSA_SaveEncPrivateKey returns " & nRet)

' Check we can read it
strPK1 = Rsa.ReadEncPrivateKey(strEPKFile, "password").ToString()

' To compare these strings, use the RSA_KeyHashCode function
Console.WriteLine("{0,8:X}", Rsa.KeyHashCode(strPK1))
Console.WriteLine("{0,8:X}", Rsa.KeyHashCode(strPrivateKey))
If Rsa.KeyHashCode(strPK1) = Rsa.KeyHashCode(strPrivateKey) Then
    Console.WriteLine("Key string values match.")
Else
    Console.WriteLine("ERROR: key strings do not match.")
End If

[Contents]

[HOME]   [NEXT: RSA_SaveEncPrivateKey...]

Copyright © 2010-20 D.I. Management Services Pty Ltd. All rights reserved.