2
for (int ii = 0 ; ii < 200 ; ii++)
    {
     encrypt();    
    }

long start = System.currentTimeMillis();
    for (int ii = 0 ; ii < 2000 ; ii++)
    {
     encrypt();    
    }
long elapsed = System.currentTimeMillis() - start;

for (int ii = 0 ; ii < 200 ; ii++)
    {
     decrypt();    
    }

long start = System.currentTimeMillis();
    for (int ii = 0 ; ii < 2000 ; ii++)
    {
     decrypt();    
    }
long elapsed = System.currentTimeMillis() - start;



private void encrypt()
    {
            M = new BigInteger(64,random);
            C = M.multiply(k).mod(N);  
    }

private void decrypt()
    {
            kk= k.modinverse(N); 
            Mp = kk.multiply(c).mod(N); 
    }

But I feel that the results are incorrect when run this program on netbeans platform. Is there way to compare any two algorithm cryptography under execution time . Is necessary decrypt algorithm take long time than encrypt algorithm? Please any suggest.

osgx
  • 90,338
  • 53
  • 357
  • 513
Mhsz
  • 243
  • 1
  • 10

1 Answers1

3

The encryption method seems to contain the generation of a random BigInteger. This is normally only performed during key pair generation and - in the case of RSA - it would require the generation of two (or more) random prime numbers. This is not a known encryption method, and the time for key generation and encryption should be separated.

What you are trying to do is called "micro-benchmarking". First the Java interpreter may need to perform the JIT compilation, after which the byte code can be run as machine code. After that the interpreter may also decide to optimize. So basically any testing should keep that in mind, using a large number of test rounds, preferably prefixed with a dummy number of rounds that don't count towards the total. Probably best is to use a framework that focuses on or supports micro-benchmarking such as JMH (no affiliation or experience).

The output of the sample encryption routine very much hinges on the state of the random number generator. If the entropy is depleted, it will take a long time for it to generate new numbers. So if it stalls, your results will reflect the state of the entropy in the system's RNG rather than anything else.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • Thank you for all.I will ignore that line which contain of generate random big-integer. If this method valid to compare between encrypt/decrypt elapsed time. now just I like to know. How many number of iteration to reach an acceptable outcome? – Mhsz Feb 10 '13 at 19:17
  • Depends on the system configuration. It could be that this is enough, but the best way to test is to go for a really high number, a few million. The minimum is not clear, but in general, if the average of a few million times is identical to the average of a few thousand times then you are getting valid results. – Maarten Bodewes Feb 10 '13 at 20:15
  • thank you owlstead . Is intended at system configuration computer hardware specifications? such as cpu and ram or else. I run this program on my laptop has cpu ci3 2.30 GH , 2.91 GB of RAM and NetBeans IDE 7.2.1 platform integrated with Java: 1.6.0_37; Java HotSpot(TM) Client VM 20.12-b01. Is it necessary to mention the computer specifications when writing the results in the my project؟ – Mhsz Feb 10 '13 at 20:42
  • I would certainly write down thes system configuration when writing down a result. Note that it also matters if your CPU is performing 32 bit or 64 bit operations. BigInteger consists of 64 bit longs internally (for the Sun implementation) so it presumably runs faster on a 64 bit machine that also has more CPU registers. The IDE and RAM make less of a difference, but it never hurts to include it in the result (together with the VM settings). – Maarten Bodewes Feb 10 '13 at 23:22