Unfortunately, there isn't a lot of 'heavy' programming needed to encrypt and decrypt messages using simple methods. Code-breaking is where the serious programming comes into play. I hope to cover code-breaking in a tutorial in a little while!
The first type of encryption that is covered in the book is known as a Caesar shift (so called due to its use by Julius Caesar).
The Caesar shift is a substitute cypher where each letter in a message is replaced with a letter a predetermined number of steps down the alphabet.
For example if we use a shift of 3 we get the following substitutions
Plain: abcdefghijklmnopqrstuvwxyz Cipher: defghijklmnopqrstuvwxyzabc
Thus the message "this is a message" would be translated to "wklv lv d phvvdjh".
In order to write a program that will encrypt a message we have to first produce the cipher alphabet based on the desired number of steps.
stdalph = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] crypalph = 
After asking the user to enter the desired number of steps we can fill the cipher alphabet using modular arithmetic. Assuming a shift of 3, we want the first entry of crypalph to be 'd', the second to be 'e' and so on until we have crypalph = c.
shift = 3 for x in range(0,26): crypalph.append(stdalph[(x+shift)%26])
Now, given a message (either entered by the user or read from a file), we need to substitute each letter in the message with the corresponding letter from the cipher alphabet. This method of encryption only substitute the letters in the message (although it can clearly be extended to cover punctuation marks and white space) so we need to decide what to do with punctuation etc. One answer is to simply leave the punctuation in place. This method is the least secure as the punctuation marks could give anyone who intercepts the message additional clues to help crack the code. The second option is to remove all punctuation and white space.
Although the second method is more secure I have opted to use the first method as it allows me to use a nice feature of python. In order to determine whether or not to substitute an individual character we need to know if it's a letter of the alphabet or not. Thus all we have to do is see if each character is in stdalph. As python evaluates all none zero integers as true and zero as false we can use the count() function to return the number of times a character is in stdalph i.e. 1 for a letter of the alphabet and 0 for any other punctuation etc.
So the encrypt the message all we have to do is loop through the message a character at a time and make the substitution where necessary.
message = 'this is a message' cryptmessage ='' for x in message: if stdalph.count(x): cryptmessage += crypalph[stdalph.index(x.lower())] else: cryptmessage += x print cryptmessage
gives the output:
wklv lv d phvvdjh
Due to the simple nature of the encryption, decryption works in exactly the same way.
cryptmessage ='wklv lv d phvvdjh' message = '' for x in cryptmessage: if stdalph.count(x): message += stdalph[crypalph.index(x)] else: message += x print message
Giving the output
this is a message
Unfortunately the Caesar shift isn't a very secure method of encryption. With only 26 possible cipher alphabets (one of which is just the standard alphabet) it wouldn't take very long for someone to try each possible shift value and decipher the message. The strength of the cipher can be improved by the use of a keyword. Using the keyword "Codecall" we can produce a cipher alphabet as follows:
1) Remove duplicate letters from the keyword giving us "codeal"
2) Place the new keyword at the start of the cipher alphabet
3) Fill the remainder of the cipher alphabet with the letters of the alphabet in order starting with the letter after the last letter of the keyword, excluding those used in the keyword.
Thus, the cipher alphabet for the keyword "codecall" would be
Plain: abcdefghijklmnopqrstuvwxyz Cipher: codealmnpqrstuvwxyzbfghijk
Given the vast number of possible keywords or keyphrases that could be used a brute force attack on the encrypted message would take much longer than if a simple Caesar shift was used.
I hope to produce a further tutorial on the use of keywords and using cryptanalysis to break codes when the keyword or phrase isn't known.