Question: Memory: 1 7 9 . 8 MB / 5 1 2 MB Housekeeping Before starting on this assignment, import the csci 2 6 library. Execute

Memory: 179.8 MB /512 MB
Housekeeping
Before starting on this assignment, import the csci26 library. Execute the cell below.
from csci26 import *
Strings and byte strings
A string in python is a sequence of characters. A character is actually a Unicode character, whose integer values can range from 0 to 130,000 or so. We've seen some Unicode values in Lesson 1. For instance, the Unicode value for the emoji is 128663. Long story short: a Unicode character isn't a single byte, nor even two bytes. A Unicode character needs a variable number of bytes (anywhere from one to four) to store its value.
A byte string is similar to a regular string, but each character is exactly one byte whose value ranges from 0 to 255.
Here are some examples of byte strings. They are represented using the b prefix and quotation marks.
b'Hello'
b'Hello'
b"CSCI 26"
b'CSCI 26'
Internally, a byte string is stored as an array of integers, where each integer is in the range 0-255. But they get displayed as ASCII characters.
This is an important distiction:
Regular strings are arrays of characters
Byte strings are arrays of numbers
For example, the byte string b'Hello' is actually the array [72,101,108,108,111]. This gives us a really concise, compact way of representing arrays whose individual elements are just bytes.
For characters that have no printable ASCII equivalent (such as control characters like CTRL-V), the value is displayed in hex with \x prepended. For example, the array [23,72,200,12] can be displayed as the byte string b'\x17H\xc8\x0c'. The "H" in there is the real character H; the rest of them are displayed in hexadecimal (17, c8, and 0c). This allows any byte string to be completely represented by printable characters; nothing is hidden or unintelligible. We'll need this feature when we encrypt strings because often the encrypted ciphertext contains otherwise unprintable characters.
Shift Cipher
For the shift cipher we will use the entire range of bytes (256 values), so the modulus will be 256.
Recall that the formula for computing the ciphertext is:
c =(p + k) mod n, where n =256
We will start by defining a function called shift that takes a single plaintext byte value (p) and a shift amount (k). It returns the ciphertext value, calculated using the formula. It's a very simple function, but it allows us to verify that the formula is working.
Run the cell below to define the function.
def shift(p, k):
return (p + k)%256
Try out some examples.
shift(65,5)
# Should return 70
70
shift(234,58)
# Should return 36
36
Next we'll write a function called shiftCipher that takes the shift amount (offset) and a byte string (plaintext). It returns the encrypted byte string.
To write this function, we will use the lambda technique shown in Lesson 4.
def shiftPartial(k):
return lambda p : (p + k)%256
def shiftCipher(offset, plaintext):
return map(shiftPartial(offset), plaintext)
Try the shift cipher
The cells below apply the shift cipher to some plaintext byte strings.
shiftCipher(5, b'Hello')
# Returns b'Mjqqt'
b'Mjqqt'
shiftCipher(189, b'Wow!')
# Returns b'\x14,4\xde'
b'\x14,4\xde'
Linear cipher
Now that you've seen how to write a shift cipher in Python, try your hand at writing a linear cipher. Recall that the formula is:
c =(p * m + k) mod n, where n =256
To complete this assignment, you will need to write two functions.
Function: linear
Define a function called linear that takes three inputs: a plaintext number (p), a multiplier (m), and an offset (k). It returns the single number generated by the linear cipher formula.
This function will be similar to the shift function defined previously.
def linear(p, m, k):
# Compute the linear transformation
return (p * m + k)%256
# Test the linear function with assertions
assert linear(100,5,10,42)==256
assert linear(10,5,20,42)==70
assert linear(123,1,0,42)==123
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[7], line 2
1 # Test the linear function with assertions
---->2 assert linear(10,5,10,42)==256
3 assert linear(10,5,20,42)==70
4 assert linear(123,1,0,42)==123
TypeError: linear() takes 3 positional arguments but 4 were given
Function: linearcipher
Define a function called linearCipher that takes three inputs: a multipler, an offset, and a plaintext byte string (plaintext). It returns the ciphertext byte string.
This function is similar to the shiftCipher function written above. Use the lambda technique to also write a helper function called linearPartial.
def linearPartial(m, k):
return lambda p: linear(p, m, k)
def linearCipher(multiplier, offset, plaintext):
# Apply the linearPartial function to each byte in the plaintext
transformed_bytes = map(linearPartial(m
Memory: 1 7 9 . 8 MB / 5 1 2 MB Housekeeping

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!