O'Reilly logo

Hands-On Cryptography with Python by Samuel Bowne

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing the Caesar cipher in Python

Let's go ahead and open the Terminal and follow these steps to implement Caesar cipher in Python:

  1. We will use Python in interactive mode first and then make a string that just has some letters in order to test this method:
>>> str = "ABCDE">>> str.find("A")0>>> str.find("B")1>>> exit()
  1. Because we understand how the string methods work, we'll exit and go into the nano text editor to look at the first version of our script:
$ nano caesar1.py
  1. When you run the command, you will get the following code:
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"str_in = raw_input("Enter message, like HELLO: ")n = len(str_in)str_out = ""for i in range(n):   c = str_in[i]   loc = alpha.find(c)   print i, c, loc,  newloc = loc + ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required