python decryption functions!
I have to create these functions:
3.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 3: Decrypt a single value
def decrypt(c, m, k):
...
4.Write a function called decryptstring that accepts a ciphertext byte string, a multiplier (m), and shift amount (k). It returns an array of integers containing the decrypted values.
# Problem 4: Decrypt a byte string into an array of ints
def decryptstring(ciphertext, m, k):
...
5.Finally, write a function called lineardecipher that accepts a ciphertext byte string, a multiplier (m), and a shift amount (k). It returns the corresponding plaintext byte string.
# Problem 5: Decrypt a byte string, returning a byte string
def lineardecipher(ciphertext, m, k):
...
Also the first two function that I created for this was a function for the Extended Euclidean Algorithm and a Multiplicative inverse function. Here they are:
# 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
If it helps I wrote these encrypting functions in my last assignment:
Write the three functions to implement the linear cipher below, these were shift ciphers.
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))