I think the problem is the way you are adding the certificate to the context:
private SecureMimeContext CreateSecureMimeContext(string certificateFilename, string certificatePassword)
{
var secureMimeContext = new WindowsSecureMimeContext();
var certificate = new X509Certificate2(certificateFilename,
certificatePassword);
var bouncyX509Certificate = Org.BouncyCastle.Security.DotNetUtilities.FromX509Certificate(certificate);
secureMimeContext.Import(bouncyX509Certificate);
return secureMimeContext;
}
What this is doing is adding the certificate to the AddressBook certificate store, but where you actually want to add it in order to decrypt it (because you'll need the private key password as well) is the personal cert store.
Try this:
private SecureMimeContext CreateSecureMimeContext (string certificateFilename, string certificatePassword)
{
var secureMimeContext = new WindowsSecureMimeContext ();
using (var stream = File.OpenRead (certificateFilename))
secureMimeContext.Import (stream, certificatePassword);
return secureMimeContext;
}
The exception message you are getting isn't very helpful, but I am guessing that it means that it can't find the certificate and/or private key. I wrote a test program and have been unsuccessful in reproducing this bug, so I could be wrong... but since my certificates/private keys are already imported into my personal cert store, I'm not sure how to remove them to test this theory.
Let me know how that works out for you.
Update: Oops, I done goofed. Just updated CreateSecureMimeContext() above to open the certificate file stream because WindowsSecureMimeContext.Import() takes a stream, not a file name.