The public key parameters for the RSA algorithm are {e, n}
, the exponent and the modulus. In .NET, these are available from the RSAParameters
struct. The other fields represent the private key.
So, to compare an X509Certificate2
and an RSACryptoServiceProvider
for public key equality, you can just grab these parameters:
AsymmetricAlgorithm signingKey;
bool signatureIsVerified = signedXml.CheckSignatureReturningKey(out signingKey);
var certificateParameters =
((RSA)certificate.PublicKey.Key).ExportParameters(
includePrivateParameters: false);
var signingParameters = signingKey.ExportParameters(
includePrivateParameters: false);
bool areEqual =
ByteArrayEquals(certificateParameters.Exponent,
signingParameters.Exponent)
&& ByteArrayEquals(certificateParameters.Modulus,
signingParameters.Modulus);
You'll have to implement ByteArrayEquals
, because there's no good way to do it in .NET.
If you're using DSA rather than RSA, the public key is made up of {p, q, g, y}
.