0% found this document useful (0 votes)
24 views5 pages

CS560-2023-Assgn05.V03.1.Programming The RSA Alg

Uploaded by

lucoin.zh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

CS560-2023-Assgn05.V03.1.Programming The RSA Alg

Uploaded by

lucoin.zh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to Computer & Network Security-CS 560

COMPSCI 560
Introduction to Computer and Network Security

Programming (in Python) the RSA Algorithm


Version 03.1
Last updated in October 2023

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

Select p, q p and q both prime, p ≠ q

Calculate n = p x q

Euler totient of n, the number of positive


Calculate ф(n) = (p - 1) (q – 1)
integers less than n and relatively prime to n

gcd (ф(n), e) = 1; ф(n) and e relatively prime


Select integer e
1 < e < ф(n)

Calculate d de mod ф(n)= 1; d is inverse of e mod ф(n)

Public key KU = {e, n}

Private key KR = {d, n}

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.

RSA class (variables)

Prime number which used to compute the


p public and private key

Prime number which used to compute the


q
public and private key

Page 2 of 5
Introduction to Computer & Network Security-CS 560

N Modulus for public and private key

ф(n), the Euler totient of n, the number of


phi positive integers less than n and relatively
prime to n

e Public key

d Private key

RSA class (methods), which you need to provide the implementations

compute_key(p, q, coprime index):


Compute the public private key from p, q
None

Encrypt the plain text using class variables


encrypt(plaintext): ciphertext
(n, e) and output cipher text

Decrypt the cipher text using class variables


decrypt(ciphertext): plaintext
(n, d) and output plain text

In writing your program, please follow the following instructions.

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.

Grading (we try each method with 5 inputs):

• 30% for correct implementation for “compute_key” method.


• 20% for correct implementation for “encrypt” method.
• 20% for correct implementation for “decrypt” method.
• 30% if your program does not crash in any one of the iterations.

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

or you can do:

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy