Why is Vigenere cipher only encrypting part of message properly? We started off writing a Caesar cipher and graduated to a Vigenere cipher using the helper functions created for Caesar. My alphabet_position and rotate_character functions seem to be working, however my encrypt function for Vigenere is only returning part of the encrypted message properly.
For example,
print encrypt('BaRFoo',Baz)
should have returned: 'CaQGon'
actually returned: 'CakGo\x88'
Below is my code so far:
import string
def alphabet_position(letter):
return (ord(letter))
def rotate_character(char,rot):
encoded = ""
# loop over characters for ord
char_ord = alphabet_position(char)
# non-alphabet characters
if (char_ord > 90 and char_ord < 97) or char_ord < 65 or char_ord >122:
encoded = encoded + char
return encoded
else:
# set each according to whether upper or lower case
# ord of "Z" is 90, so 91 for upper range and ord of "A" is 65 so
uppercase boundary
if char.isupper():
asciirange = 91
asciibound = 65
else:
# ord of "z" is 122 so 123 for upper range and ord of "a" is 97
so lowercase boundary
asciirange = 123
asciibound = 97
enc_char = ((char_ord + rot) % asciirange)
if enc_char < asciibound:
enc_char = (enc_char + asciibound)
encoded = encoded + (chr(enc_char))
else:
encoded = encoded + (chr(enc_char))
return (encoded)
def encrypt(text,keyword):
encoded_text = ""
key_start = 0
# find rot
alpha = string.ascii_letters
for char in text:
# check if char is a letter
if char.isalpha():
key_num = (alphabet_position(keyword[key_start]))
# convert key_num to a letter to find its index
rot = alpha.find(chr(key_num))
encoded_text += (rotate_character(char,rot))
if key_start == (len(keyword)-1):
key_start = 0
else:
key_start += 1
else:
encoded_text += char
return encoded_text
Sorry, I'm not sure this is what you asked for, but i try ;) Hope it helps.