Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

c# - Using a previously generated RSA public/private key with the .net framework

I have a pre-existing public/private key pair for RSA encryption which I need to use in .net . All the examples I can find online demonstrate how to generate a new private/public pair and then encrypt/decrypt. ie. something like this:

const int PROVIDER_RSA_FULL = 1;
const string CONTAINER_NAME = "SpiderContainer";
CspParameters cspParams;
cspParams = new CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
rsa = new RSACryptoServiceProvider(cspParams);
.....
rsa.encrypt(...)
rsa.decrypt(...)

As can be seen, there is no avenue for specifying a pre-existing public/private key.

Would anyone know how to accomplish what I am trying to do? Any help would be much appreciated.

Cheers Naren

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To use an existing key, you can use the ImportParameters-method:

RSAParameters parameters = new RSAParameters()
parameters.Modulus = // ...
parameters.Exponent = // ...

RSA rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(parameters);
rsa.Encrypt(/*...*/);

You can add the private parameters, too, in order to use it for decrypting or signing.

In order to tell you how to get from your existing keydata to the parameters, we need to know exactly how they are encoded. Try showing us the strings (replace most of the private key with Xs if it is a real key).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...