Verifies that a pair of "internal" RSA private and public key strings are matched.
Public Declare Function RSA_KeyMatch Lib "diCrPKI.dll"
(ByVal strPrivateKey As String, ByVal strPublicKey As String) As Long
nRet = RSA_KeyMatch(strPrivateKey, strPublicKey)
long __stdcall RSA_KeyMatch(const char *szPrivateKey, const char *szPublicKey);
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.
Rsa.KeyMatch Method (String)
Rsa.KeyMatch Method (StringBuilder)
static bool dipki::Rsa::KeyMatch (const std::string &priKeyStr, const std::string &pubKeyStr)
static Rsa.key_match(prikeystr, pubkeystr)
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.
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.