Oct 16, 2010

System.Security.Cryptography.CryptographicException: Object already exists.

It occurs when I try to initialize an RSACryptoServiceProvider instance.

This happens because the program is run with different users; One with normal user and another with startup user.

When the key is created, its permission is only granted to the creator.

Therefore, you need to change the permission of the key in order that it can be used by everyone.

Here is the code:


CspParameters cspParams;
cspParams = new CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";

CryptoKeyAccessRule rule = new CryptoKeyAccessRule("everyone", CryptoKeyRights.FullControl, AccessControlType.Allow);

cspParams.CryptoKeySecurity = new CryptoKeySecurity();
cspParams.CryptoKeySecurity.SetAccessRule(rule);



OR another solution is to not use UseMachineKeyStore Option.