Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python question using thonny : [your name here] # McGill ID: [your ID here] # Assignment 1, Question 3 import random # Given a dictionary

python question using thonny image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

: [your name here] # McGill ID: [your ID here] # Assignment 1, Question 3

import random

# Given a dictionary d and value v, # find and return the key associated with that value. # (From the slides.) def reverse_lookup(d, v): for key in d: if d[key] == v: return key return None

LETTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()`~-=_+[]{}|;\':",./? ' def create_cypher_dictionary(): random.seed(1337) # important - please keep this line here. cypher = {} # create empty dictionary # add code here return cypher # return the completed cypher dictionary

def encrypt(s): cypher = create_cypher_dictionary() # get the cypher dictionary # add your code here

def decrypt(s): cypher = create_cypher_dictionary() # get the cypher dictionary # add your code here

def encrypt_multiple_times(s, n): pass # delete this line and add your code here

# Source: https://en.wikipedia.org/wiki/Most_common_words_in_English COMMON_WORDS = [" the ", " be ", " to ", " of ", " and ", " a ", " in ", " that ", " have ", " I ", " it ", " for ", " not ", " on ", " with ", " he ", " as ", " you ", " do ", " at "]

def decrypt_multiple_times(s): cypher = create_cypher_dictionary() # get the cypher dictionary # add your code here

s = input("Enter text to encrypt: ")

# add your code here

print("Encrypted string:", encrypt(s)) print("Decrypted string:", decrypt(encrypt(s)))

salted_s = encrypt_multiple_times(s, 2) print("Encrypted x2 string:", salted_s) print("Decrypted x2 string:", decrypt_multiple_times(salted_s))

* 48% Sat 7:41 PM COMP_208 Winter_2020 Assignment2.rev3.pdf (page 7 of 12) Q Search Question 3 (40 points) In cryptography, a simple substitution cypher is a method of encryption in which each letter of a phrase is replaced by a different letter or sequence of letters. The phrase can then be decrypted by performing the inverse of the substitution. In this question, we will implement a simple substitution cypher in which a character (letter, number, or special symbol) is substituted by a two-digit number between 00 and 99 (inclusive). For example, the letter 'a' could be encrypted to become the number '06', or the special symbol l' could become the number '57'. We will use a dictionary to store the mapping between each character and its corresponding en- crypted form. That is, the keys of the dictionary will be the regular characters (letters, numbers and special symbols), and the values will be the encrypted forms of the characters (the two-digit numbers between 00 and 99) Your code should implement the following five functions for this question. Name: create cypher dictionary Parameters: No parameters. Return value: A dictionary where each key is a regular character and each value is a random two-digit number between 00 and 99, inclusive. What it should do: At the top of the code provided to you, the string LETTERS is defined. This string contains all the letters, numbers and special symbols that should be keys in the dictionary. You will have to loop over this string, and generate a two-digit random number between 00 and 99 for each character. The two-digit numbers will be the values in the dictionary; each character (key) will have one two-digit number (value). Note that the numbers 0, 1, 2, ..., 9 should not be values; instead, they should be the numbers 00, 01, 02, ..., 09. Further, note that each character should have a unique random number. That is, if the character 'a' maps to the value '57', no other character should map to the value '57'. (You will need to use some sort of loop to keep generating new random numbers until a unique one is found.) ols Window Help % 48% Sat 7:41 PN COMP_208 Winter 2020 Assignment 2 fev3.pdf (page 7 of 12) Name: encrypt Parameters: A string s. Return value: The encrypted version of the string s. Each character of the original string s should be replaced by its corresponding two-digit number in the cypher dictionary. Name: decrypt Parameters: A string s. Return value: The decrypted version of the strings. Each two-digit number of the encrypted string s should be replaced by its correponding character in the cypher dictionary. Note that here, we have the encrypted form (value in the dictionary), and are looking to find the regular character (key in the dictionary). To do this, we will need to use the reverse lookup function as seen in class. This function has been provided to you in the included encryption.py file. We will write two further functions to increase the strength of our encryption. One function will repeatedly encrypt the string multiple times. The other function will attempt to decrypt the string, without knowing the number of times it was encrypted (thus defeating the point of the stronger COMP_208_Winter_2020 Assignment 2 rev3.pdf (page 8 of 12) Name: encrypt_multiple times Parameters: A string s and integer value n for the number of times to encrypt the string. Return value: The string s encrypted n times. Le., the string will be encrypted a first time, with each character turned into its two-digit representation. Then, the encrypted string will be re- encrypted, with each digit turned into its two-digit representation. (The length of the string will thus double after every encryption.) (Hint: You will have to call your encrypt function multiple times.) Name: decrypt multiple times Parameters: A string s. Return value: The decrypted version of the string s. As we do not know how many times to decrypt the string, we will keep calling decrypt on the string until the string contains a common word in the English language. A list of common words, COMMON_WORDS, has been provided for you in the encryption.py file. If, after decrypting once, the string contains any word in this list, then you should immediately return the decrypted string. Otherwise, continue calling decrypt on the string until it does contain one of the common words. (We will assume that the fully decrypted string will always contain at least one common word.) Finally, at the end of the file encryption.py, you will notice some code already written for you. This code asks the user to input a string, then calls the various functions and prints what they return (to produce the examples seen below). You must alter this code so that if the user enters a string that does not contain any of the words in the COMMON WORDS list, then the program should output "Invalid input." and not execute the rest of the code. Otherwise, the program should continue with the rest of the code. (Hint: Think back to Assignment 1 and if/else branches.) Filename You must write this program in file called encryntion.my o Tools Window Help 48 COMP 201 Winter 2020 Assignment2jev3.pdf (page 9 of 12) Sat 7:45 Examples (as executed in Thonny) EXAMPLE 1: >>> Run encryption.py Enter text to encrypt: abcdef Invalid input. EXAMPLE 2: >>> "Run encryption.py Enter text to encrypt: The quick brown fox jumps over the lazy dog. Encrypted string: 0766546138001892056159999714256163973761640073940161973354996 - 169665461919809956156975155 Decrypted string: The quick brown fox jumps over the lazy dog. Encrypted x2 string: 3417626247536227487834342778713634476227477171717117275336 476227624871174817622762533434174871533427622771174848475371716227627162624 - 7536227712771783471714762274762711747274747 Decrypted x2 string: The quick brown fox jumps over the lazy dog. EXAMPLE 3: Enter text to encrypt: - No, not again. I... why does it say paper jam when there is no paper jan? Encrypted string: 129746612597696198519818255561705555556114669561569754016118696101989561949 - 89454996164987361146654256169665499546118016125976194989454996164987390 Decrypted string: No, not again. I... why does it say paper jas when there is - no paper jam? Encrypted x2 string: - 273671175362622736477117627162277178472771782778364747476227173447474747474 762272753526271476227476271174753342762272778627162273427717871476227715371 Window Help 48% D Sat 7:42 COMP_208 Winter_2020 Assignment2.reva.pdf (page 9 of 12) EXAMPLE 3: >>> Run encryption.py Enter text to encrypt: No, not again. I... why does it say paper jam when there is no paper jam? Encrypted string: 129746612597696198519818255561705555556114669561569754016118696101989561949 - 89454996164987361146654256169665499546118016125976194989454996164987390 Decrypted string: No, not again. I... why does it say paper jam when there is - no paper jam? Encrypted x2 string: - 273671175362622736477117627162277178472771782778364747476227173447474747474 - 762272753626271476227476271174753342762272778627162273427717871476227715371 - 787153475371716227625371781748622727536262475336476227627162624753717147536 2272778342762273647711762277153717871534753717162276253717817487134 Decrypted x2 string: No, not again. I... why does it say paper jam when there - is no paper jam? Thonny File Edit View Run Device Tools Help Dy 56:54 encryption.Dy parentheses.py Author: your name herel McGill ID: Iyour ID here! Assignment 1. Question 3 import randon 7 10 Given dictionary and value ve find and return the key associated with that value. (From the slides.) def reverse Lookupid, v): for key in d: ir dikel : return key return None _01011 "./07. LETTERS - abcdefghijklmnopqrstuvwxyzABCDEFGHIJKUNOPORSTUVWXYZ1234567890106 def create cypher dictionary(): random.seed (1337) portantolease keep this line her Cypher create aty dictionary add cadere dictionary return cypher return the coupleted def encrypt(s): cypher = create_cypher_dictionary) get the cypher dictionary 155K GA Thonny Hle Edit View Run Device Tools Help Thonny Users abertos Downloads encryption by 56:64 encryption.py parentheses.py # add your code here def decrypt(s); cypher = create cypher dictionary() = get the cypher dictionary add your code here det encrypt multiple timesis, n): pass + delete this line and add your code here 39 Sources httpsillen.wikipedia.org/wiki Most common words in English and "a", in ", " that ", " have "," I", "it", "for " "not","on" det decrypt multiple times(s): Cypher create cypher dictionary() et the cypher dictionary add your code here 47 5 input("Enter text to encrypt: ") 19 dd your code bere 51 print("Encrypted string", encrypt(s)) 52 print("Decrypted stringi", decrypt (encrypt(s))) Python 3.7.5 und 90 D5 2. WAO AS MacBook Air w Run Device Tools Help in " " that have I t ", "for not #on , with he","as","you", "do", " at ") LEO WA ACTh MacBook Air Thonny File Edit View Run Device Tools Help Thonny - /Users/albertouknine/Downloads/encryption.py 56 encryption.py parentheses.py cypher = create_cypher_dictionary() # get the cypher dictionary add your code here def encrypt_multiple timess, n): pass delete this line and add your code here Source: https://en.wikipedia.org/wiki/Most common words in English 40 COMMON WORDS - the be to # of #Hand in that have " TH, "it" def decrypt_multiple_tines(s): cypher create_cypher_dictionary() get the cypher dictionary dd your code here 5 - input("Enter text to encrypt: ") add your code here print("Encrypted string:", encrypt(s)) 52 print("Decrypted string", decrypt (encrypt(s))) salted 5 encrypt_multiple timesls, 2) print("Encrypted x2 string:", salted_s) 55 print("Decrypted X2 string", decrypt_multiple_tines(salted_s)) Shell Python 3.7.5 (bundled)

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

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions

Question

What information do users need about current assets?

Answered: 1 week ago