Generate random RSA keys with RSACryptoServiceProvider
How do I generate 开发者_StackOverflowrandom RSA public and private keys (RSAParameters) using RSACryptoServiceProvider class? Each time I create a new instance of RSACryptoServiceProvider, I end up exporting the same keys.
Thanks
I did some test on the following code, and the exported parameters are always different:
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...
}
Using the following code you should never get all the same keys out
var rsa = new RSACryptoServiceProvider();
var rsaParams = rsa.ExportParameters(true);
However you should note that the Exponent key can be the same and if often is 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
精彩评论