[英]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 . 我有一个预先存在的RSA加密公钥/私钥对,我需要在.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: 要使用现有密钥,可以使用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. 为了告诉您如何从现有的keydata到参数,我们需要确切知道它们是如何编码的。 Try showing us the strings (replace most of the private key with Xs if it is a real key). 尝试向我们展示字符串(如果它是真正的密钥,则用X替换大多数私钥)。
I realise this is a very old question but maybe someone still looks at this... 我意识到这是一个非常古老的问题,但也许有人仍然看着这个......
Nowadays you can retrieve/store your keys in XML format (which you possibly could back in the days too). 现在,您可以以XML格式检索/存储密钥(您可能也可以在以后的日子里找回)。
Example import: 示例导入:
this.RSAKey = RSA.FromXmlString(xmlString: myRSAXMLKey);
Example export: 示例导出:
this.RSAKey_XMLString = RSA.ToXmlString(includePrivateParameters: false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.