Question: Python Help! Decrypt Function using a Multiplicative inverse function. Here is the code that I have so far, I have a multiplicative inverse function that

Python Help! Decrypt Function using a Multiplicative inverse function.

Here is the code that I have so far, I have a multiplicative inverse function that use an Extended Euclidean Algorithm function. I need to use these the multinv function to create a decrypt function. "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 256. You will need to compute the multiplicative inverse of m mod 256 to decipher c."

# Problem 1: Extended Euclidean Algorithm def egcd(a, b): if b == 0: return (1, 0) else: # Calculate q q = a // b # Calculate r r = a % b # Calculate (s, t) by calling egcd(b, r) (s,t) = egcd(b, r) return (t, s-q*t) # Problem 2: Multiplicative Inverse def multinv(a, n): g = egcd(a, n) return g[0] % n

# Problem 3: Decrypt a Single Value

def decrypt(c, m, k):

If it helps, the previous assignment had us create cipher functions:

# Shift cipher

# Shift a single plaintext byte by k (mod 256) def shift(b, k): return (b + k) % 256 # Shift a whole byte string by k, returning an array of numbers def shiftstring(plaintext, k): return [ shift(p, k) for p in plaintext ] # Shift a whole byte string by k, returning a byte string def shiftcipher(plaintext, k): return bytes( shiftstring(plaintext, k) ) ### Write the three functions to implement the linear cipher below

def linear(b, m, k): return (b * m + k) % 256 def linearstring(plaintext, m, k): return [linear(p, m, k) for p in plaintext] def linearcipher(plaintext, m, k): return bytes(linearstring(plaintext,m,k))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!