This is for python. (Question 3, will post others separately.) # 3. Implement su
ID: 3799225 • Letter: T
Question
This is for python.
(Question 3, will post others separately.)
# 3. Implement sub_decrypt(password)
def sub_decrypt(password, ciphertext):
"""Decrypt ciphertext using the substitution cipher. If a
character is not in the key, the character remains unchanged.
The ciphertext should be normalized to all lowercase letters.
s = 'le tge svhpbkeges. cyee ti bape.'
sub_decrypt(s) # --> we are discovered. flee at once.
:param password: the password used to generate a key
:type password: str
:param ciphertext: the text to be decrypted
:type ciphertext: str
:return: the plain text that results from decrypting the ciphertext
:rtype: str
"""
# implement this function!
return ''
def vig_encrypt(key, message):
"""Encrypt a message using the vigenère cipher. Punctuation is
preserved.
:param key: the key used for encrypting the message
:type key: str
:param message: the message to be encrypted
:type message: str
:return: the ciphertext, as a string, produced by encrypting the message
:rtype: str
"""
cypher_text = ''
alphabet = gen_consecutive_chars()
key_len, alphabet_len = len(key), len(alphabet)
# or replace next two lines with...
# for i, ch in enumerate(message):
for i in range(len(message)):
ch = message[i]
# based on key... what row (labeled by letters) in table will we
# use? the example in the book shows a mapping of the key to a
# message:
# DAVINCIDAVINCIDAVINC
# the eagle has landed
# consequently, the row in we use for the first letter, t is D
# (row_letter is D for first letter, t)
row_letter = key[i % key_len].lower()
# calculate offset to simulate shifting letters for each row:
# again, using the first letter t, and D as the row letter...
# D is at position 3 of the alphabet, which means that the key
# (the alphabet) is shifted by 3: defghijklmnopqrstuvwxyzabcd
# ... let's save this shift in a variable called offset
offset = alphabet.index(row_letter.lower())
try:
# so now, we can translate our original character, ch, by
# finding out where it is in the offset row represented by D.
# simply add the offset to what ch's position would be in the
# alphabet (so if ch is t and the key specifies that the
# offset is 3, then the index of the character that t is
# translated to 19 + 3)...
other_index = (alphabet.index(ch) + offset) % alphabet_len
# if working with the letter and key mentioned above, t is
# translated to the letter at index 22 of the alphabet: w
cypher_text += alphabet[other_index]
except ValueError:
cypher_text += ch
return cypher_text
I don't understand how to implement this (or the others I posted seperately) function, can I get some kind of walkthrough on these? (Code + comments explaining how or why it's like that would be awesome.)
Explanation / Answer
Question is not clear . There are some missing functions like gen_consecutive_chars() etc if you can upload all the methods it will be helpful to understand the context completely and solving the problem will be easy in that way.
Thank you