Write a program called vigenere.py that can take text both encode and decode a V
ID: 3678531 • Letter: W
Question
Write a program called vigenere.py that can take text both encode and decode a Vigenere ciphertext based on a given key word. For all given text, convert all letters to lowercase for processing. For the purposes of this assignment, assume ‘alphabet’ refers to only lowercase a-z (no symbols or uppercase). In your vigenere.py file, there should only be these six functions! You should not make any calls to input here for a user to type anything in. This file is just a library of functions.
1. letter_to_index(letter): Given a letter, return the index position of that letter in the alphabet (assuming ‘a’ is at position 0). If the letter provided is not one of the 26 letters of the alphabet, return -1.
2. index_to_letter(index): Given an integer value, return the letter at that position in the alphabet (again assuming ‘a’ is at position 0). If the index provided is not between 0 and 25 (inclusive), return a question mark.
3. vigenere_encrypt(plain_letter, key_letter): Given a letter to encrypt and a key letter, return the encrypted letter. If either the plaintext letter or key letter are not in the alphabet, return the original plaintext letter.
4. vigenere_decrypt(cipher_letter, key_letter): Given a letter to decrypt and a key letter, return the decrypted letter. If either the ciphertext letter or key letter are not in the alphabet, return the original ciphertext letter.
5. encrypt(plaintext, key): Given a plaintext phrase or sentence and a key word, return the phrase or sentence encrypted. For the purposes of this assignment, you should ‘line up’ the key exactly with the plaintext, including punctuation and spacing, even though the punctuation and spacing is not encrypted.
6. decrypt(ciphertext, key): Given a cipher text phrase or sentence and a key word, return the phrase or sentence decrypted. For the purposes of this assignment, you should ‘line up’ the key exactly with the ciphertext, including punctuation and spacing, even though the punctuation and spacing is not encrypted.
Explanation / Answer
Hey heres the code
letters={'a':0,'b':1,'c':2,'d':3,'e':4,'f':5,'g':6,'h':7,'i':8,'j':9,'k':10,'l':11,'m':12,'n':13,'o':14,'p':15,'q':16,'r':17,'s':18,'t':19,'u':20,'v':21,'w':22,'x':23,'y':24,'z':25}
indexes={0:'a',1:'b',2:'c',3:'d',4:'e',5:'f',6:'g',7:'h',8:'i',9:'j',10:'k',11:'l',12:'m',13:'n',14:'o',15:'p',16:'q',17:'r',18:'s',19:'t',20:'u',21:'v',22:'w',23:'x',24:'y',25:'z'}
def letter_to_index(letter):
if letter in letters.keys():
return letters[letter]
else:
return -1
def index_to_letter(index):
if index in indexes.keys():
return indexes[index]
else:
print '?'
def vigenere_encrypt(plain_letter, key_letter):
if plain_letter and key_letter in letters.keys():
i=(letter_to_index(plain_letter)+letter_to_index(key_letter))%26
return index_to_letter(i)
else:
return plain_letter
def vigenere_decrypt(cipher_letter, key_letter):
if cipher_letter and key_letter in letters.key():
k= letter_to_index(cipher_letter) - letter_to_index(key_letter)>0
if k>=0:
return index_to_letter(k)
else:
k=k+26
return index_to_letter(k)
else:
return cipher_letter
def encrypt(phrase,key):
word1=phrase.split()
word=''
total=''
for i in word1:
l=list(i)
print l
k=list(key)
print k
count=0
le=len(l)
for x in range(le):
if count>=len(k):
count=0
word=word+vigenere_encrypt(l[x],k[count])
count=count+1
total=total+word+' '
print total
def decrypt(phrase,key):
word1=phrase.split()
word=''
total=''
for i in word1:
l=list(i)
print l
k=list(key)
print k
count=0
le=len(l)
for x in range(le):
if count>=len(k):
count=0
word=word+vigenere_decrypt(l[x],k[count])
count=count+1
total=total+word+' '
print total
encrypt('hey hello','key')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.