Excessively slow first call to RSAParameters .ExportParameters(false)

F

floppyzedolfin

Hello.
I'm working in Visual Studio, language is C#, and I am coding a
cryptography program.
At some point, I need random values, and rather than using a made-
for-
it random generator, I use the first bytes of
RSACryptoServiceProvider.ExportParameters(false).Modulus.

I've posted this subject in the Security subsection of the dotnet
group, but it seems that that group is a bit more inactive than this
one, hence I'm also posting it here.

The reason for that is just that I also have a call to that
RSACryptoServiceProvider.ExportParameters(false) further on, and my
problem is that the first call to
RSACryptoServiceProvider.ExportParameters(false) is excessivly slow
(it's in a range of 5 secs to 120 secs), whereas all furtherer calls
are fast (less than 20 ms)


Could there be a reason for that, and could it be avoided?


Here's a part of the code I use :


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;


namespace My_Project
{
class My_Class
{
static void Main() {


RSACryptoServiceProvider RSACrypto = new
RSACryptoServiceProvider(4096);


byte[] My_Hash = new byte[20];
My_Hash[0] = RSACrypto.ExportParameters(false).Modulus[0]; //this
line takes 20 secs, even if it's included inside the loop
for(int i=1; i<20; i++) // this loop is fast enough to be
performed in less than 0.5 sec
{
My_Hash = RSACrypto.ExportParameters(false).Modulus;
}
/* blah blah blah */
// do things such as encrypt(data,
RSACrypto.ExportParameters(false));



}
}
}


A few words about that : it's not the entire code, so don't be afraid
of the uselessness of those lines; and, yes, I could've written the
RSACrypto.ExportParameters(false).Modulus into a buffer, but that's
not the point I want to emphatize on right now (since it will not
skip
that first call)

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top