Bob
Bob

Reputation: 1524

RSACryptoProvider generates same key repeatedly for same CspParameter ContainerName

I'm new to the .NET CryptoProvider space, and am a little concerned by what I have seen regarding the repeated creation of the same key by the RSACryptoProvider. I am using a container because I am storing the key off to file on the server, like so (I export the CspBlob subsequent to this creation and reimport it later)...

_cp = new CspParameters { KeyContainerName = ContainerName };

In this case the ContainerName has a hardcoded value that I reference the container by. What's bothering me is that when I create the RSACryptoProvider, and by exentsion the key pair, the generated key values are always the same!

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(RSAKeySize, _cp);

If I change the name of the container, the key changes. There must be SOME other source of randomness than the container name when you create an RSACryptoProvider, right? Otherwise that makes the name of the container a password, which is not my intention.

Upvotes: 2

Views: 1929

Answers (2)

freewill
freewill

Reputation: 1171

Following code will delete the key(if exist) related with the containername. After you delete the key; you can create a new one with the same conatiner name and you will get new random key.

            CspParameters cspParams = new CspParameters();
            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            //Create a new instance of RSACryptoServiceProvider. 
            //Pass the CspParameters class to use the 
            //key in the container.
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParams);

            //Delete the key entry in the container.
            rsa.PersistKeyInCsp = false;

            //Call Clear to release resources and delete the key from the container.
            rsa.Clear();

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273264

It's the name of a container, not of a generator.

If you want different keys each time, just create a new CryptoServiceProvider w/o referencing a container( == stored key-pair).

Upvotes: 2

Related Questions