如何加快RSA密钥对的生成过程

时间:2017-06-06 09:21:49

标签: c# .net multithreading rsa bouncycastle

我使用下面的代码生成10000个RSA密钥对。这似乎需要很长时间(大约需要25-30分钟):

for (int i = 0; i < 10000; i++)
{
    String[] arr = GenerateKeys(1024);
}

public static string[] GenerateKeys(int keySizeInBits)
{
    var r = new RsaKeyPairGenerator();
    r.Init(new KeyGenerationParameters(new SecureRandom(), keySizeInBits));
    var keyPair = r.GenerateKeyPair();
    var publicKey = string.Empty;
    using (var stream = new MemoryStream())
    {
        var textWriter = new StreamWriter(stream);
        var pemWriter = new PemWriter(textWriter);
        pemWriter.WriteObject(keyPair.Public);
        pemWriter.Writer.Flush();
        stream.Position = 0;
        var sr = new StreamReader(stream);
        publicKey = sr.ReadToEnd();
        textWriter.Close();
    }

    var privateKey = string.Empty;
    using (var stream = new MemoryStream())
    {
        var textWriter = new StreamWriter(stream);
        var pemWriter = new PemWriter(textWriter);
        pemWriter.WriteObject(keyPair.Private);
        pemWriter.Writer.Flush();

        stream.Position = 0;
        var sr = new StreamReader(stream);
        privateKey = sr.ReadToEnd();
        textWriter.Close();
    }

    return new string[] { publicKey, privateKey };
}

如何让它更快? (多线程可能?)

0 个答案:

没有答案