\
In order to create a RSA ket, you first need to learn how to create a random number. To create a (pseudo) random integer in the range [0,n) you can use the Random class:
Random gen = new Random() creates a new random number generator gen. The code randIndex = gen.nextInt(n) assigns to randIndex a pseudorandom, uniformly distributed integer value between 0 (inclusive) and n (exclusive), drawn from the random number generator gen's sequence. Inclusive means that the integer value 0 can be generated, while exclusive means the integer value n cannot.
Problem 1: Write a subroutine RandBetween which returns a random integer between low and high inclusive:
For example,
randIndex = RandBetween(0,4);
will assign to randIndex either 0, 1, 2, 3 or 4 with equal probability.
In lecture, we learned about RSA and how to implement RSA key generation in Java using the fields and methods of the BigInteger class.
Problem 2: Write a Java method keyGenerate selects an n and calculates the values of k and d. Make two copies of your prelab code. Submit one copy to your Lab TA at the beginning of lab session.
In Lab 10 you will add your key generation code to an RSA demo applet and implement the RSA encryption and decryption subroutines.
Follow these instructions carefully, as the RSA applet which you write in the lab, will expect your subroutine to have a particular name and assign values to the specified fields.
Name your subroutine keyGenerate.
There are no input parameters.
The return type of your subroutine should be void, that is, your subroutine does not return a value when called. The reason for this is as follows: the public and private keys will be declared as fields or attributes in the java code. Their values will be computed and assigned by your subroutine.
The variable names for the public and private keys are k, d and n and are of type BigInteger. The pair (k,n) corresponds to the public RSA key and the pair (d,n) corresponds to the private RSA key.
You must declare any variables other than the aforementioned that you require. Declaring a variable means that you must explicitly specify a name and a type before you can use it in your program.
You must have already seen the probablePrime method that can be
used to generate a prime BigInteger with specified bit length.
There are two subroutines provided for you in the RSA code that you should
use to specify bit lengths. They are bigBit() and
smallBit(), and they return a random bit size within a
prespecified range such that the values returned by bigBit() are
always larger than those returned by smallBit(). For example, if
we want to assign a "large" prime to a BigInteger named
p, we could use the statement
To assign a "small" prime to a BigInteger named s, we would use the statement
Note that the above statements are only valid when p, s and rng have already been declared.
The purpose of the keyGenerate subroutine is to compute the values of the public and private RSA keys. As such, your subroutine must assign values to k, d and n.
The Java API specification for the BigInteger class is found here:
http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html
Credit to Albert Meixner and Tammy Bailey for originally developing this assignment.