Enter the plaintext and the key phrase for determining shift
plaintext = raw_input("enter plaintext to encipher: ")
shift = raw_input("enter key phrase: ")
prepare the plaintext and the key phrase:
import string
from sets import Set
string = string.ascii_lowercase
plaintext = plaintext.lower()
plaintext = plaintext.replace(" ", "")
first, part 1 of Caesar shift
shift = shift.lower()
shift = shift.replace(" ", "")
shift = [letter for letter in shift]
unique = reduce(lambda l, x: l+[x] if x not in l else l, shift, [])
shift1 = ''.join(unique)
Then second part
shift = Set(shift)
string = Set([letter for letter in string])
string_balance = string - shift
string_balance = list(string_balance)
string_balance = sorted(string_balance)
locate starting letter in the shift
last_letter = shift1[(len(shift1)-1):]
for start in last_letter:
result = [letter for letter in string_balance
if letter[0] >= start] + [letter for letter in string_balance if letter[0] < start]
string_balance = ''.join(result)
combine them to get key:
caesar = shift1+string_balance
print "caesar cypher is: ",caesar.upper()
compile a key
alphabet = [chr(num) for num in range(97,123)]
caesar = [letter for letter in caesar]
crypto_key = zip(alphabet,caesar)
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 "ciphertext is: ",cipher_text
gives:
caesar cypher is: JULISCAERTVWXYZBDFGHKMNOPQ
ciphertext is: ESWWZNZFWI
Then do the reverse to yield plaintext, i.e. symmetric
ciphertext = raw_input("enter ciphertext to decipher: ")
shift = raw_input("enter key phrase: ")
import string
from sets import Set
string = string.ascii_lowercase
ciphertext = ciphertext.lower()
ciphertext = ciphertext.replace(" ", "")
prepare keyphrase, as before
shift = shift.lower()
shift = shift.replace(" ", "")
shift = [letter for letter in shift]
unique = reduce(lambda l, x: l+[x] if x not in l else l, shift, [])
shift1 = ''.join(unique)
shift = Set(shift)
string = Set([letter for letter in string])
string_balance = string - shift
string_balance = list(string_balance)
string_balance = sorted(string_balance)
last_letter = shift1[(len(shift1)-1):]
for start in last_letter:
result = [letter for letter in string_balance
if letter[0] >= start] + [letter for letter in string_balance if letter[0] < start]
string_balance = ''.join(result)
caesar = shift1+string_balance
print "caesar cypher is: ",caesar.upper()
compile a key
alphabet = [chr(num) for num in range(97,123)]
caesar = [letter for letter in caesar]
crypto_key = zip(alphabet,caesar)
crypto_key = dict((v, k) for k,v in crypto_key)
convert from plaintext to ciphertext
i = 0
plain_text = ''
while i < len(ciphertext):
ct = ciphertext[i]
pt = crypto_key[ct]
plain_text = plain_text+pt
i = i+1
plain_text = plain_text.lower()
print "plaintext is: ",plain_text
Gives:
caesar cypher is: JULISCAERTVWXYZBDFGHKMNOPQ
plaintext is: helloworld