CS560-2023-Assgn05.V03.1.Programming The RSA Alg
CS560-2023-Assgn05.V03.1.Programming The RSA Alg
COMPSCI 560
Introduction to Computer and Network Security
Acknowledgement: This assignment has been redesigned by Chang Zeng, one of the TAs for this course
in fall 2023. Thank you, Chang!
In this assignment you will write a program, in Python, for the RSA (public/private key) Encryption
Algorithm.
Page 1 of 5
Introduction to Computer & Network Security-CS 560
Introduction
Please consult page 84 of the textbook by Stallings for a description of the algorithm. A brief description
of the algorithm follows (taken from Stallings’ textbook)
Key Generation
Calculate n = p x q
Encryption
Plaintext: M<n
Ciphertext: C = Me (mod n)
Decryption
Ciphertext: C
Plaintext: M = Cd (mod n)
Your program should be contained in one (1) Python file named "rsa.py".
We will test your program with the values that we choose by directly calling your methods.
There are quite a few sample codes available online which implement the RSA algorithm. Keep in mind
that the code should be original and should not be copied from any source on the internet. You can
learn the logic from these resources, but please write your own program.
There will be a template file provided to you. It contains the RSA class with unimplemented methods.
Page 2 of 5
Introduction to Computer & Network Security-CS 560
e Public key
d Private key
1- Implement three methods which are “encrypt”, “decrypt”, and “compute_key”. You are expected to
accept the specified input and produce the specified output.
Instructions
1- Implement “compute_key” method. It should take in p (int), q (int), and coprime_index (int). p
and q are used to compute public key, coprime_index is used to fetch the parameter e that is
nth coprime with ф(n). You need to assign class variables p, q, n, phi, e, and d during the
process.
2- Implement “encrypt” method. It should only take in plain text (int) and output cipher text (int).
3- Implement “decrypt” method. It should only take in cipher text (int) and output plain text (int).
4- Test your problem with the p = 67, q = 83, coprime_index = 3, and plaintext = 24. You should
have e = 13, d = 1249, ciphertext = 3658.
Here are the general instructions on how your program should be executed.
5- Call compute_key method, take in 2 prime numbers, p and q, and the coprime_index.
6- Calculate n = p x q, and assign them to class variables, p, q, n.
7- Calculate the Euler totient of n, ɸ(n) = (p-1) x (q-1), and assign it to the class variable phi.
a. Note that n is not a prime number.
8- Calculate a list of eligible coprimes of ɸ(n).
a. A coprime of ɸ(n) is an integer less than ɸ(n) and relatively prime with ɸ(n).
b. Using Python, you may want to use a List to store the coprime values.
9- Choose the coprime_index-th smallest coprime; this is the e for your private key. For example,
ф(n) = 3120, coprime_index = 2 will have e = 11 as 3120 is coprime with 7 and 11. We will
Page 3 of 5
Introduction to Computer & Network Security-CS 560
ensure that for the choice of p and q in the test cases, there are at least coprime_index number
of coprimes, so there can never be an Index Error.
10- Calculate the matching d value such that d.e mod ɸ(n)=1.
11- Assign the calculated e and d to class variables e, d.
12- The public key is PU={e,n}, and the private key is PR={d,n}.
13- Call “encrypt” method, take in the message you will encrypt. Please note that, for simplicity, you
will be dealing ONLY with integer numbers, less than or equal to 50.
14- Encrypt the message with the public key (this is your encrypted)
15- Call “decrypt” method, take in the cipher text you just generated using “encrypt” method.
16- Decrypt the encrypted message with the private key (this is your decrypted).
17- Stop the program.
In principle, the core of your program can deal with any input, integers, or any alphanumeric text. To
deal with textual input, you first convert the text to its ASCII value and then deal with the input. This
usually results in large numbers which may require special programming to handle very large numbers.
Assume your program only deals with relatively small integers as input.
Rubrics
We will run your programs with our input values and compare them with the results which we have
calculated. There would be multiple computeKey-encrypt-decrypt iterations and examination on class
variables to consolidate the correctness of program.
Please note that if your program crashes on our first message, you don’t get any points for this
homework.
We will randomly choose a few of the program submissions to check if any part of it is copied from a
program taken from the Internet. We will use an online program checker.
Testing
You are also provided with one test.py and one input.txt. You can test your rsa.py by running the test.py.
python3 test.py -i input.txt (test your program with selected parameters in input.txt)
Page 4 of 5
Introduction to Computer & Network Security-CS 560
python3 test.py (test your program with parameter you specified in terminal)
Suppose the input file contains the following 12 lines (2 pairs of p and q and 5 messages apiece):
Notes:
• The assignment is auto graded, please follow the exact type notation specified in the template.
• Ensure that all numbers you produced are integers, not floating points.
• We will feed ALL programs with the same input and grade them based on the rubric mentioned
above.
• Your program should behave as specified in the assignment instruction.
• There is no need for extra instructions and submission.
• We will examine (i.e., look inside) a few of the submissions, randomly, and feed them to a
program to check for plagiarism.
Good luck!
Page 5 of 5