views:

24

answers:

2

This code below thows the following error: System.Security.Cryptography.CryptographicException "The handle is invalid"

CspParameters CSPParam = new System.Security.Cryptography.CspParameters(13, null, null);
CSPParam.Flags = CspProviderFlags.UseMachineKeyStore;
CSPParam.KeyContainerName = "MyKeys";

System.Security.Cryptography.DSACryptoServiceProvider r = new System.Security.Cryptography.DSACryptoServiceProvider(CSPParam);  //Error occurs here

This code had been working with no issues then it started throwing this error. I can't think of anything that I have changed that relates to this. Has nayone seen this error? Any ideas of what to try?

A: 

Make sure you have the proper permissions to do this action.

information

Woot4Moo
I looked a that article and did what it said but it did not help.
Loki Stormbringer
A: 

I cannot remember everything that goes on behind the scenes in CAPI, but if I recall it can be fairly painful. By using null arguments you might get something called (I think) the default key container. I don't think you can count on the default key container not existing before you run your code, and perhaps if it exists then you get an error. You can read all about the CryptAcquireContext function and the various _KEYSET_THIS_THAT flags to learn about how to do this in unmanaged code. I have no clue if there is a .NET wrapper around that horror or not.

GregS