Write a program to lunch a brute-force attack on a ciphertext encrypted using th
ID: 3702753 • Letter: W
Question
Write a program to lunch a brute-force attack on a ciphertext encrypted using the Caesar Cipher. The program should be able to read a ciphertext stored in a file then print in another file all the possible plaintexts. Can you find a way to figure out which one from the resulted plaintexts is the original message? Write a program to lunch a brute-force attack on a ciphertext encrypted using the Caesar Cipher. The program should be able to read a ciphertext stored in a file then print in another file all the possible plaintexts. Can you find a way to figure out which one from the resulted plaintexts is the original message? Write a program to lunch a brute-force attack on a ciphertext encrypted using the Caesar Cipher. The program should be able to read a ciphertext stored in a file then print in another file all the possible plaintexts. Can you find a way to figure out which one from the resulted plaintexts is the original message?Explanation / Answer
The Caesar cipher is one of the earliest known and simplest ciphers. It is a type of substitution cipher in which each letter in the plaintext is 'shifted' a certain number of places down the alphabet. For example, with a shift of 1, A would be replaced by B, B would become C, and so on.
.......................................................................................................................................
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class CaesarCipher
{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
public static String decrypt(String cipherText, int shiftKey)
{
cipherText = cipherText.toLowerCase();
String plainText = "";
for (int i = 0; i < cipherText.length(); i++)
{
int charPosition = ALPHABET.indexOf(cipherText.charAt(i));
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0)
{
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
plainText += replaceVal;
}
return plainText;
}
public static void main(String[] args)
{
BufferedReader br = null;
BufferedReader br2 = null;
BufferedWriter bw = null;
FileWriter fw = null;
try{
br = new BufferedReader(new FileReader("B:\ciphertext.txt"));
//One way of reading the file
System.out.println("Reading the file using readLine() method:");
String contentLine = br.readLine();
while (contentLine != null)
{
System.out.println("chipher text"+contentLine);
String originalMessage =decrypt(contentLine, 3);
System.out.println("original text"+originalMessage);
fw = new FileWriter("B:\originalmessage.txt");
bw = new BufferedWriter(fw);
bw.write(originalMessage);
System.out.println("Done");
contentLine = br.readLine();
}
}//try
catch (IOException e)
{
e.printStackTrace();
}
}//main
}//class
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.