'How to export X509Certificate2 from store to .pfx .p7b

I'm having a hard time in need of certificate export. Export of 3 extensions needed: .cer .pfx .p7b It seems that .cer export is OK, but .pfx will not export private key and .p7b will not work at all with error "Invalid content type" Not expert at this field, any help please? :(

Dim varStore As X509Store = New X509Store("MY", StoreLocation.CurrentUser)
varStore.Open(OpenFlags.ReadWrite)
Dim varCert As X509Certificate2Collection = varStore.Certificates.Find(X509FindType.FindByThumbprint, varThumbCert, False)
If varCert.Count > 0 Then
   Select Case varEXT
    Case ".pfx"
     File.WriteAllBytes(FileTextBox.Text, varCert(0).Export(X509ContentType.Pfx, PasswordTextBox.Text))
    Case ".p7b"
     File.WriteAllBytes(FileTextBoxX.Text, varCert(0).Export(X509ContentType.Pkcs7))
    Case ".cer"
     File.WriteAllBytes(FileTextBoxX.Text, varCert(0).Export(X509ContentType.Cert))
End If
varStore.Close()


Solution 1:[1]

Let's say you have your question and answers in a dictionary called qa you can use the random module and choice function to chose keys at random, In code, that would look like

import random
question = random.choice(list(qa.keys()))
answer = qa.pop(question)

Pop will return the value corresponding to they key (question) and remove that pair from your dictionary

Solution 2:[2]

You could shuffle the keys and loop over them. You're assured to have each question only once until you decide to stop.

Here is an example of code logic, assuming ask asks the question:

import random

keys = list(questions_answers.keys())

random.shuffle(keys)

for key in keys:
    answer = ask(key)
    if answer != questions_answers[key]:
        break

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Simon Hawe
Solution 2 mozway