且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用先前生成的RSA公钥/私钥与.net框架

更新时间:2023-01-18 09:19:44

要使用现有密钥,可以使用 ImportParameters -method:

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

您也可以添加私有参数,以便将其用于解密或签名。 p>

为了告诉你如何从现有的密钥数据中获取参数,我们需要准确地知道它们的编码方式。尝试向我们显示字符串(如果是真正的密钥,请用X替换大部分私钥)。


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

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).