Sunteți pe pagina 1din 2

ITK 368/374/385, Spring 2009 Cryptography

Programming Assignment 4: RSA (60 points)

Due: Apr. 2, Thursday

In this assignment you are asked to use C++/Java to implement an RSA cipher with
arbitrary key size (in decimal digits). I will ask you to test your cipher up to 90 digits.
You are free to use any C++ package to handle big integers. We adapt the convention that
Bob is the party who will announce the public keys and Alice the one who will send Bob
message encrypted with the public keys. The two parties are to be implemented in two
classes described as follows, where xxxx is a big integer data type. If you are using Java,
you should convert following C++ syntax into appropriate one for Java, provided that you
have to use the same names for the classes and methods.
class RSA Bob {
public:
RSA Bob(int d); // d digits RSA. Keys will be generated randomly
RSA Bob(xxxx a, xxxx p, xxxx q); // Setup with given a, p, and q, and let n=pq
xxxx get key a(); // Return the public key a
xxxx get key n(); // Return the public key n
xxxx decrypt(xxxx y); // Decrypt y and Return the message
private:
xxxx a, n; // Public keys
xxxx b, p, q, phi; // Private keys
};

class RSA Alice {


public:
RSA Alice(xxxx a, xxxx n); // Initialize the public keys
xxxx get key a(); // Return the public key a
xxxx get key n(); // Return the public key n
xxxx encrypt(xxxx x); //Encrypt x and return the ciphertext
private:
xxxx a, n; // Public keys
};

The constructor RSA Bob(int d) will set up a d digits RSA cipher as follows: First,
randomly find two prime numbers for p and q.1 The ideal size for p and q is d/2
and d d/2, respectively.2 Once the two appropriate primes, p and q, are obtained,
compute n = pq, phi = (pq). Then, randomly pick a {2, 3, . . . , n 1} such that
gcd(a, (pq)) = 1, and compute b such that ab 1 (mod (pq)). Afterwards, Bob can
use get key a() and get key n() to announce the public keys.
The constructor RSA Bob(xxxx a, xxxx p, xxxx q) sets the keys according to the
arguments and calculates all required values as follows: n = pq, phi = (pq) and b
such that ab 1 (mod (pq)). If the arguments do not represent valid values, print
out some warning messages. I.e., if any of p and q is composite or gcd(a, (pq)) 6= 1,
tell the user the situation.
The constructor RSA Alice(xxxx a, xxxx n) simply sets the public keys for encryp-
tion.

The meanings and functionalities of the rest methods should be clear from the description
in the comment lines above.
1
You have to use a 20-round Miller-Rabin primality test.
2
You should make sure that the size of pq is not d 1. For example, if d = 2, you should avoid the situation
such as p = 2, q = 3, since pq = 6, which is 1 digit, not 2 digits as required.

Due: Apr. 2, Thursday c Chung-Chih Li


P-1/2
ITK 368/374/385, Spring 2009 Cryptography

What to test

Include the following tests in your report. (See the sample output from the Webpage.)

1. Suppose Bob has announced the following 5 sets of public keys.

n: 2130823927
a: 1969825549
n: 10003830247254270607
a: 5816502492932991877
n: 294477607044773613683792037277
a: 39589949053584291190231504387
n: 2652641470224071885338583352066147997379
a: 630744317911048980200952132968936360747
n: 14659675518547608283114839209179803045399087670327
a: 1114752877829729375783111016047040196191446175273

Use your RSA Alice to encrypts your ISU ID with each different public keys.
Hint: use copy-and-past to avoid mistyping.

2. Randomly test your cipher for 10, 20, 30, . . . 90 digits. You should call the constructor
RSA Bob(d) for d = 10i , i = 1, 2, . . . , 9. Then, use the randomly selected public keys of size
d to set up Alice. For each size, Alice should create three random messages of size d (more
precisely, the message must be less than n), encrypt them, and sent the cypher texts to
Bob. Then, Bob should be able to decrypt the cypher texts back to the original messages.
(See the same output).
item As Before, follow the guideline for submission.

Due: Apr. 2, Thursday c Chung-Chih Li


P-2/2