hello everyone,
I have implemented RSA algorithm in java but now i am asked to determine the time for encrypting variable length messages(plain text). As an example, i have to find the time in which input message of length 3 will get encrypted, similarly the time in which input message of length 7 will get encrypted so that i can make comparisons. So please help me in determining that which software i can use for it?

ive seen a thread about the same kind of problem go by on thsi very forum earlier this week, let me find it and refer it to you.

you will not need any other software, just 3-4 extra lines of code.

hello everyone,
I have implemented RSA algorithm in java but now i am asked to determine the time for encrypting variable length messages(plain text). As an example, i have to find the time in which input message of length 3 will get encrypted, similarly the time in which input message of length 7 will get encrypted so that i can make comparisons. So please help me in determining that which software i can use for it?

couldnt you use the

System.currentTimeMillis();

to get the time before the statement/method executes and get the time when it returns, minus the 2 and you will have the time it took to execute the method/statement?:

long startTime = System.currentTimeMillis();
//method/statement call
		long stopTime = System.currentTimeMillis();
		long elapsedTime = stopTime - startTime;
		System.out.println(elapsedTime);

aight well i couldnt find the thread i saw this week about it but i found one from 2 years ago which talks about the same thing, check this out

couldnt you use the

System.currentTimeMillis();

to get the time before the statement/method executes and get the time when it returns, minus the 2 and you will have the time it took to execute the method/statement?:

long startTime = System.currentTimeMillis();
//method/statement call
		long stopTime = System.currentTimeMillis();
		long elapsedTime = stopTime - startTime;
		System.out.println(elapsedTime);

that is exactly what the other thread i saw this week and the one i linked suggested, i was just trying to minimize replication :P

Edited 4 Years Ago by Philippe.Lahaie: typo

thanks to all.. the code provided worked well, but plz tell me how to get it in seconds or picoseconds.

here is my code:

import java.math.BigInteger;
import java.security.SecureRandom;
    

public class RSA {
   private final static BigInteger one      = new BigInteger("1");
   private final static SecureRandom random = new SecureRandom();

   private BigInteger privateKey;
   private BigInteger publicKey;
   private BigInteger modulus;

  
   RSA(int N) {
      BigInteger p = BigInteger.probablePrime(N/2, random);
      BigInteger q = BigInteger.probablePrime(N/2, random);
      BigInteger phi = (p.subtract(one)).multiply(q.subtract(one));

      modulus    = p.multiply(q);                                  
      publicKey  = new BigInteger("65537");     
      privateKey = publicKey.modInverse(phi);
   }


   BigInteger encrypt(BigInteger message) {
      return message.modPow(publicKey, modulus);
   }

   BigInteger decrypt(BigInteger encrypted) {
      return encrypted.modPow(privateKey, modulus);
   }

   public String toString() {
      String s = "";
      s += "public  = " + publicKey  + "\n";
      s += "private = " + privateKey + "\n";
      s += "modulus = " + modulus;
      return s;
   }
 
   public static void main(String[] args) {
      int N = Integer.parseInt(args[0]);
      //int N = 88;
	  RSA key = new RSA(N);
      System.out.println(key);
 
      
      BigInteger message = new BigInteger(N-1, random);


	long startTime = System.currentTimeMillis();
      BigInteger encrypt = key.encrypt(message);
	long stopTime = System.currentTimeMillis();
	long elapsedTime = stopTime - startTime;
	System.out.println("Elapsed Time: "+elapsedTime + " " + "milliseconds");
      BigInteger decrypt = key.decrypt(encrypt);
      System.out.println("message   = " + message);
      System.out.println("encrpyted = " + encrypt);
      System.out.println("decrypted = " + decrypt);
   }
}

i have used the above provided code  at line number 51-54 including my method call. but i am getting the output for elapsed time as 0 miliseconds. can anyone help please.

here is my code:

import java.math.BigInteger;
import java.security.SecureRandom;
    

public class RSA {
   private final static BigInteger one      = new BigInteger("1");
   private final static SecureRandom random = new SecureRandom();

   private BigInteger privateKey;
   private BigInteger publicKey;
   private BigInteger modulus;

  
   RSA(int N) {
      BigInteger p = BigInteger.probablePrime(N/2, random);
      BigInteger q = BigInteger.probablePrime(N/2, random);
      BigInteger phi = (p.subtract(one)).multiply(q.subtract(one));

      modulus    = p.multiply(q);                                  
      publicKey  = new BigInteger("65537");     
      privateKey = publicKey.modInverse(phi);
   }


   BigInteger encrypt(BigInteger message) {
      return message.modPow(publicKey, modulus);
   }

   BigInteger decrypt(BigInteger encrypted) {
      return encrypted.modPow(privateKey, modulus);
   }

   public String toString() {
      String s = "";
      s += "public  = " + publicKey  + "\n";
      s += "private = " + privateKey + "\n";
      s += "modulus = " + modulus;
      return s;
   }
 
   public static void main(String[] args) {
      int N = Integer.parseInt(args[0]);
      //int N = 88;
	  RSA key = new RSA(N);
      System.out.println(key);
 
      
      BigInteger message = new BigInteger(N-1, random);


	long startTime = System.currentTimeMillis();
      BigInteger encrypt = key.encrypt(message);
	long stopTime = System.currentTimeMillis();
	long elapsedTime = stopTime - startTime;
	System.out.println("Elapsed Time: "+elapsedTime + " " + "milliseconds");
      BigInteger decrypt = key.decrypt(encrypt);
      System.out.println("message   = " + message);
      System.out.println("encrpyted = " + encrypt);
      System.out.println("decrypted = " + decrypt);
   }
}

i have used the above provided code  at line number 51-54 including my method call. but i am getting the output for elapsed time as 0 miliseconds. can anyone help please.

this would help:http://www.simetric.co.uk/si_time.htm

Do you mean that it is a conversion problem?

no sorry that was for your previous post:

the code provided worked well, but plz tell me how to get it in seconds or picoseconds.

as for why it keeps showing 0, because the statement is executing so fast basically there is no significant amount of processor time used. Try changing you N value to something bigger:

int N = Integer.parseInt("4000");

That took my computer 4 miliseconds to execute, as the number gets bigger it takes even longer for the encryption to run, thus taking more time.

ya u said right, but its the execution time and not the encryption time? is it so?

sorry but i do not quite get your question, mind rephrasing it

is the time displayed the execution time or encryption time?

well think about it:

long startTime = System.currentTimeMillis();
      BigInteger encrypt = key.encrypt(message);//encryption method call
	long stopTime = System.currentTimeMillis();
	long elapsedTime = stopTime - startTime;

so yes this would only specify the encryption time, getting the full execution time of a program can be done by a basic:

public class FullExecutionTime {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();//this would be the very first statement in the main
      ....//rest of the processing and method calls etc will happen
	long stopTime = System.currentTimeMillis();//get the time after all has been completed
	long elapsedTime = stopTime - startTime;
}//end if main
}

but its still showing me 0 milliseconds

have you changed the value of N? because it doesn't mean because we are checking the entire program for its execution time, that it will take long/longer, or for that matter show a number of meaning that will appear in the milliseconds... As i said, if your PC is fast, or little calculation is been done, the time taken is practically nothing, and will not be shown.

This question has already been answered. Start a new discussion instead.