COS 126
RSA
Public-Key Cryptosystem
|
Programming Assignment 10
Due: Wednesday, 11:59pm |
Overview.
Write a program to implement the RSA public-key cryptosystem.
The RSA (Rivest-Shamir-Adleman)
cryptosystem is widely used for secure communication
in browsers, bank ATM machines, credit card machines,
mobile phones, smart cards, and the Windows operating system.
It works by manipulating large integers. In the first part of the assignment,
you will write a C program that converts a text message into a sequence
of integers.
To thwart eavesdroppers, the RSA cryptosystem must manipulate
large integers (hundreds of digits). The built-in C type int
is only capable of dealing with 16 or 32 bit integers, providing little
or no security.
For the second part of the assignment, you will
design, implement, and analyze an extended precision
arithmetic data type that is capable of manipulating much
larger integers. Finally, you will use this data type to
write a client program that encrypts and decrypts messages
using RSA.
Note: the training wheels are off on this assignment - you're
starting with a blank screen, not our code.
This means that you must pay particular attention to the process of
building up your program from scratch. Consider carefully how your
program should be structured and how you are going to implement
the various functions before plunging in.
Remember to thoroughly test and debug each function as you write it.
The RSA cryptosystem.
As discussed in lecture S1, the RSA public key cryptosystem involves
three integers d, e, and n that
satisfy certain mathematical properties.
The private key (d, n) is
known only by Alice, while the public key
(e, n) is published on the Internet.
If Bob wants to send Alice a message, he encodes his message
as an integer M that is between 0 and n-1.
(Below, we describe what to do if Bob's message is longer
than n-1.) Then he computes:
C = Me mod n
and sends the integer C to Alice.
As an example, if M = 2003, e = 7, d = 2563, n = 3713, then Bob
computes
C = 20037 mod 3713 = 129,350,063,142,700,422,208,187 mod 3713 = 746.
When Alice receives the
encrypted communication C, she decrypts it by computing:
M = Cd mod n.
Continuing with the example above, Alice recovers the original message by computing:
M = 7462563 mod 3713 = 2003.
To check your arithmetic, you may use bc, maple, or
xmaple.
Part 1 (converting between text and integers).
Instead of writing separating programs to do this (as was originally
proposed), you will write two extra interface functions
XPinitASCII() and XPshowASCII(), as described
below.
We think this will make things easier.
Part 2 (extended precision arithmetic).
The RSA cryptosystem is easily broken if the private key d or
the modulus n are too small (e.g., 32 bit integers).
The built-in C types int and long int can typically handle
only 16 or 32 bit integers.
Your main challenge is to design, implement, and analyze an
extended precision arithmetic data type that can manipulate
large (nonnegative) integers. To make it easier to check your work, w
recommend working with integers represented using familiar decimal (base 10)
notation. (In real applications, the base is typically chosen to be a
power of 2 for performance reasons.)
Your data type will support the following types of operations:
addition, subtraction, multiplication, division, modular
exponentiation, and comparison. You may use the header file
XP.h.
Part 3 (encryption and decryption).
Now it's time to put all your hard work to use.
The encryption and decryption processes are identical, except
that they involve different keys.
Write a program rsa.c that takes in two command
line arguments (the string "-d" or "-e,
followed by the name of the encryption key), reads in ASCII
text
from standard input, and applies the RSA encryption/decryption
procedure to the message. Note: if the original message is larger
than the modulus, you should break up the message into
appropriate sized chunks, convert each chunk into an integer between
0 and the modulus, and encrypt each chunk individually.
It should work as follows.
% gcc126 rsa.c xp.c # compile
% a.out -e alice.pub < message.txt > message.encrypted # Bob encodes and encrypts
% mail alice < message.encrypted # Bob emails to Alice
% a.out -d alice.pri < message.encrypted # Alice decrypts and decodes
Part 4 (analysis).
Perform a complexity analysis of the following operations for
N-digit extended precision arithmetic:
addition, multiplication, division, and the rsa encryption,
and decryption algorithms.
For each operation, give the exponent and coefficient of the leading
term in its asymptotic running time,
e.g., 1.3 × 10-5 N2 seconds.
Justify your answers with empirical and theoretical evidence.
Using this analysis, estimate the largest input (measured in
number of digits) that each of your functions could solve
in 1 day (86,400 seconds).
Depending on your conclusions, you may wish to modify your design
and implementation to improve performance.
You may find it helpful to use the
program
timing.c to estimate the running times of your functions.
Legal notice.
It is a violation of US law to export your solution for this assignment
to foreign governments or embargoed destinations
(Cuba, Iran, Iraq, Libya, North Korea, Serbia,
Sudan, Syria, and Taliban-controlled areas of Afghanistan as of January 2000).
It is also illegal to import your solution into several countries, including
France, Iran, Iraq, and Russia.
Extra credit.
Write a program that compute two large pseudo-random prime numbers
p and q of a specified number of digits.
Compute φ = (p - 1) (q - 1), and select
a small integer e that is relatively prime with
φ.
Use these to generate a public key (e, n)
and its companion secret key (d, n).
This assignment was created by Bob Sedgewick and Kevin Wayne.
Copyright © 2000
Robert Sedgewick