Project 2: RSA Encryption

Write a program as described below first in Java and then again in Kotlin. In both cases, the difficult operations are available to you in java.lang.BigInteger.

Perform the basic math in encrypting and decrypting several messages in the Rivest-Shamir-Adleman cryptosystem, a cryptosystem for public-key encryption. The input consists of any number of lines of plain text and of encrypted text. The text in this case will be a single number, possibly large (but not too large).

message 981723918274
message 8432981723918274
cipher  9834598734559
message      2314986213123475435

The output of the program is of the same form, except the plain text has been replaced by the encrypted text and vice versa.

The program is an involution, that is to say, it is its own inverse. Running the program again on its output will result in the original input.

There are only a few key values in the program:


Encryption is simple (a single function call). Raise the message to the power e mod the product of the primes p and q.


Decryption is simple (a single function call). Raise the cipher to the power d mod the product of the primes p and q.

Key Generation

For the scheme to work we need to generate three primes. For the results to be reproducible we must generate the same three primes. To do this we require the program to take two properties seed and bits. The program will be run like this:

java    -Dseed=23434 -Dbits=100 RSA
kotlin  -Dseed=23434 -Dbits=100 RSA

The -Dname=value things on the command line are called system properties in Java. Your RSA programs should require no command line arguments. Create the primes p, q, and e in that order using the probablePrime function with the arguments bits and a random number generator using the given seed. The system property bits is used for the paramber bitLength in the probablePrime method.

Programs with the same seed, the same bit length, and the same input will produce the same output.

Sample Input/Output

For -Dseed=23434 -Dbits=100 and the input:
message     981723918274
message 8432981723918274
cipher     9834598734559
message      2314986213123475435
the output is:
cipher  191853804053733607643460120109149562803779107775517482874222
cipher  990247782703453419935166393974114859351043136440810572699234
message 952014225310412564875114228138574997898437010425924791995896
cipher  8919361814022696132891758631023073401838209481380951402492
(The white space between the label and the number in the input and the output is one or more spaces.)

And, if you use the above output as the input, then output will be the input above. Cute, no? Now you have two test cases and you make as many test cases as you want!

Turning it in

Write the programs as clearly as possible, including reasonable comments. You may work alone or with one other student in the class on this pair of projects, as long as you did not work with this partner in any of the previous projects.

Turn in the source code for the program using the Submit Server, each program separately. The project tags are proj2J and proj2K. In the comments somewhere at the beginning of your source file include a header like the following:

 - Author: name1, e-mail address
 - Author: name2, e-mail address
 - Course: CSE 4250, Fall 2017
 - Project: Proj2, RSA Project

All programs are analyzed for similarity with other programs, both current and past. Students whose programs are very similar to other sources will receive no credit. This policy is necessary to ensure that students take reasonable action to avoid and prevent plagiarism, and to ensure the proper recognition of independent effort. Without student cooperation individual grades would have less meaning and the incentive for learning decreases. If you have evidence of academic misconduct, you should bring it to the attention of your instructor, the head of the department, or Dean of the school of engineering.