/** * ************************************************************** * This program is responsible for simple encryption using RSA. * It uses pre-existing keys of a short length. It is designed * to be modular, for possible later additions of other * algorithms, multiple pairs of plain- and ciphertexts, and * decryption/cryptanalysis within this main. * * ******************* * File: encryption.java * Associated classes: * Text * RSA * AttackRSA * Key * Prime * $id * ************************************************************ */ import java.lang.*; import java.io.*; /** * This is the primary class that is the center of whatever encryption, * decryption, or cryptanalysis activities are present. */ public class Encryption { public static boolean debug = false; // true to see all the substeps public static boolean stillValid = true; // if false, proceed to exit public static StringBuffer userInput = new StringBuffer(); public static int userSelection = 5; public static short badSelections = 0; public static Text currentCase = new Text(); public static InputStreamReader streamIn = new InputStreamReader(System.in); public static BufferedReader in = new BufferedReader(streamIn); public static void main (String args[]) throws IOException { while (stillValid) { // clean up before content of loop resetValues(); if (debug) System.out.println("badSelections " + badSelections); System.out.println("\n"); System.out.println("Enter 3 to try to break a cipher."); System.out.println("Enter 2 to decrypt using the existing key."); System.out.println("Enter 1 to encrypt using the existing key."); System.out.println("Enter 0 to quit."); System.out.print("Your selection: "); userInput.insert(0, in.readLine()); try { // catch an error if the input is not an integer userSelection = Integer.parseInt(userInput.toString()); }catch (NumberFormatException e) { if (debug) System.out.println("Main number error caught."); badUser(); continue; } try { // this is actually the main activity interactWithUser(); } catch (WrongInputLengthException e) { // applies only to case 2 System.out.println("Incorrect input length for this key."); badUser(); } catch (NumberFormatException e) { System.out.println("This takes only integers."); if (debug) System.out.println(e); badUser(); } } } // main /** * Prints out users instructions according to selection, and calls the * relevant class for the requested activity. Called by main. * * @throws IOException If error with input * @throws WrongInputLengthException If user inputs incorrect length */ public static void interactWithUser() throws IOException, WrongInputLengthException { switch (userSelection){ case 0: stillValid = false; break; case 1: System.out.println("Enter your text to encrypt with RSA."); System.out.println("No fancy characters, including accents."); System.out.println("Pressing a carriage return will begin" + " encryption."); currentCase.initializePlain(in.readLine()); System.out.print("Encrypting ..."); if (debug) System.out.println(); RSA.encrypt(); System.out.println("\nYour ciphertext is as follows:"); System.out.println(currentCase.cipherBlock); break; case 2: System.out.println("Enter your text to decrypt with RSA."); System.out.println("Enter the large integer to decrypt."); System.out.println("Pressing a carriage return will begin" + " decryption."); currentCase.initializeCipher(in.readLine()); System.out.print("Decrypting ..."); if (debug) { System.out.println("yo"); } RSA.decrypt(); System.out.println("\nPound signs at the end may not have" + " been part of the original message."); System.out.println("Your plaintext is as follows:"); System.out.println(currentCase.plainBlock); break; case 3: AttackRSA.attack(); break; default: // not a sanctioned entry, though an integer badUser(); break; } } /** * Keeps track of how many times the user has messed up, and eventually * just quits. Called by main or elsewhere. */ static void badUser() { // When dealing with a stupid or defiant user, don't even attempt // to explain. Just try it again. Eventually give up. if (badSelections < 5) { badSelections++; } else { stillValid = false; } } /** * Clears variable values all over the classes before repeating the loop. */ static void resetValues(){ userSelection = 5; userInput.delete(0, userInput.length()); currentCase.clearAll(); RSA.clearAll(); } } // class