Question: Problem 2 Write a function called decrypt that accepts three numbers ( c , m , and k ) and returns the corresponding plaintext (
Problem
Write a function called decrypt that accepts three numbers and and returns the corresponding plaintext value Greatest Common Divisor, Extended Euclidean Algorithm
In Lesson you learned about the Euclidean Algorithm for computing the greatest common divisor. And in a recent lecture we learned about the Extended Euclidean Algorithm for computing the more general form of the greatest common divisor: gcd
The solution to this equation is a pair a tuple of integers x y
Here is a recursive definition by parts of the Extended Euclidean Algorithm, which I will call egcd:
egcdif otherwise, where:egcdmodq is the integer quotient
Let's turn this into a Python function. I've given you some hints in the comments below.
def egcda b:
if b :
return
else:
q a b
s t egcdb a b
return t s q t
# Run this cell to check your egcd function
assert egcd
assert egcd
Multiplicative Inverse
To be able to decrypt a linear cipher, we need to solve the following equation for p:
mod
After a little algebraic manipulation, we get
mod
But dividing by m would result in a fraction. Instead of dividing, we can multiply by the multiplicative inverse of m mod n Recall that mod
is the nomenclature for the multiplicative inverse of m mod n We rewrite the equation as:
mod
To compute the multiplicative inverse, we'll use the Extended Euclidean Algorithm! Recall that it returns the x y solution to gcd
It turns out that x is the multiplicative inverse of a mod b
Therefore, if we call egcdm n and get back the tuple x y the multiplicative inverse is just the x value.
Sometimes you get back a negative value for x; that's okay, but a positive integer would be better. To convert it to the congruent positive modular value, just do a final mod n For example, if we were trying to compute the multiplicative inverse of mod and we got back we could just calculate mod and return the desired value, to the caller.
To write the Python function, simply call egcda n obtain the first element of the tuple that you get back, do a final mod n on it and return the result.
With that working, we will be ready to decrypt the linear cipher!
Problem
Write the multinv function. It takes two numbers, a and n and returns the multiplicative inverse of mod
There are comments in the function below to help you.
def multinva n:
# Call egcda n and get back a pair of integers x y
x y egcda n
# Return the first element the x value modulo n
return x n
# Run this cell to check your multinv function
assert multinv
assert multinv
assert multinv
# Try it out! This should return
multinv
# Verify mod This should return
multinv
Decrypting the Linear Cipher
The functions you'll write below are similar to the ones you wrote for Lab except they decrypt instead of encrypt. You may want to open up that notebook and refer to them.
If all this seems daunting to you, relax. You have the pieces you need to write the functions; you just have to put them together. An essential aspect of problemsolving is taking inventory of what you already know and what you have available to use.
You've learned about lists and applying functions to elements of a list using map.
You know about byte strings: special arrays of integers that appear onscreen kind of like strings.
You know that byte strings can be accessed like lists; you can loop across them. Python's map makes this very easy.
You have a working function for calculating the mutiplicative inverse of an integer.
These are all the pieces you need to finish the programs. None of the functions below are more than one or two lines of code.
Problem
Write a function called decrypt that accepts three numbers c m and k and returns the corresponding plaintext p value as a number. You can assume the modulus n is You will need to compute the multiplicative inverse of mod
to decipher c; use your multinv function to help you.
The formula for calculating the plaintext is:
mod
where
is the multiplicative inverse of mod and
is
def decryptc m k:
# Use the multiplicative inverse function to obtain the inverse of k mod m
kinv multinvk m
# Decrypt the cipher using the formula c kinv mod m
return c kinv m
# Run this cell to check your decrypt function
assert decrypt
assert decrypt
assert decrypt
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
