Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this lab, you are provided with a program that contains bugs. Your task is to debug it and get the program to a working

In this lab, you are provided with a program that contains bugs.

Your task is to debug it and get the program to a working state.

You must rename the file crypto.pyand submit a working version.

'''crypto.py

A program implementing a simple encoding scheme.

The goal is to generate an unreadable stream of numbers

from some text input given a codec and an encryption key.

The same codec and key can be used to decrypt messages

created using the same encoding scheme.

The encoding scheme itself is based on a simple substitution

rule whereby each character is assigned a numeric value in

the codec. This numeric value is then added to the key during

encryption and the result modulo 100 (the number of printable

ascii characters) is returned. Decryption works in reverse.

'''

import string

def encode_char(char, ctx) -> int:

'''Generate a numeric value based on the

char argument

Args:

char(str): a single character to encode

ctx(dict): a dict containing the encryption

codec and key

Returns:

int: the generated numeric value

'''

codec = ctx['codec']

key = ctx['key']

return (codec[char] + key) % 100

def decode_char(code, ctx) -> str:

'''Retrieve the original character which

produced the code passed in

Args:

code(int): a integer produced by encoding

ctx(dict): a dict containing the encryption

codec and key

Returns:

str: the decoded character

'''

index = (code - ctx['key']) % 100

chars = list(ctx['codec'].keys())

return chars[index - 1]

def encode_msg(msg, ctx) -> str:

'''Encode a message based on the

provided encryption context

Each character in the message is encoded based

on the specified codec and key and the resulting

value is padded with zeros if it has less than 3 digits.

Args:

msg(str): the plain text to encode

ctx(dict): a dict containing the encryption

codec and key

Returns:

str: the encoded message

'''

encoded_msg = ''

for char in msg:

code = encode_char(char, ctx)

if len(code) != 3:

code = '0' * (3 - len(code)) + code

encoded_msg += code

return encoded_msg

def decode_msg(secret, ctx) -> str:

'''Decode a message based on the

provided encryption context

The message is decoded three characters

at a time yielding a zero-padded value of

length 3 which must be converted to an int.

Args:

secret(str): the encrypted message to decode

ctx(dict): a dict containing the encryption

codec and key

Returns:

str: the decoded message

'''

decoded_msg = ''

while secret:

code = secret[:3]

decoded_msg += decode_char(code, ctx)

secret = secret[3]

return decoded_msg

def main() -> None:

'''Create the codec and ask the user

to provide the encryption key, which is

a number between 1 and 99.

The key and code become the encryption

context, which is passed to the cipher

functions, i.e. encode_msg and decode_msg

'''

# create context

codec = {}

for index, char in enumerate(string.printable, start=1):

codec[char] = index

# Get encryption key from user

key = input('Enter a number between 1-99: ').strip()

# Encryption context

ctx = {

'key': key,

'codec': codec

}

original = '''

Cryptography is the study of secure communications

techniques that allow only the sender and the intended

recipient of a message to view its contents.

'''

print('Original message')

print('-' * 50)

print()

print(original)

secret = encode_msg(original, ctx)

print('Encoded message')

print('-' * 50)

print()

print(secret)

print()

message = decode_msg(secret, ctx)

print('Decoded message')

print('-' * 50)

print()

print(message)

if __name__ == '__main__':

main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+what were the US and the UR?

Answered: 1 week ago