Write 2 programs in python using the following steps below. step 1: Vigenere Cip
ID: 3790154 • 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
package com.sanfoundry.setandstring; public class VigenereCipher { public static String encrypt(String text, final String key) { String res = ""; text = text.toUpperCase(); for (int i = 0, j = 0; i 'Z') continue; res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A'); j = ++j % key.length(); } return res; } public static String decrypt(String text, final String key) { String res = ""; text = text.toUpperCase(); for (int i = 0, j = 0; i 'Z') continue; res += (char) ((c - key.charAt(j) + 26) % 26 + 'A'); j = ++j % key.length(); } return res; } public static void main(String[] args) { String key = "VIGENERECIPHER"; String message = "Beware the Jabberwock, my son! The jaws that bite, the claws thatcatch!"; String encryptedMsg = encrypt(message, key); System.out.println("String: " + message); System.out.println("Encrypted message: " + encryptedMsg); System.out.println("Decrypted message: " + decrypt(encryptedMsg, key)); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.