physics, maths & computer science

Enter the plaintext and the number of places to shift:

plaintext = raw_input("enter plaintext to encipher: ")
shift = int(raw_input("enter places to shift (1-26): "))

Prepare the plaintext (no spaces etc.); make a key with a suitable offset (shift).

import string
string = string.ascii_lowercase

plaintext = plaintext.lower()
plaintext = plaintext.replace(" ", "")

split1 = string[shift:]
split2 = string[:shift]
ceasar = split1+split2

alphabet = [chr(num) for num in range(97,123)]
ceasar = [letter for letter in ceasar]
crypto_key = zip(alphabet,ceasar)
crypto_key = dict((k, v) for k,v in crypto_key)

Convert from plaintext to ciphertext:

i = 0
cipher_text = ''

while i < len(plaintext):
pt = plaintext[i]
ct = crypto_key[pt]
cipher_text = cipher_text+ct
i = i+1

cipher_text = cipher_text.upper()
print "cipher text is: ",cipher_text

gives e.g.:
cipher text is: LIPPSASVPH

to decipher, go back the way you came in…

ciphertext = raw_input("enter ciphertext to decipher: ")
shift = int(raw_input("enter places to shift (1-26): "))

and:

ciphertext = ciphertext.lower()
split1 = string[shift:]
split2 = string[:shift]
ceasar = split1+split2

alphabet = [chr(num) for num in range(97,123)]
ceasar = [letter for letter in ceasar]
crypto_key = zip(alphabet,ceasar)
crypto_key = dict((v, k) for k,v in crypto_key)

convert from ciphertext to plaintext:

i = 0
plain_text = ''

while i < len(ciphertext):
ct = ciphertext[i]
pt = crypto_key[ct]
plain_text = plain_text+pt
i = i+1
print "plaintext is: ",plain_text

gives:
plaintext is: helloworld