question

osvbnet avatar image
0 Votes"
osvbnet asked JiachenLiMFST-9349 edited

Export Problem :(

Hello,
I'm not sure if I'm doing this right, since cannot export correctly:

Dim MyStore As X509Store = New X509Store(CertificateStoreName, StoreLocation.CurrentUser)
MyStore.Open(OpenFlags.ReadWrite)
Dim MyCertificate As X509Certificate2Collection = MyStore.Certificates.Find(X509FindType.FindByThumbprint, CertificateThumbprint, False)
If MyCertificate.Count > 0 Then
File.WriteAllBytes(FileTextBox.Text, MyCertificate(0).Export(X509ContentType.Pfx, PasswordTextBox.Text))
'OR
File.WriteAllBytes(FileTextBox.Text, MyCertificate(0).Export(X509ContentType.Pkcs7))
End If
MyStore.Close()

  1. If I export as .Pfx my exported cert will not contain the private key at all, can't figure it out.

  2. If I export as .Pkcs7 (.p7b ext) I always get Invalid content type error (export as .Cert works fine)

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JiachenLiMFST-9349 avatar image
0 Votes"
JiachenLiMFST-9349 answered JiachenLiMFST-9349 edited

Hi @osvbnet ,
The following link might be helpful.
X509Certificate2.Export(X509ContentType, String) Method
The contentType parameter accepts only the following values of the X509ContentType enumeration: Cert, SerializedCert, and Pkcs12. Passing any other value causes a CryptographicException to be thrown.
Best Regards.
Jiachen Li


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hey thx, yeah I found it, so why the other values are there?! Does not make sense! :(

0 Votes 0 ·

Hi @osvbnet ,
https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509contenttype?view=xamarinios-10.8#remarks

Content types Cert, Pkcs12, and SerializedCert can be exported as byte arrays.
And other values are used elsewhere.

0 Votes 0 ·