MATH 3600 HW19

2979 days ago by warner

MATH 3600 - HW19

Your name: _____________________________

Date: ________________



A Very Brief Introduction to Cryptography.

Cryotography deals with the task of communicating secrets to those and only those with whom you want to share them with, while keeping them secret from everyone else. We start with a plaintext message, encrypt it and then transmit the ciphertext. The receiver then decrypts the ciphertext to recover the plaintext message. 

One of the oldest and simplest encryption systems is the Caesar cipher.  Imagine two concentric rings, each enscribed with a copy of the alphabet with each letter in the same corresponding position. Rotate the inner ring by an amount agreed to by the sender and the receiver. Then for each letter in the plaintext, find the letter on the outer ring and write down the corresponding letter on the inner ring.  As long as your alphabet is realtively small, the Caesar cipher can be implemented physically as just described.

A variation of the Caesar cipher that has been used on the Usenet to encrypt punch lines on jokes is the Rot13 cipher.  This rotates the 26 letters of the lower case alphabet by 13 letters, the 26 letters of the uppercase alphabet by 13 letters, and leaves digits and punctuation alone. Note that applying Rot13 a second time restores the plaintext.

For an excellent introduction to these topics check out William Stein's book, "Elementary Number Theory: Primes, Congruences, and Secrets".

The following blocks and exercises will provide basic encryption and decryption functions, which implement Rot13 and provide programming examples that can be useful in more elaborate cryptographic projects.

# Plaintext using a multi-line string plaintext = '''Now is the time for all good men to come to the aid of their country. ''' print plaintext 
# Retrieving the ascci code for each character in the plaintext for c in plaintext: print ord(c),c 
# Create a list of ascii codes ascii_list = [78,111,119,32,105,115,32,116,104,101,32,116,105,109,101] # Print the characters as a string s = '' for c in ascii_list: s += chr(c) print s 
def Rot13(plaintext): ciphertext = '' Acode = ord('A') Zcode = ord('Z') acode = ord('a') zcode = ord('z') for ch in plaintext: c = ord(ch) if Acode <= c <= Zcode: c = Acode + (c-Acode+13)%26 elif acode <= c <= zcode: c = acode + (c-acode+13)%26 else: pass ciphertext += chr(c) return ciphertext 
ciphertext = Rot13(plaintext) print ciphertext print Rot13(ciphertext) 

Exercise 1.  Use the Rot13 function to decrypt the following multi-line string.

gibberish = ''' Wnoorejbpxl ol Yrjvf Pneebyy 'Gjnf oevyyvt, naq gur fyvgul gbirf Qvq tler naq tvzoyr va gur jnor; Nyy zvzfl jrer gur obebtbirf, Naq gur zbzr enguf bhgtenor. "Orjner gur Wnoorejbpx, zl fba Gur wnjf gung ovgr, gur pynjf gung pngpu! Orjner gur Whowho oveq, naq fuha Gur sehzvbhf Onaqrefangpu!" Ur gbbx uvf ibecny fjbeq va unaq; Ybat gvzr gur znakbzr sbr ur fbhtug— Fb erfgrq ur ol gur Ghzghz gerr, Naq fgbbq njuvyr va gubhtug. Naq, nf va hssvfu gubhtug ur fgbbq, Gur Wnoorejbpx, jvgu rlrf bs synzr, Pnzr juvssyvat guebhtu gur ghytrl jbbq, Naq oheoyrq nf vg pnzr! Bar, gjb! Bar, gjb! Naq guebhtu naq guebhtu Gur ibecny oynqr jrag favpxre-fanpx! Ur yrsg vg qrnq, naq jvgu vgf urnq Ur jrag tnyhzcuvat onpx. "Naq unfg gubh fynva gur Wnoorejbpx? Pbzr gb zl nezf, zl ornzvfu obl! B senowbhf qnl! Pnyybbu! Pnyynl!" Ur pubegyrq va uvf wbl. 'Gjnf oevyyvt, naq gur fyvgul gbirf Qvq tler naq tvzoyr va gur jnor; Nyy zvzfl jrer gur obebtbirf, Naq gur zbzr enguf bhgtenor. ''' 

Exercise 2:  In the next block copy five or more lines of text and create a multi-line plaintext message.  Also include your name as the last line of the message. Then in the subsequent block, encrypt your plaintext message using Rot13 and print the ciphertext.


Exercise 3:  Using Rot13 decrypt the message from the previous exercise. Indicate any and all changes introduced by this two step process.