import java.util.Scanner; import java.io.PrintWriter; import java.io.File; import java.io.FileNotFoundException; /** * Performs a traditional Caesar cipher using text files for input and output. * @author Sarah Larkin * CS3090, Spring 2018 * Date LastModified: March 3, 2018 * */ public class TraditionalCaesarCipher { public static void main(String[] args) { TraditionalCaesarCipher self = new TraditionalCaesarCipher(); String original = self.readFile("TradCaesarCipherABC1Orig.txt"); String encrypted = self.shiftCipher(original, 3); self.printFile(encrypted, "TradCaesarCipherABC1Coded.txt"); } /** * Reads in a file and returns its contents as a string * @param filename the name of the file to read in, including its type * @return the contents string */ public String readFile(String filename) { String input = ""; try(Scanner scan = new Scanner(new File(filename))) { while(scan.hasNext()) { input += scan.nextLine() + "\n"; } } catch (FileNotFoundException e) { e.printStackTrace(); } return input; } /** * Performs a shift cipher on a string, using modulus to wrap around the * letters at the end of the alphabet. * @param input the string to cipher * @param shift the cipher amount * @return the cipher shifted string */ public String shiftCipher(String input, int shift) { String coded = ""; char [] message = input.toCharArray(); for (int i = 0; i < message.length; i++) { char letter = message[i]; letter = (char) cipherUppercase(letter, shift); letter = (char) cipherLowercase(letter, shift); message[i] = letter; } for (int i = 0; i < message.length; i++) { coded += message[i]; } return coded; } /** * Perform the cipher on an uppercase letter, keeping it in the letter range. * If the character given is not an uppercase letter, return the character unchanged. * @param letter the character to cipher * @param shift the amount by which to shift the letter * @return code the letter shifted */ private int cipherUppercase(char letter, int shift) { final int a = 65; // ASCII value for 'A' final int z = 90; // ASCII value for 'Z' int coded = letter; if ((a <= letter) && (letter <= z)) { // Get the value of the letter on a scale of 0 to 25 int azLetter = letter - a; int newLetter = azLetter + shift; // Add the shift int remainder = newLetter % 26; // Get the remainder coded = a + remainder; // Return to ASCII scale } return coded; } /** * Perform the cipher on an lowercase letter, keeping it in the letter range. * If the character given is not an lowercase letter, return the character unchanged. * @param letter the character to cipher * @param shift the amount by which to shift the letter * @return code the letter shifted */ private int cipherLowercase(char letter, int shift) { final int a = 97; // ASCII value for 'a' final int z = 122; // ASCII value for 'z' int coded = letter; if ((a <= letter) && (letter <= z)) { // Get the value of the letter on a scale of 0 to 25 int azLetter = letter - a; int newLetter = azLetter + shift; // Add the shift int remainder = newLetter % 26; // Get the remainder coded = a + remainder; // Return to ASCII scale } return coded; } /** * Prints a string to the specified file and the console. * @param fileOutput the string to print * @param filename the file to which to print the string */ public void printFile(String fileOutput, String filename) { try(PrintWriter print = new PrintWriter(new File(filename))) { print.print(fileOutput); System.out.println(fileOutput); } catch (FileNotFoundException e) { e.printStackTrace(); } } }