简体   繁体   中英

How to store private key in Key Container?

I want to store a private key in a key container to create a signature over data for an asp.net website.

//First I Read PrivateKey from text file that i Uploaded with FileUpload(fuPrivateKey)
var pk = Encoding.UTF8.GetString(fuPrivateKey.FileBytes);
CspParameters csp = new CspParameters();
csp.KeyContainerName = "PrivateKeyForSignature";
DSACryptoServiceProvider rsa = new DSACryptoServiceProvider(csp);//error
rsa.FromXmlString(pk);

but I got the following error at line of DSACryptoServiceProvider rsa = new DSACryptoServiceProvider(csp); :

The specified cryptographic service provider (CSP) does not support this key algorithm.

I have used this method for storing an RsaCryptoServiceProvider key without any problem. However when I want to use it for DSA it doesn't work.

If I take a look at the default (no argument) constructor of CspParameters then I get this text :

This form of CspParameters initializes the ProviderType field to a value of 24 , which specifies the PROV_RSA_AES provider. This default provider is compatible with the Aes algorithm.

If I look up the constructor that accepts a 32 bit integer I get :

Initializes a new instance of the CspParameters class with the specified provider type code.

...

To specify a provider compatible with the DSA algorithm, pass a value of 13 to the dwTypeIn parameter.

So it seems to me this can be solved by calling the right constructor of CspParameters with the right code.

The criteria as mentioned by @MaartenBodewes is correct. CspParameters by default are configured to RSA container. Which is why, there were no errors when the same was tried with RSACryptoServiceProvider .

CspParameters csp = new CspParameters();

is same as:

CspParameters csp = new CspParameters(1);

To use crypto service in DSACryptoServiceProvider , the parameterized constructor has to be called as:

CspParameters csp = new CspParameters(13);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM