Assignments‎ > ‎

Encryption, decryption

Due Friday November 9, 2012

Your goal in this project is to learn about encryption and decryption of text. First, you will encrypt and decrypt some text using a simple rotation cipher, called a Caesar cipher, and then a password-based cipher.  Finally, you will decrypt a phrase using frequency analysis. We are going to use Python for this project because it is so much easier you to use for manipulating text (You can use Java if you want, though).

You will not be submitting things to svn for this project. Instead, you will give me a printout with the answers I request below as well as your source code. The source and Python should fit on page.

Caesar cipher

The idea is to rotate characters n positions to the LEFT. For example a rot-1 maps a to b, b to c, etc...

abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyza

A rot-2 maps characters like this:

abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab

and so on. For this project, however, you will need a rotation map for the LATIN-1  character set of 256 characters. That's what the string.translate() method and Python wants to use and so 256 is convenient. That means that some letters could convert to punctuation and vice versa.

You should write some Python code that encrypts text using the following methods. For example, my solution uses the following functions that return strings:

def rot_encrypt(text, n):
...
def rot_decrypt(text, n):
...

The key for encryption and decryption in this case is simply the rotation number.

You will find things like the following useful

MAX_ASCII = 256
ascii = ''.join([chr(i) for i in range(0,MAX_ASCII)])

then you can use string.translate() to performs the forward and backward translations.

Deliverables

  • Print out the rot-3 encryption for "Attack at dawn!"
  • Print out the decrypted rot-4 text of "Exxego$ex$he{r%"
  • Print your Python (or Java) code

Password-based cipher

A password-based cipher uses a password to map characters. For example, if our passphrase is 'bonkers', then we could create a cipher alphabet like this:

abcdefghijklmnopqrstuvwxyz
bonkersacdfghijlmpqtuvwxyz

That won't work for punctuation, digits and so on. We need to add password to printable characters to create the cipher. Remove duplicate chars and we have our mapping.

# Put passwd on left and then rest of *printable* ascii

# Return unique string, which is cyper

def passwd_cypher_chars(passwd):

    seq = passwd + string.printable

    uniq = []

    [uniq.append(i) for i in seq if not uniq.count(i)]

    return uniq


Deliverables

  • Encrypt and print out "Attack at dawn!" with passphrase "honey badger"
  • Decrypt and print out "BRbdh~7p~ru3pjf3Z" with passphrase "honey badger"
  • Print your Python (or Java) code

Decryption using frequency analysis

Decrypt the following phrase. Do not cheat and use crackers on the net. 

SB GHN BCZN MGWGBUI KSWTNW'P GVVCQN RSP S BCHI, MWSJ SHM MCHLI QYJJIUGAN CH BUN ZNBWGKGACBSH GKNWS UGYPN CH HNR IGWT. SP HG GHN NXNW XCPCBNM, PUN PGGH JNQSZN WSBUNW AGHNAI.
GHN MSI S BWSMNPZSH SWWCXNM BG KSCHB S PCLH GH KSWTNW'P GVVCQN MGGW. CHPBNSM GV UNW HSZN, PUN USM UCZ KSCHB S PCHLAN RGWM: "LNHBANZNH"!

Remember, the first task is to compute character frequencies (you can look back at the notes we had a class). Then, replace the most common character in the encrypted text with E and so on. You might be wrong on some of the letters, because the phrase is so short and the frequencies will be off. you can find the character frequencies here. I suggest starting with the three most frequent letters from the sample and then try to make progress mapping them to one of the first three most popular English characters in general. Even though we are not doing by bi-gram character analysis, you can look at how often the most frequent letters in the sample appear before or after other letters or only after a few special letters. this can give you a good indication of whether a character in the sample represents a vowel or consonant. According to Simon Singh in "The Code Book", letters that represent vowels should appear before and after most of the other letters. He also points out that the consonant t, for example, is rarely seen before or after letters such as B, D, G, J,...

In order to make your life easier, use another Python mapping function that lets you specify what you think some the letters are and then translate the above encrypted text according to that mapping. For example, the most common letter in the sample is N. To try out what this looks like as E, you could create a hash map called cipher = {'N':'E'} and then do something like

''.join([cipher.get(c,c) for c in encrypted_text])

to get the partially decrypted string out.

Deliverables

  • Print out the decrypted string
  • Print your Python (or Java)
Comments