Python decrypting ciphers! Write a function called decryptstring that accepts a
ID: 3842020 • Letter: P
Question
Python decrypting ciphers!
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.
it needs to be able to decrypt anything put into this function:
def linearstring(plaintext, m, k):
return [linear(p, m, k) for p in plaintext]
def decryptstring( ciphertext, m, k):
these need to work:
decryptstring(b"s''6«ªEEð", 5, 11) should return [72, 101, 108, 108, 111, 32, 83, 105, 101, 114, 114, 97]
decryptstring(b'AB', 13, 50) should return [139, 80]
Explanation / Answer
cipher.py
keyval = 'abcdefghijklmnopqrstuvwxyz'
def encode(x, text):
"""This will encode the string and return the value"""
res = ''
for u in text.lower():
try:
y = (keyval.index(u) + x) % 26
res += keyval[y]
except ValueError:
res += u
return res.lower()
def decode(x, encryptText):
"""This will decode the string and return the text"""
res = ''
for u in encryptText:
try:
y = (keyval.index(u) - x) % 26
res += keyval[y]
except ValueError:
res += u
return res
def dispRes(text, x):
"""This will generate a Resulting cipher"""
encrypted = encode(x, text)
decrypted = decode(x, encrypted)
print 'Rotating: %s' % x
print 'text: %s' % text
print 'Encrytped Text: %s' % encrypted
print 'Decrytped Text: %s' % decrypted
Output:
Please rate the answer if it helped...
Hope this helps....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.