o CSCI/CMPE 1370

Lab: Cipher Text

Something more interesting with sequence data

The Problem

Here is the 2-key encryption algorithm we discussed in class:

  • Select two integer keys, key1 and key2
  • For each character in the original message (a string):
    1. Look up the ASCII value for that character
    2. Multiply that value by key1 and add key2
    3. Append the resulting number to the encrypted message (a list)

You know that key1 is between 3 and 7 (inclusive) and key2 is between 15 and 22 (inclusive). Write a program to help you crack the code! Here is my encoded message as a list of numbers:

[524, 644, 626, 212, 710, 626, 614, 704, 626, 716, 212, 602, 680, 650, 674, 602, 668, 212,  686, 632, 212, 716, 644, 626, 212, 620, 602, 746, 212, 650, 710, 212, 602, 212, 692, 626, 680, 638, 722, 650, 680, 296]

Part I. Simplify and Decompose

If the answer isn't immediately obvious, break it down. For this part, we will simplify (assume you do know the keys) and decompose (focus on one character only).

If key1 is 5 and key2 is 17:

  • The letter 'c' encrypted is 512
  • The letter 'f' encrypted is 527
  • The letter 'v' encrypted is 607
key1 = 5
key2 = 17
test_char = 512

# write code to print the decrypted character corresponding to test_char
#  make sure it works for all three examples above



Attach your solution to each part in its own Python file.

Part II. Just Simplify

Building on what you did above, still simplify (assume you do know the keys), but do an entire list of numbers.

If key1 is 5 and key2 is 17:

  • The word 'bubba' encrypted is [507, 602, 507, 507, 502]
  • The word 'shark' encrypted is [592, 537, 502, 587, 552]
  • The phrase 'meep meep' encrypted is [562, 522, 522, 577, 177, 562, 522, 522, 577]
key1 = 5
key2 = 17
test_list = [507, 602, 507, 507, 502]

# write code to print the decrypted word or phrase corresponding to test_list
#  make sure it works for all three examples above



Attach your solution to each part in its own Python file.

Part III. And Then the Real Thing

You know how to decrypt a list of numbers if you know the keys. How do you do it if you don't? As we said in class, one simple approach is to try them all and see if one makes sense. That would be a pain, but computers are good at that kind of thing.

Now, clearly, whatever you did in Part II, you could just do over and over, manually, with different values for the keys. But you know that loops are there for exactly that kind of thing. If you wanted to do something with the numbers 4, 5 and 6, you might do something like:

for x in [4,5,6]:
  y = x * x
  print(y)

What we haven't commented on yet is the fact that the second and third lines above are both indented. Consider how this would be different, then run them both.

for x in [4,5,6]:
  y = x * x
print(y)

The indented stuff is called a block, and the implication is that everything in the block is what is being repeated. Here's the thing about the language being consistent: a block is a sequence of statements, and any statement can be in a block. Consider and run this one:

for x in [4,5,6]:
  output = ""
  for i in ['a', 'b', 'c']:
    output = output + " " + str(x) + i
  print(output)

Write Python code using loops to try out all the key combinations given in the problem statement at the top, in order to decrypt my secret message. In a comment in the file, state what the decrypted message is and what the two keys are.

Attach your solution to each part in its own Python file.