System.Security.Cryptography.CryptographicException: keyset does not exist

大城市里の小女人 提交于 2019-11-28 16:22:01

This question is old but for someone looking for the solution while continuing to use Encrypt and Decrypt here is how I manage to solve this error:

The base is my certificate have been installed the wrong way by double-clicking the .pfx file and selecting the store.

The wrong way to install the certificate

1. Double click the certificate:

2. The wizard opens, click in the next button:

3. The wizard show the certificates location, click in the next button:

4. Enter the password then click next:

5. Select the store then click next:

6. The wizard show the certificate information, click in Finish button

7. Succes dialog is showed:

So at this point I had the error "Keyset does not exist".


To solve that I proceeded this way (the correct way)

1. Execute Microsoft Management Console (mmc.exe):

2. A blank MMC instance showed:

3. Click in File->Add/Remove Snap-in...

4. Select certificate snap-in an click in Add button:

5. Select Computer account then click in Next button:

6. Select Local computer then click in Finish button:

7. The certificate snap-in is now added, click in OK button:

8. Select the personal store, then right click and select Import:

9. Browse the certificate, and click next:

10. Enter the password, then click in Next button:

11. Automatically select the certificate store:

12. The certificate information shows:

13. Success dialog message shows:

14. Refresh the MMConsole to show the certificate:

15. Right click on the certificate, then click in Manage Private Keys...:

16. Add the pool identity or the IIS user in my case I added IIS_IUSRS:

17. The user has been added, click on OK button:

And it is finished the keyset does exist now!!

The application might be trying to write to the following folder path: C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys

If your application is using impersonation or using the IUSR_MACHINENAME user, then configure the MachineKeys folder sercurities and give the user Read & Execute, List Folder Contents, Read, Write. If that doesn't work, try giving the Everyone user the same permissions.

I believe when using Encrypt and Decrypt it expects a public key for encrypt and a private key for decrypt. So it is failing because you are trying to decrypt without the private key and that causes the exception.

You should really be using SignData method for creating the signature and VerifyData for the verification.

I did run into the same problem. The message is not ideal and in my case my user did not have permission to access the private key. You can fix this using these steps:

  1. Open mmc
  2. Add certificate snap-in
  3. select the certificate you want to use
  4. right click on it and select 'All Tasks' / 'Manage Private Keys...'
  5. Add your user to the list of authorized users and allow 'Full Control'
Mough

I ran into this error when I wasn't loading in the PrivateKey from my certificate into my signedXmlElement's SigningKey when trying to sign a SAML response.

signedElement.SigningKey = myCertificate.PrivateKey;

I received same error as OP: "System.Security.Cryptography.CryptographicException: keyset does not exist"

The resolution (for me) was: Visual Studio needs to be (run as Admin)

As was explained to me(YMMV), VS needs to be run as Admin in order to extract the certificates private key from the key store, in order to negotiate auth/handshake with keyvault.

Try running vs as Administrator. Worked for me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!