How to check if private/public key pair match using (.NET / BouncyCastle)?
The simplest way to check whether a private key and a public key match is to encrypt a piece of data with the public key and see if you can decrypt it with the private key - or alternatively to sign a piece of data with the private key and see if you can verify it with the public key.
If the keys are RSA keys, you can cast the public key to Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters
and the private key to Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters
and verify that the Modulus
is the same and that Exponent
of the public key is equal to PublicExponent
of the private key. If you want to get really fancy, you could also validate all the remaining parameters of the private key (follow PKCS#1 section 3.2).