Write 2 programs in PYTHON using the following steps below. step 1: Vigenere Cip
ID: 3791531 • Letter: W
Question
Write 2 programs in PYTHON using the following steps below.
step 1: Vigenere Cipher
Write a program named vigenerecipher.py that takes a file name and a code word/phrase as arguments. Open the file and use the Vigenere cipher to encode the contents of the file according to the cipher. The program should output the cipher text into ‘filename’-cipher.txt where ‘filename’ was the original file name. For example, if the file’s name is message.txt the ciphered text would be saved in the message-cipher.txt
The Vigenere cipher is polyalphabetic substitution cipher. It is variation of Caesar’s cipher where the alphabet is shifted by a fixed number of letters. For example, if the alphabet is shifted by eight, then the upshifted alphabet (first line) will map the following letters (second line):
ABCDEFGHIJKLMNOPQRSTUVWXYZ
IJKLMNOPQRSTUVWXYZABCDEFGH
The Vigenere cipher uses 26 alphabets that are each progressive shifted from 0 to 26. The code word provides an index into which one of the shifted alphabets will be used to cipher each letter of the clear text message into cipher text. For example, the clear text is “The eagle has landed” and the code word is “lime.”
The above text would be ciphered to:
Epq iloxi sie plvpio
Step two: Vigenere De-cipher
Write a program named vigeneredecipher.py that takes a file name and a code word/phrase as arguments. Open the file and use the Vigenere cipher to decode the contents of the file according to the cipher. The program should output the clear text into ‘filename’-clear.txt where ‘filename’ was the original file name. For example, if the input is message-cipher.txt the output of the program is saved in message-cipher-clear.txt
Explanation / Answer
ENCRYPTION CODE :
import sys
def main():
inputfile = sys.argv[1]
codeword = []
codeword = sys.argv[2]
cwIndex = 0
count = 0
original_msg = []
cipherList = []
codewordList = []
list1 = [] # list for ascii values (btwn 0 and 25) of original message
list2 = [] # list for ascii values (btwn 0 and 25) of codeword
list3 = [] # final cipher list
bitlist = []
for letter in codeword:
ascii_of_cw = ord(letter) - ord('a')
list2.append(ascii_of_cw)
inputfile.find('.')
outputfilename = inputfile[:inputfile.find('.')] + '-cipher' + inputfile[inputfile.find('.'):]
with open(inputfile, 'r') as ifh:
with open(outputfilename, 'w') as ofh: # creating the output file with 'open'
for line in ifh: # for each line in the input file
for char in line: # for each character in the line
if char.isalpha():
if char.isupper():
list1.append(ord(char) - ord('A'))
bitlist.append('upper')
else: # char.islower()
list1.append(ord(char) - ord('a'))
bitlist.append('lower')
list3.append((int(list1[count]) + int(list2[cwIndex])) % 26)
# Commented out the line below because it was used for testing purposes
#print('[{} {}]'.format(char, codeword[cwIndex]), end = '')
original_msg.append(char)
codewordList.append(codeword[cwIndex])
cwIndex = (cwIndex + 1) % len(codeword)
else: # char == ' '
list1.append(-88)
list3.append(-88)
original_msg.append(' ')
codewordList.append(' ')
bitlist.append(' ')
count += 1
i = 0
for value in list3:
if bitlist[i] == 'upper':
cipherList.append(chr(value + ord('A')))
elif bitlist[i] == 'lower':
cipherList.append(chr(value + ord('a')))
else:
cipherList.append(' ')
i += 1
# Commented out the lines below because they were used for testing purposes
#print(' list1: {}'.format(list1))
#print('list2: {}'.format(list2))
#print('list3: {}'.format(list3))
#print('original message: {}'.format(original_msg))
#print('codeword list: {}'.format(codewordList))
#print('cipher message: {}'.format(cipherList))
print(''.join(cipherList), file = ofh, end = '')
if __name__ == '__main__':
main()
DECRYPTION CODE :
import sys
def main():
inputfile = sys.argv[1]
codeword = sys.argv[2]
list1 = []
#list2 = []
#clearList = []
#count = 0
#cwIndex = 0
#for letter in codeword:
# list2.append(ord(letter) - ord('a'))
inputfile.find('.')
outputfilename = inputfile[:inputfile.find('.')] + '-clear' + inputfile[inputfile.find('.'):]
with open(inputfile, 'r') as ifh:
with open(outputfilename, 'w') as ofh:
for line in ifh:
for char in line:
if char.isalpha():
if char.isupper():
list1.append(chr((ord(char) - ord('A')) + 65))
else: # char.islower()
list1.append(chr((ord(char) - ord('a')) + 97))
#clearChar = ((int(list1[count]) - int(list2[cwIndex])) % 26)
#clearList.append(chr(clearChar))
#cwIndex = (cwIndex + 1) % len(codeword)
else: # char == ' '
#clearList.append(' ')
list1.append(' ')
#count += 1
# Commented out the lines below because they were used for testing purposes
#print(' list1: {}'.format(list1))
#print('list2: {}'.format(list2))
#print('clear list: {}'.format(clearList))
#print(''.join(clearList), file = ofh, end = '')
print(''.join(list1), file = ofh, end = '')
if __name__ == '__main__':
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.