如何比较 .NET 中的公钥?
我有一个 X509Certificate2
包含公钥。我有一个
RSACryptoServiceProvider
(来自调用 SignedXml.CheckSignatureReturningKey
),还包含公钥。
我想知道其中一个是否来自另一个。我如何比较两者?
I've got an X509Certificate2
containing a public key. I've got an RSACryptoServiceProvider
(which came from calling SignedXml.CheckSignatureReturningKey
), also containing a public key.
I want to find out if one came from the other. How can I compare the two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以比较 PublicKey 中签名证书的属性href="https://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(System.Security.Cryptography.Xml.SignedXml.KeyInfo);k(KeyInfo) ;k(TargetFrameworkMoniker-.NETFramework,版本%3Dv4.5.2);k(DevLang-csharp)&rd=true" rel="nofollow">SignedXml.KeyIfo 带有来自 SignedXml.CheckSignatureReturningKey。这个 C# 扩展方法为我完成了这项工作:
像这样使用它:
You can compare the PublicKey property of signing certificates in the SignedXml.KeyIfo with signing key output from SignedXml.CheckSignatureReturningKey. This C# extension method does the job for me:
Use it like this:
RSA 算法的公钥参数是
{e, n}
、指数和模数。在 .NET 中,可以从RSAParameters
结构。其他字段代表私钥。因此,要比较
X509Certificate2
和RSACryptoServiceProvider
的公钥相等性,您只需获取以下参数:您必须实现
ByteArrayEquals
,因为在 .NET 中没有好的方法。如果您使用 DSA 而不是 RSA,则公钥由
{p, q, g, y}
组成。The public key parameters for the RSA algorithm are
{e, n}
, the exponent and the modulus. In .NET, these are available from theRSAParameters
struct. The other fields represent the private key.So, to compare an
X509Certificate2
and anRSACryptoServiceProvider
for public key equality, you can just grab these parameters: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}
.