CPSC 120 Programming Project 4
Public Key Cryptography

Due Friday, December 5, 2003 by 4:00 p.m.

In this project you will write a class that models a public key cryptography system. The main program will have two public key coder objects that can send encrypted messages to each other (and decrypt messages received).

In a public key cryptography system each person who wants to communicate with others has two sets of "keys" - one set is private, only known to the owner, and the other set is public, available to anyone who wants to send encrypted messages to the owner of the key. The public keys are used by the sender to encode the message and the private keys are used by the recipient to decode the message. Since the private keys are known only to the owner, that person is the only one who can read the message.

In this project you will implement a version of the RSA public key encryption scheme. This scheme is named after its developers - Ron Rivest, Adi Shamir, and Len Adleman. The system works as follows:

Each participant (which we will call a coder) constructs a "key" for encoding and decoding as follows:

  1. Pick two primes p and q and compute n = pq. (In a good coding system p and q should be very large.)
  2. Compute m, the least common multiple of p - 1 and q - 1.
  3. Pick r so that is has no divisors in common with m other than 1 (that is, r is relatively prime to m - any such r works).
  4. Find s so that rs = 1 (mod m) (there is only one such s between 1 and m).
  5. The n and r are the "public key" - they are available to anyone and are used to send encrypted messages to the holder of this key. p, q, and s are secret and are used to decrypt messages sent to the holder of this key.

The sender of a message gets the two parts of the public key of the recipient and then does the following:

  1. Converts the message to a string of digits.
  2. Breaks the message up into uniformly sized blocks of digits.
  3. If M is the integer form of the block of digits, then M must be relatively prime to n (otherwise the code can easily be broken). In practice, this should never be a problem - p and q should both be larger than any possible M hence n will automatically be relatively prime to n.
  4. For each block M, computes and sends R = Mrmod n.

The receiver of the message receives the sequence of R's and for each, calculates Rsmod n and then converts the resulting string of digits back to characters.

Given this description of the algorithm, we see that the attributes for the Coder class are the integers p, q, r, m, and s. The class needs the following public methods:

The Coder class will also need quite a few private support (helper) methods. Some of them are:

The main program should use two Coder objects - one for Alice and one for Bob (or you can use your own names for the two "people" sending encrypted messages back and forth -- it is standard practice in cryptography books to use Alice and Bob). Read in the p, q, and r for each then instantiate the object. Have a loop that lets the two send encrypted messages back and forth. So if Alice wants to send a message to Bob, your program needs to get the message, then get Bob's public keys, then encrypt the message, display the encrypted message, then Bob's coder will decode the message. The decoded message should be displayed. Use dialog boxes for input and output.

As usual, use good programming techniques. You should use good variable names, constants where appropriate, whitespace, and correct indentation and alignment of statements (emacs will do this for you if you let it!). Documentation must include a header with the file name, your name, the date, and a clear, complete description of what the program does. Each method must have a brief description of what it does. Both the Coder class and the class containing the main program must be documented.

Hand In

Turn in a printed copy of your program (the source code for both the Coder class and the main program). Tar your directory containing the project and email the tar file to ingram@roanoke.edu.

Academic Integrity Reminder!!! Programming assignments are to be your own work. You may get help on the specifics of the assignment from no one except the instructor. You may not show your program to anyone or look at anyone else's program code or share ideas with anyone about how to write the program.