Solve the Caesar Cipher problem using JAVA language Ask the user if they\'d like
ID: 3793983 • Letter: S
Question
Solve the Caesar Cipher problem using JAVA language
Ask the user if they'd like to encrypt or decrypt the file. Ask for the string key. Ask for the input and output filename.
The encryption is solved by adding the numeric values of the key to the input . This may mean that the key needs to be repeated many times. The numeric values of the key can be calculated by determining the offset of the letter into the alphabet (a=0, b=1, c=2, etc). Please note, that the key will only contain letters, the input may contain other characters, but the key will only contain letters and it is NOT case sensitive. Upper case will always convert to upper case. Lower case will always convert to lower case. For example, "Hello world!" + "Password" ="Weddk nrglv!"
A good definition for the cipher can be found here: http://www.cleavebooks.co.uk/trol/trolc05.pdf
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class CryptoUtils {
public static void main(String[] args){
// For Encrypt, input Y else N
String isEncryptDecrypt = JOptionPane.showInputDialog("Do you want to encrypt the file, input Y. For decrypting, input N?");
boolean isEncrypt = false;
if(isEncryptDecrypt.equalsIgnoreCase("Y")){
isEncrypt = true;
}
// Input key
String key = JOptionPane.showInputDialog("Input String Key?");
key = key.trim();
// Input file Name
String inputFile = JOptionPane.showInputDialog("Input File Name path?");
// Output file Name
String outputFile = JOptionPane.showInputDialog("Output File Name path?");
String alphabet="abcdefghijklmnopqrstuvwxyz";
//String key = "password";
ArrayList<Integer> values = new ArrayList<Integer>();
for(int iCount=0;iCount<key.length();iCount++){
values.add(alphabet.indexOf(key.toLowerCase().charAt(iCount)));
}
if(isEncrypt)
doenc(values, inputFile, outputFile);
else
doDecrypter(values, inputFile, outputFile);
}
public static void doenc(ArrayList<Integer> values,
String inputFile, String outputFile){
String alphabet="abcdefghijklmnopqrstuvwxyz";
String encryptedVal = "";
String line="";
ArrayList<String> encryptedValArr = new ArrayList<String>();
try {
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
while((line=reader.readLine())!=null){
String message = line.trim();
for(int iCount=0;iCount<message.length();iCount++){
int val = iCount;
if(iCount >= values.size()){
val = iCount - values.size();
}
boolean isUpper = false;
char caseCheck = message.charAt(iCount);
if(caseCheck>='A' && caseCheck<='Z'){
isUpper = true;
}
if(alphabet.indexOf(message.toLowerCase().charAt(iCount)) != -1){
encryptedVal += cipher(message.toLowerCase().charAt(iCount), values.get(val), true, isUpper);
}else{
encryptedVal += message.charAt(iCount);
}
}
encryptedValArr.add(encryptedVal);
encryptedVal = "";
}
reader.close();
FileWriter writer = new FileWriter(outputFile);
for(int iCount=0;iCount<encryptedValArr.size();iCount++){
writer.write(encryptedValArr.get(iCount));
writer.write(" ");
}
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void doDecrypter(ArrayList<Integer> values,
String inputFile, String outputFile){
String alphabet="abcdefghijklmnopqrstuvwxyz";
String decryptedVal = "";
String line="";
ArrayList<String> decryptedValArr = new ArrayList<String>();
try {
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
while((line=reader.readLine())!=null){
String message = line.trim();
for(int iCount=0;iCount<message.length();iCount++){
int val = iCount;
if(iCount >= values.size()){
val = iCount - values.size();
}
boolean isUpper = false;
char caseCheck = message.charAt(iCount);
if(caseCheck>='A' && caseCheck<='Z'){
isUpper = true;
}
if(alphabet.indexOf(message.toLowerCase().charAt(iCount)) != -1){
decryptedVal += cipher(message.toLowerCase().charAt(iCount), values.get(val), false, isUpper);
}else{
decryptedVal += message.charAt(iCount);
}
}
decryptedValArr.add(decryptedVal);
decryptedVal = "";
}
reader.close();
FileWriter writer = new FileWriter(outputFile);
for(int iCount=0;iCount<decryptedValArr.size();iCount++){
writer.write(decryptedValArr.get(iCount));
writer.write(" ");
}
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String cipher(char msg, int shift, boolean isEncrypted, boolean isUpper){
String s = "";
if(isEncrypted){
char c = (char)(msg + shift);
if (c > 'z')
s += (char)(msg - (26-shift));
else
s += (char)(msg + shift);
}else{
char c = (char)(msg - shift);
if (c < 'a')
s += (char)(msg + (26-shift));
else
s += (char)(msg - shift);
}
if(isUpper)
s = s.toUpperCase();
return s;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.