Use Python programing for the following problem. File Encryption/Decryption Prog
ID: 3880617 • Letter: U
Question
Use Python programing for the following problem.
File Encryption/Decryption Program: You are to design and write a program that allows a user to interactively select between encrypting or decrypting files * encrypting a user-specified text-file (.txt extension) using a Vigenere-cipher encryption scheme to generate a new encoded file with the same name, except with a .zzz extension. The Vigenere-cipher is a form of substitution cipher based on a user-specified keyword. see below example) * decrypting a user-specified Vigenere-cipher file (.zzz extension) using a user-specified keyword to generate a new text-file with the same name, except with a .txt extension Vigenere-cipher encryption example: For the keyword "hobwhite" and the subset of 32 characters shown in the top row helow (i.e., letters,space, nnew-line, , /comma, /period,/question-mark, and !Hexclamation-point), the Vigenere-cipher table would be Additional Positions row#| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|11112131415|16|17|18| 19202122232425 2627282930|31 ?!a 816 24 0 a 1018I 26 7. r s 1422 30 To encrypt a message with the keyword "bobwhite", the first character of message (at position 0) uses row 0 ('a' maps to a ;h', 'h' maps to a·c", etc.). The second character of the message (at position l ) uses row I ("a' maps to a , b' maps to a 'p', etc). Tf the message is longer than the keyword, then every 8th character (i.e., the length of "bobwhite") uses the same row (e.g., characters of the message at positions 1,9, 17, 25, 33, etc. al use row 1). All upper-case letters can he converted to lower-case before encryption. Any characters not in the top row can be completely ignored (i.e., discarded) without incrementing the position in the message The text-file message.txt (with new-line characters shown as in') would he encrypted to message. zzz and then decrypted back to decryptedMessage.txt as shown below: message.txt message zz2 decryptedMessage.txt Meet by the Union at noon today!hn -Sam rn eet by the union at noon today!r samn lints: . You don't actually need to create the Vigenere-cipher table shown above. Instead just use the position of each character from the overall sequence, and the position of each keyword character in the overall sequence Letter minO Position 012 3456 7 Letter b o b wh ite Letter seq You might also want to create a dictionary to enable quick lookup of characters position in the sequence (e.g. "m" 12, '?' . 30, etc.)Explanation / Answer
playing = True
string = ""
Alphabet = ('z','a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
def EncryptVigenere():
inputFilename = 'frankenstein.txt'
fileObj = open(inputFilename)
content = fileObj.read()
fileObj.close()
key = input("Please enter the key: ")
keyList = []
keyLength = 0
num = 0
while keyLength < len (content) :
try:
char = key[num]
except IndexError:
num = 0
char = key[num]
keyList.append(str(char))
keyLength += 1
num+= 1
CipherText = []
IndexValue = 0
keyIncrement = 0
for contentChar in content:
IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(contentChar)
while IndexValue > 26:
IndexValue = IndexValue - 26
CipherText.append(Alphabet[IndexValue])
keyIncrement = keyIncrement + 1
print (''.join(CipherText))
finish = input('Would you like to go again Y or N')
if finish == 'n' or finish == 'N':
retry = input ("Would you like to go again? Y or N: ")
if retry == 'N' or retry == 'n':
print ("Please exit the window")
import time
time.sleep(1)
import sys
sys.exit()
def DecryptVigenere():
inputFilename = 'frankenstein.txt'
fileObj = open(inputFilename)
content = fileObj.read()
fileObj.close()
keyList = []
keyLength = 0
num = 0
while keyLength < len(content):
try:
char = key[num]
except IndexError:
num = 0
char = key[num]
keyList.append(str(char))
keyLength += 1
num -= 1
CipherText = []
IndexValue = 0
keyIncrement = 0
for contentChar in content:
IndexValue = Alphabet.index(keyList[keyIncrement]) - Alphabet.index(contentChar)
while IndexValue > 26:
IndexValue = IndexValue - 26
CipherText.append(Alphabet[IndexValue])
keyIncrement = keyIncrement + 1
print (''.join(CipherText))
finish = input('Would you like to go again Y or N')
if finish == 'n' or finish == 'N':
retry = input ("Would you like to go again? Y or N: ")
if retry == 'N' or retry == 'n':
print ("Please exit the window")
import time
time.sleep(1)
import sys
sys.exit()
while playing == True:
keyIncrement = 0
string = ""
eord = input('Type "d" to "decrypt" and "e" to "encrypt": ')
if eord == 'e':
texte = input ("Type your word to encrypt: ")
key1 = int(input("Choose a key between 1-26: "))
for letter in texte:
number = (ord(letter)) + (key1)
letter=(chr(number))
string = (str(string)) + (str(letter))
print (string)
keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")
if keyword == 'encrypt' or keyword == 'e':
EncryptVigenere()
elif keyword == 'decrypt' or keyword == 'd':
DecryptVigenere()
elif eord == 'd':
texd = input ("Type your word to decrypt: ")
key2 = int(input("Choose a key between 1-16: "))
for letter in texd:
number = (ord(letter)) - (key2)
letter=(chr(number))
string = (str(string)) + (str(letter))
print (string)
keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")
if keyword == 'decrypt' or keyword == 'd':
DecryptVigenere()
elif keyword == 'encrypt' or keyword == 'e':
EncryptVigenere()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.