'Cannot find the requested object' exception while creating X509Certificate2 from string

前端 未结 2 1069
时光取名叫无心
时光取名叫无心 2021-01-11 12:36

I am trying to create X509Certificate2 from string. Let me show an example:

string keyBase64String = Convert.ToBase64String(file.PKCS7);
var cer         


        
2条回答
  •  一整个雨季
    2021-01-11 13:05

    If file.PKCS7 represents a PKCS#7 SignedData blob (what gets produced from X509Certificate2.Export(X509ContentType.Pkcs7) or X509Certificate2Collection.Export(X509ContentType.Pkcs7)) then there are two different ways of opening it:

    • new X509Certificate2(byte[])/new X509Certificate2(string)
      • The single certificate constructor will extract the signing certificate of the SignedData blob. If this was just being exported as a collection of certs, but not signing anything, there is no such certificate, and so it fails with Cannot find the original signer. (Win 2012r2, other versions could map it to a different string)
    • X509Certificate2Collection::Import(byte[])/X509Certificate2Collection::Import(string)
      • The collection import will consume all of the "extra" certificates, ignoring the signing certificate.

    So if it's really PKCS#7 you likely want the collection Import (instance) method. If it isn't, you have some odd variable/field/property names.

提交回复
热议问题