CryptoSys PKI Toolkit Manual

RSA_KeyMatch

RSA_KeyMatch verifies that a pair of "internal" RSA private and public key strings are matched.

VB6/VBA Syntax

Public Declare Function RSA_KeyMatch Lib "diCrPKI.dll" (ByVal strPrivateKey As String, ByVal strPublicKey As String) As Long

nRet = RSA_KeyMatch(strPrivateKey, strPublicKey)

Parameters

strPrivateKey
[in] String containing an "internal" RSA private key string.
strPublicKey
[in] String containing an "internal" RSA public key string.

C/C++ Syntax

long _stdcall RSA_KeyMatch(const char *szPrivateKey, const char *szPublicKey);

Returns (VB6/C)

Long: If the pair of private and public keys match, the return value is zero (0); if the key strings are valid but not matched, the return value is NO_MATCH_ERROR (-21). If an error occurs, it returns a non-zero error code.

.NET Equivalent

Rsa.KeyMatch Method (String)
Rsa.KeyMatch Method (StringBuilder)

Remarks

This function allows you to check that a private key file is matched with the public key in an X.509 certificate. You must read the keys into "internal" key strings before comparing. Note that the return value for success is zero.

Example

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)

Using the sample Mexican Government SAT files, the output is as follows:

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

See Also

RSA_KeyHashCode

[Contents] [Index]

[HOME]   [NEXT: RSA_MakeKeys...]

Copyright © 2004-9 D.I. Management Services Pty Ltd. All rights reserved.