I'm building an application (win form) by C# .NET for document signing. I have got an error in signing. When I sign document on some computers (windows 7, windows 10, not in Windows Server), I got an error: "The keyset not defined".
So, Someone can tutorial or suggest me how can fix this problem? Thank so much!
This is my code:
// get certficate
public X509Certificate2 LoadCertificateFromWindowsStore()
{
X509Store x509Store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
try
{
x509Store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection numberCerts = (X509Certificate2Collection)x509Store.Certificates;
X509Certificate2Enumerator certEnumerator;
if (numberCerts.Count == 1)
{
certEnumerator = numberCerts.GetEnumerator();
while (certEnumerator.MoveNext())
return certEnumerator.Current;
return null;
}
else if (numberCerts.Count > 1)
{
X509Certificate2Collection chooseCert = X509Certificate2UI.SelectFromCollection(numberCerts,
"Certificates List", "Choose your certificate", X509SelectionFlag.SingleSelection);
if (chooseCert.Count == 1)
return chooseCert[0];
else
return null;
}
else
return null;
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
finally
{
x509Store.Close();
}
return null;
}
// using the cert to sign
var cert = LoadCertificateFromWindowsStore();
if (cert.HasPrivateKey) // WORKS!!!
{
signedXml.SigningKey = cert.PrivateKey; // THROW "keyset does not exist" EXCEPTION
...
Asked
Active
Viewed 6,196 times
5

Tony Luca
- 71
- 1
- 4
-
1This is not a duplicate of the suggested question. "Keyset does not exist" and "Keyset is not defined" are 2 distinct error codes that occur for 2 different reasons. As the suggested duplicate indicates, "Keyset does not exist" is often a permissions issue. However, "Keyset is not defined" can occur when the Cryptographic Service Provider containing the key cannot be loaded. As the answer below suggests, this can sometimes be caused by a 32/64-bit mismatch between the program and the CSP. – jproch Oct 13 '17 at 13:29
1 Answers
6
I have solved this error. It is very easy. You select the "Platform target" to x86.
Right click on your project -> Properties -> Build -> Platform target -> x86
Regards,

Nguyen Van Tam
- 115
- 5
-
-
3More generally, the issue is a mismatch between the bitness of the program and the bitness of the CSP. In some cases, changing the platform target will be sufficient, but in other cases, the issue may be using a CSP with the correct bitness. For example, nCipher CSPs come with separate 32-bit and 64-bit versions. – jproch Oct 13 '17 at 13:32