在 .net 框架中使用先前生成的 RSA 公钥/私钥 [英] Using a previously generated RSA public/private key with the .net framework

查看:33
本文介绍了在 .net 框架中使用先前生成的 RSA 公钥/私钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预先存在的用于 RSA 加密的公钥/私钥对,我需要在 .net 中使用它.我可以在网上找到的所有示例都演示了如何生成新的私有/公共对,然后加密/解密.IE.像这样:

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.

干杯纳仁

推荐答案

要使用现有密钥,您可以使用 ImportParameters-方法:

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.

为了告诉您如何从现有的密钥数据获取参数,我们需要确切地知道它们是如何编码的.尝试向我们展示字符串(如果它是真正的密钥,请将大部分私钥替换为 Xs).

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

这篇关于在 .net 框架中使用先前生成的 RSA 公钥/私钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆