为什么PFX的public键在签名pdf文档时不一样?

Why PFX's public key is different when sign pdf document?

使用我创建的证书,我使用 iTextSharp 库签署了一个 PDF 文档。

当我在 pdf 签名面板中看到 public 签名时,我在 hexa

中有以下信息

但是,当我通过我在 c#

中的代码从同一个证书中获取 public 密钥时
    var certificate = new X509Certificate2(@"C:\mycert.pfx", "pass");
    byte[] publicKey = certificate.PublicKey.EncodedKeyValue.RawData;

十进制(转换为十六进制)的结果不同。

例如在字节数组中,5 在 Hexa 中是 130 - 82,但在 pdf 中,第 6 个键在 hexa

中是 0D

我做错了什么?

谢谢,对不起我的英语。

就差异的原因而言,您的问题与 重复;答案解释:

The difference is that Adobe presents the hex dump of the complete SubjectPublicKeyInfo object (the public key including the algorithm information and the key value) while your code only dumps the RSAPublicKey (the key itself).

...

有关详细信息,请阅读此处。

您实际上可以识别出 Adob​​e Reader 输出中的纯 public 键,它从第一行的末尾开始,

[...] 30 82 02

0A 02 82 02 01 00 FA 8B ...


不过,解决方案略有不同,因为该问题中的代码在 Java。

您的代码使用了 System.Security.Cryptography.X509Certificates class X509Certificate2As far as I can see 这个 class 不会暴露整个 SubjectPublicKeyInfo 本身。因此你应该在这里使用不同的class。

一个明显的选择是 BouncyCastle classes,X509CertificateStructureX509CertificateHolder,具体取决于您使用的 BouncyCastle 版本(旧版本的 BouncyCastle 包含在 iTextSharp 中,较新的可以从他们的网站上检索)。这些 classes 公开了 SubjectPublicKeyInfo.