CryptoSys PKI Pro Manual

RSA_KeyMatch

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

VBA/VB6 Syntax

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

nRet = RSA_KeyMatch(strPrivateKey, strPublicKey)

C/C++ Syntax

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

Parameters

szPrivateKey
[in] containing an "internal" RSA private key string.
szPublicKey
[in] containing an "internal" RSA public key string.

Returns (VBA/C)

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 nonzero error code.

.NET Equivalent

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

C++ (STL) Equivalent

static bool dipki::Rsa::KeyMatch (const std::string &priKeyStr, const std::string &pubKeyStr)

Python Equivalent

static Rsa.key_match(prikeystr, pubkeystr)

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]

[PREV: RSA_KeyHashCode...]   [Contents]   [Index]   
   [NEXT: RSA_KeyValue...]

Copyright © 2004-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-09-23T07:52:09Z.