import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class test { static ArrayList ciphers = new ArrayList(); static ArrayList keys = new ArrayList(); static ArrayList output = new ArrayList(); public static void main (String[] args) { loadCiphersandKeys(); fixKeys(); processCiphers(); } public static void loadCiphersandKeys() { File f = new File("cipher.txt"); Scanner input = null; boolean cipherData = true; try //try...catch is used when the possibility of an error could result and we want to catch it without causing a runtime error { input = new Scanner(f); //Scanner is build based on input from the text, not the console } catch (FileNotFoundException e) { e.printStackTrace(); } while (input.hasNext()) { if (cipherData) ciphers.add(input.nextLine()); else keys.add(input.nextLine()); cipherData = !cipherData; } input.close(); System.out.println(ciphers.size() + " ciphers loaded."); //outputs the size of the ArrayList built System.out.println(keys.size() + " keys loaded."); //outputs the size of the ArrayList built } public static void fixKeys() { for(int x=0; x < keys.size(); x++) { if(keys.get(x).length() < ciphers.get(x).length()) //adding letters to the key to make same length as cipher { String newKey = keys.get(x); System.out.println("Adjusting"); int keyLength = keys.get(x).length(); int cipherLength = ciphers.get(x).length()-1; int index = keys.get(x).length(); while(index <= cipherLength) { newKey = keys.get(x) + keys.get(x).substring(index % keyLength, index % keyLength + 1); keys.set(x, newKey); index++; } } else if(keys.get(x).length() > ciphers.get(x).length()) { int cipherLength = ciphers.get(x).length(); String newKey = keys.get(x).substring(0,cipherLength); keys.set(x, newKey); } } } public static void processCiphers() { for(int x =0; x < ciphers.size(); x++) { System.out.println(ciphers.get(x)); String result = null; char[] temp = new char[ciphers.get(x).length()]; for(int i=0; i < temp.length; i++) { int cipherChar = (ciphers.get(x).charAt(i) - 65); int keyChar = (keys.get(x).charAt(i) - 65); char newChar = (char) ((cipherChar - keyChar) % 26 + 65); System.out.println(cipherChar + " " + keyChar + " " + newChar); temp[x] = newChar; } result = new String(temp); output.add(result); System.out.println(result); } } }