CryptoSys PKI examples VB6 to VB.NET

RSA_KeyMatch

Verifies that a pair of private and public key strings are matched.

VB6/VBA

Debug.Print "Testing RSA_KeyMatch ..."
Dim strCertFile As String
Dim strKeyFile As String
Dim strPassword As String
Dim strPublicKey As String
Dim strPrivateKey As String
Dim nRet As Long

' Input files
strCertFile = "AAA010101AAAsd.cer"
strKeyFile = "AAA010101AAA_0408021316S.key"
' Test password - CAUTION: DO NOT hardcode production passwords!
strPassword = "Empresa1"

' Read in private key from encrypted .key file
strPrivateKey = rsaReadPrivateKey(strKeyFile, strPassword)
If Len(strPrivateKey) > 0 Then
    Debug.Print "Private key is " & RSA_KeyBits(strPrivateKey) & " bits"
Else
    Debug.Print "ERROR: Cannot read private key file."
    Exit Sub
End If

' Clean up password as we are done with it
strPassword = wipeString(strPassword)

' Read in public key from certificate
strPublicKey = rsaGetPublicKeyFromCert(strCertFile)
If Len(strPublicKey) > 0 Then
    Debug.Print "Public key is " & RSA_KeyBits(strPublicKey) & " bits"
Else
    Debug.Print "ERROR: Cannot read certificate file."
    Exit Sub
End If

' See if the two key strings match
nRet = RSA_KeyMatch(strPrivateKey, strPublicKey)
If nRet = 0 Then
    Debug.Print "OK, key strings match."
Else
    Debug.Print "FAILED: key strings do not match."
End If

' Clean up private key string
strPrivateKey = wipeString(strPrivateKey)

Output

Testing RSA_KeyMatch ...
Private key is 1024 bits
Public key is 1024 bits
OK, key strings match.

VB.NET

Console.WriteLine("Testing RSA_KeyMatch ...")
Dim strCertFile As String
Dim strKeyFile As String
Dim sbPassword As StringBuilder
Dim sbPublicKey As StringBuilder
Dim sbPrivateKey As StringBuilder
Dim nRet As Integer

' Input files
strCertFile = "AAA010101AAAsd.cer"
strKeyFile = "AAA010101AAA_0408021316S.key"
' Test password - CAUTION: DO NOT hardcode production passwords!
sbPassword = New StringBuilder("Empresa1")

' Read in private key from encrypted .key file
sbPrivateKey = Rsa.ReadEncPrivateKey(strKeyFile, sbPassword.ToString())
If sbPrivateKey.Length > 0 Then
    Console.WriteLine("Private key is " & Rsa.KeyBits(sbPrivateKey.ToString()) & " bits")
Else
    Console.WriteLine("ERROR: Cannot read private key file.")
    Exit Sub
End If

' Clean up password as we are done with it
Wipe.String(sbPassword)

' Read in public key from certificate
sbPublicKey = Rsa.GetPublicKeyFromCert(strCertFile)
If sbPublicKey.Length > 0 Then
    Console.WriteLine("Public key is " & Rsa.KeyBits(sbPublicKey.ToString()) & " bits")
Else
    Console.WriteLine("ERROR: Cannot read certificate file.")
    Exit Sub
End If

' See if the two key strings match
nRet = Rsa.KeyMatch(sbPrivateKey.ToString(), sbPublicKey.ToString())
If nRet = 0 Then
    Console.WriteLine("OK, key strings match.")
Else
    Console.WriteLine("FAILED: key strings do not match.")
End If

' Clean up private key string
Wipe.String(sbPrivateKey)

[Contents]

[HOME]   [NEXT: RSA_MakeKeys...]

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