Just read the first few, short paragraphs for the details: http://www.csee.usf....aesarCipher.pdf
I can't get the part where I need to make sure that ONLY letters are changed. Symbols, punctuation and basically anything that is not a letter needs to be kept the same.
Look at my "else if" statement in the code below:
import java.io.*;
public class main {
public static void main(String args[]) {
//get message and store it into "message"
String message = getMessage("text.txt");
String encrypt = "";
int key = 1;
//System.out.println("Test to see if file is being read)" + message); //test to see if file is being read properly
//Yes, file is read.
//We will loop thru each char and add the key to it.
for(int x = 0; x < message.length(); x++){
//Create a temporary char so we can add the key to it.
int temp;
//if there is a space, then add a space to the the encrypted message
if(message.charAt(x) == (' ')){
encrypt += " ";
}
//if there is a line break, add a line break
else if(message.charAt(x) == (char) 10){
encrypt += "\n";
}
//if the character is anything BUT a letter ex: !@#$%, don't encrypt it.
// else if(!(message.charAt(x).isLetter(a-z))){
//
// }
else{ //add key to char at X and store new char in encrypted message
temp = (int) (message.charAt(x) + key);
encrypt += (char) temp;
}
}
System.out.println("Encrypted Message: " + encrypt);
System.out.println("Original Message(test): " + message);
}
public static String getMessage(String location) {
String data = "";
try {
FileReader fr = new FileReader(location); // Opens the file
BufferedReader br = new BufferedReader(fr); //Sets up a buffered stream so that large files can be read
String line = null;
while((line = br.readLine())!= null && !line.equalsIgnoreCase("stop") ){ // Reads each line while 'line' is not null
data += line;
data += (char) 10; // Starts a new line, look at ASCII chart to see what 10 is
}
br.close(); // closes the file so other programs can read it
} catch (Exception e) {
e.printStackTrace(); // if the files does not exist or if the file is locked then it will let you know by giving an error
}
return data;
}
}


Sign In
Create Account


Back to top









