Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will write a program that involve Python concepts like classes, decorators,

ID: 3904289 • Letter: Y

Question

You will write a program that involve Python concepts like classes, decorators, gen- erators and iterators. You will be writing an RSA encryption/decryption system that conforms to the following specifications.

Write a class called RSA that will contain all of the functions in the program. In the constructor forRSA initialize an empty list and set variables e and d to 0.

Write a function called inputFunc that reads in the number of entries from the user. Then, read in those many values and add them to the list.

Write a function called printFunc that takes in a number and prints “message is ” followed by the number.

Write a generator function called primeGen that will take a minimum value as a parameter and then yield the next prime numbers. Please note that this input parameter will be quite large and you might want to use long if you’re using Python 2.

Write a function called keyGen. This function will read in a minimum value from the user. Then, it will use the primeGen generator to get the next 2 prime numbers and generate the value N and the keys e and d. Print e and N but not the other values.

You would probably also need to write helper functions for the Lowest Common Multiple (LCM), Greatest Common Divisor (GCD) and the Totient function.

Write a function called encrypt that takes in a number as a parameter and returns the RSA encrypted value of the number.

Write a function called decrypt that takes in an encrypted number as a parameter and returns the RSA decrypted value.

Write a decorator function for printFunc that will print ”The encrypted ” before the printed message.

Write another decorator function for printFunc that will print “The decrypted ” before the printed message.

Write a function called messages that calls inputFunc and keyGen and then, uses an iterator to iterate through the list and encrypts each of the numbers using the encrypt function. Store the results in another list. Then, go through the second list and print each encrypted number using the decorator for the encrypted message.

Verify your results be decrypting each of the encrypted messages and checking if you get the old value back. Print the decrypted values using the decorator for the decrypted message.

In main, create an RSA object and call the messages function.

This section shows the sample output for the program. Please note that the exceptions raised when the iterator hits the end of the list is not included here, since I checked for the exception while generating the output. Please make sure you do so as well. Also, you might have different values even if you have the same inputs, depending on how ‘e’ is chosen. As long your decrypted message matched your initial input, you should be fine.

The RivestShamir Adleman system is a simple public key cryptosystem that is based on prime numbers. Here's a very basic outline of how to encrypt using RSA 1. Generate 2 prime numbers p and q. N-p*q, N is the modulus 2. The Totient Function ?(N)-(p-1)*(q-1). 3. Choose an integer e such that 1

Explanation / Answer

ScreenShot

---------------------------------------------------------

Program

#random number generation package
import random
#Eucludien method for finding GCD
def euclid(val1,val2):
    #avoid negative
    val1 = abs(val1)
    val2 = abs(val2)
    #Val1<valb then reverse value
    if val1 < val2:
        val1, val2 = val2,val1
   #loop for common divisor
    while val2 != 0:
        val1, val2 =val2, val1 % val2
        #return e value
    return val1
#checke generation GCD=1 means co-prime
def coprime(L):
     for i in range (0, len(L)):
        for j in range (i + 1, len(L)):
            if euclid(L[i], L[j]) != 1:
                return False
     return True
#to generate d value
def extendedEuclid(val1,val2):
    list2 = []
    if val1 < val2:
        small= True
        val1, val2 = val2, val1
    else:
        small = False
    while val2 != 0:
        list2.append((val1 % val2, 1, val1, -(val1//val2), val2))
       #Return a paired GCD
        val1, val2 = val2, val1 % val2
   #from there to find d
    list2.reverse()
    list2.pop(0)
    x = list2[0][1]
    y = list2[0][3]
  
    for i in range (1, len(list2)):
        x_temp = x
        y_temp = y
        x = y_temp * list2[i][1]
        y = y_temp * list2[i][3] + x_temp
      

    if (small != True):
        return (val1, x, y)
    else:
        return (val1, y, x)

def modInv(a,m):
    '''returns the multiplicative inverse of a in modulo m as a
       positve value between zero and m-1'''
    # notice that a and m need to co-prime to each other.
    if coprime([a, m]) == False:
        return 0
    else:
        linearcombination = extendedEuclid(a, m)
        return linearcombination[1] % m
#Prime check function
  
def checkPrime(pVal):
        if pVal > 1:
            for i in range(2,pVal):
                if (pVal % i) == 0:
                    return False
                else:
                    return True
#encrypt function
def encrypt(m,e):
        cypher=m**e%N
        return cypher
#decrypt function
def decrypt(c,d):
        original=c**d%N
        return original
#RSA class
class RSA:
    #constructor
    def __init__(self):
        self.d=0
        self.e=0
        self.N=0
        self.list1=[]
    #Take input list
    def inputFun(self):
        n=int(input("Enter the number of messages:"))
        print("Enter the messages:")
        for i in range(0,n):
            self.list1.append(int(input()))
  
     #print input list   
    def printFun(self):
        print("The message is:")
        n=len(self.list1)
        for i in self.list1:
            print(i)
   #Minimum prime
    def primeGen(self):
        pr=int(input("Enter the minimum value for the prime numbers:"))
        while(checkPrime(pr)==False):
            pr=int(input("Enter the minimum value for the prime numbers:"))
        return pr
    #keygeneration
    def keyGen(self,pr):
        p=pr+1
        while(checkPrime(p)==False):
            p=p+1
      
        q=p+1
        while(checkPrime(q)==False):
            q=q+1
      
        N=p*q
        print(N)
        T=(p-1)*(q-1)
        #Use Euclid's Algorithm to generate the public key key
        while True:
            e = random.randint(1, T)
            if coprime([e, T]):
                break
          
      
        #Use Extended Euclid's Algorithm to generate the private key
        d = modInv(e,T)
      
        #return key value
        return (N,e,d)
    #print encrypted data
    def printCypher(self):
        for i in self.list1:
            print("The encrypted message is:",encrypt(i,e))
     #print encrypted data
    def printOriginal(self):
        for i in self.list1:
            #val=encrypt(i,e)
            print("The decrypted message is:",decrypt(encrypt(i,e),d))

#Test ,Object creation and function call
r=RSA()
r.inputFun()
r.printFun()
minPrime=r.primeGen()
N,e,d=r.keyGen(minPrime)
print("N is:",N)
print("e is:",e)
print("d is:",d)
r.printCypher()
r.printOriginal()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote