In **Lecture 16** you
learned everything you needed to implement RSA key generation in Java using
the fields and methods of the `BigInteger` class. In your **Prelab** you wrote the Java code for the
`keyGenerate()` subroutine. In lab today you will add your
subroutine to the RSA demo applet code and implement the RSA encryption and
decryption subroutines. Once your code is working, you will have an applet
just like this:

Press the *Generate RSA Keys* button to compute public and private
key pairs. Enter your plaintext message in the box indicated and press
*Encrypt*. Your encrypted text is shown. Press the *Decrypt*
button to decrypt the ciphertext. You can generate a new set of keys for
the same message by pressing the *Generate RSA Keys* button again.
Press *Clear* to clear all areas of text.

Download **lab7.zip** and
extract its contents to the `cps001` subdirectory of your
`public_html` folder. You should now have a folder in your
`cps001` directory named `lab7`. Open the `lab7`
folder. It should contain two files, `RSA.java` and
`RSA.html`.

**Starting Emacs:** On the desktop of your computer there is a folder
named *Class Applications*. Open this folder and find the link to
Emacs. Double click on the link to start Emacs.

Open the file `RSA.html` in Emacs and add your name and today's
date. Save the file.

Open `RSA.java`. Look for the following:

You should type both your subroutines in the space between these comment lines. You do not need to modify any other code in the applet.

Type your prelab code for the `keyGenerate` subroutine.

The only thing left to do before you can compile is to add a subroutine
to perform encryption and decryption. In **Lecture 16** we discussed how one
subroutine could be used to perform both encryption and decryption, as they
both perform the same computation.

Recall that RSA encyption takes as input a numeric value for the
plaintext message `P` and the public key pair `(k,n)`. The
numeric ciphertext message `C` is then computed via the equation

C = (M^{k})%n

Similarly, RSA decryption takes as input a numeric value for the
ciphertext message `C` and the private key pair `(d,n)` and
decrypts the ciphertext via the equation

D = (C^{d})%n

All the values used for encryption and decryption are of type
`BigInteger`. We can use the `modPow` method of the
`BigInteger` class to perform the above computations for us.
Specifically, given `BigInteger` values `x`, `y` and
`z`, the value of ^{y})%z is the
`BigInteger` value

We can write a subroutine `crypt` to perform both encryption and
decryption as follows. The input parameters to `crypt` will be
three variables of type `BigInteger`. The first variable, say
`M`, represents the numeric text message. The second and third
variables, say `pkey` and `modkey`, represent the RSA key
pair for the desired operation - `pkey` being the unique public or
private key and `modkey` being the shared modulus key. The
`crypt` subroutine returns a value of type `BigInteger`,
which is computed using the `modPow` method.

Type your code for the `crypt` subroutine below the code you
just added for `keyGenerate`.

Compile `RSA.java` in Emacs by selecting *Tools->Compile*
from the drop-down menu.

**Emacs note:** If you want to get back to one window, go to
*File->Unsplit Windows* on the drop-down menu.

Once your applet has compiled, open `RSA.html` in a web browser
and test it. If everything runs as it should -- that is, you can generate
keys, enter a message, encrypt it and get your original message back by
decrypting -- move on to the next section and add a link to your applet.