In your vigenere.py file, there should only be these six functions! You should n
ID: 3679634 • Letter: I
Question
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. 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).
Try examples in which the key is longer or shorter than plaintext or ciphertext
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.
For example:
Plaintexts: This is cool!!
EncryptKey: baconbaconbaco
Ciphertext: uhkg js qbpl!!
Notice that we converted the uppercase letter to lowercase and ignored the punctuation and spacing.
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.
For example:
Ciphertext: Uhkg Js qbpl!!
DecryptKey: baconbaconbaco
Plaintexts: this is cool!!
Notice that we converted the uppercase letter to lowercase and ignored the punctuation and spacing.
Explanation / Answer
i have create six program according to your requrement. run all program one by one and check output
# This program return index position of that letter in the alphabet
# if index not found return -1
import string
def letter_to_index(letter):
try:
return string.lowercase.index(letter)
except:
return -1
print letter_to_index('ccc')
# This program return the letter at that position in the alphabet
# if index not found return ?
import string
def index_to_letter(index):
try:
return string.ascii_uppercase[index]
except:
return '?'
print index_to_letter(24)
# This program return return the encrypted letter
# in vigenere_encrypt first latter add 's' and in second add 'm'
# then return encrypt value e
# if plaintext letter or key letter are not in the alphabet, return the original plaintext letter
import string
def vigenere_encrypt(plain_letter, key_letter):
try:
if(plain_letter.isalpha() == True and key_letter.isalpha() == True):
string.lowercase.index(plain_letter)
key_letter = string.lowercase.index(key_letter)
plain_letter = plain_letter.lower()
secret = ""
for c in plain_letter:
if c in "abcdefghijklmnopqrstuvwxyz":
num = ord(c)
num += key_letter
if num > ord("z"):
num -= 26
elif num < ord("a"):
num += 26
secret = secret + chr(num)
return secret
else:
return plain_letter
except:
return plain_letter
print vigenere_encrypt('s','m')
# This program return return the encrypted letter
# in vigenere_decrypt first latter add 's' and in second add 'm'
# then return decrypt value e
# if ciphertext letter or key letter are not in the alphabet, return the original ciphertext letter
import string
def vigenere_decrypt(cipher_letter, key_letter):
try:
if(cipher_letter.isalpha() == True and key_letter.isalpha() == True):
string.lowercase.index(cipher_letter)
key_letter = string.lowercase.index(key_letter)
if (key_letter > 0):
key_letter = -key_letter;
cipher_letter = cipher_letter.lower()
secret = ""
for c in cipher_letter:
if c in "abcdefghijklmnopqrstuvwxyz":
num = ord(c)
num += key_letter
if num > ord("z"):
num -= 26
elif num < ord("a"):
num += 26
secret = secret + chr(num)
return secret
else:
return plain_letter
except:
return plain_letter
print vigenere_decrypt('e','m')
# This program return encrypted value acording to digite pass in encrypt('This is cool!!', 'baconbaconbaco')
# We use base64 for encrypt and decrypt any value
import base64
def encrypt(plaintext, key):
enc = []
for i in range(len(plaintext)):
key_c = key[i % len(key)]
enc_c = chr((ord(plaintext[i]) + ord(key_c)) % 256)
enc.append(enc_c)
return base64.urlsafe_b64encode("".join(enc))
print encrypt('This is cool!!', 'baconbaconbaco')
# This program return decrypted value acording to digite pass in decrypt('tsnM4o7L1IPS3dHNhJA=', 'baconbaconbaco')
# We use base64 for encrypt and decrypt any value
import base64
def decrypt(ciphertext ,key):
dec = []
ciphertext = base64.urlsafe_b64decode(ciphertext)
for i in range(len(ciphertext)):
key_c = key[i % len(key)]
dec_c = chr((256 + ord(ciphertext[i]) - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
print decrypt('tsnM4o7L1IPS3dHNhJA=', 'baconbaconbaco')