+ Reply to Thread
Results 1 to 7 of 7

Thread: [cryptography / python] Caesar Shift encryption

  1. #1
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    [cryptography / python] Caesar Shift encryption

    I've been reading a book about cryptography recently. The book in question is "The Code Book - A Secret History of Codes and Code-Breaking" by Simon Singh (Amazon link). While there is no programming content in the book it covers the ideas needed to be able to come up with your own programs.

    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

    Code:
    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.

    Code:
    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[25] = c.

    Code:
    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.

    Code:
    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:

    Code:
    wklv lv d phvvdjh
    Due to the simple nature of the encryption, decryption works in exactly the same way.

    Code:
    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
    Code:
    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

    Code:
    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: [cryptography / python] Caesar Shift encryption

    Nice intro to encryption
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Jordan Guest

    Re: [cryptography / python] Caesar Shift encryption

    Very cool Encryption article. +rep!

  5. #4
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: [cryptography / python] Caesar Shift encryption

    This is a simple method of encryption and you done a great job of explaining it mate. Well done, I will look forward to your tutorial on code-breaking.

    Will it be written in Python too?

    EDIT: Forgot +rep
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  6. #5
    OrlandoBlue is offline Newbie
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Thumbs up Re: [cryptography / python] Caesar Shift encryption

    Nice work man!!!
    keep that way, i'll be looking for your tutorial.

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: [cryptography / python] Caesar Shift encryption

    I have the book you mentioned - fantastic read. I actually wrote a Vigenere cipher program last week...maybe I'll post it as an addendum to your series (not to steal your thunder, Hignar). It's in C, though.
    sudo rm -rf /

  8. #7
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: [cryptography / python] Caesar Shift encryption

    Quote Originally Posted by dargueta View Post
    I have the book you mentioned - fantastic read. I actually wrote a Vigenere cipher program last week...maybe I'll post it as an addendum to your series (not to steal your thunder, Hignar). It's in C, though.
    Don't worry about stealing my thunder, I really don't think there's all that much to steal. Besides I'd love to see your code.

    For those who've expressed interest in the follow up I promised, it is coming. I've been very busy at work and I've decided to drop both python and C# for the moment to learn C++. If I can find time this weekend I may try getting back into it by rewriting this in C++.
    If there's a new way, I'll be the first in line.

    But, it better work this time.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Hash Cryptography, why so speedy?
    By liamzebedee in forum General Programming
    Replies: 6
    Last Post: 06-21-2011, 11:59 PM
  2. Cryptography
    By Apprentice123 in forum General Programming
    Replies: 1
    Last Post: 04-17-2011, 04:02 PM
  3. [Python] Encryption Program - PyCrypt
    By ShadenSmith in forum Classes and Code Snippets
    Replies: 1
    Last Post: 07-18-2009, 06:34 PM
  4. Caesar IV
    By falco85 in forum Video Game Talk
    Replies: 3
    Last Post: 10-09-2006, 03:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts