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 newKeys = new ArrayList(); static ArrayList output = new ArrayList(); static ArrayList plain = new ArrayList(); public static void main (String[] args) { loadCiphersandKeys(); fixKeys(); processCiphers(); generateOutput(); } 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()) { String st = input.nextLine(); if (cipherData) ciphers.add(st); else { keys.add(st); newKeys.add(st); } 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); 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++) { 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; if (cipherChar < keyChar) cipherChar += 26; int n = cipherChar - keyChar +64; char newChar = (char) (n); temp[i] = newChar; } result = new String(temp); plain.add(result); } } public static void generateOutput() { for(int x = 0; x < ciphers.size(); x++) { output.add(ciphers.get(x) +"/"+newKeys.get(x) + " = " + plain.get(x) ); } for(int x = 0; x < ciphers.size(); x++) { System.out.println(output.get(x)); } } }