关于c#:使用RSACryptoServiceProvider生成随机RSA密钥

Generate random RSA keys with RSACryptoServiceProvider

如何使用rsacryptoServiceProvider类生成随机的rsa公钥和私钥(rsaparameters)?每次我创建rsacryptoServiceProvider的新实例时,都会导出相同的密钥。

谢谢


我对以下代码做了一些测试,导出的参数总是不同的:

1
2
3
4
5
6
7
8
9
10
var rsaAlgo1 = new RSACryptoServiceProvider();
var rsaAlgo2 = new RSACryptoServiceProvider();

var xml1 = rsaAlgo1.ToXmlString(true);
var xml2 = rsaAlgo2.ToXmlString(true);

if (xml1 != xml2)
{
   // it always goes here...
}


使用以下代码,您永远不应该取出所有相同的钥匙

1
2
var rsa = new RSACryptoServiceProvider();
var rsaParams = rsa.ExportParameters(true);

但是,您应该注意,指数键可以是相同的,如果经常是65537(0x010001)。

"Choose an integer e such that 1 < e < φ(n) and gcd(e, φ(n)) = 1; i.e., e and φ(n) are coprime. e is released as the public key exponent. e having a short bit-length and small Hamming weight results in more efficient encryption – most commonly 216 + 1 = 65,537. However, much smaller values of e (such as 3) have been shown to be less secure in some settings." RSA wiki