/* * ************************************************ * Handles the plaintext and ciphertext. * * ********************** * File: Text.java * This class: Text * Main class: Encryption * * $id * ************************************************ */ import java.io.*; import java.lang.*; /** * Handles the storage of the ciphertext and plaintext, including dispensing it * to other methods. */ public class Text { public char[] plainArray = new char[RSA.chunkSize]; public StringBuffer plainBlock = new StringBuffer(); public StringBuffer cipherBlock = new StringBuffer(); public int cipherSegment = 0; /** * Stores the user-inputed plaintext into the StringBuffer plainBlock. * * @param input User input which is passed from Encryption. */ public void initializePlain(String input){ plainBlock.insert(0, input); } /** * Carries out a padding scheme so the plaintext may be chunked properly. * Called by RSA.encrypt. */ public void addMuck(){ // makes the input string be the correct length if (Encryption.debug) System.out.println("pbln " + plainBlock.length()); int toFill = 3 - (plainBlock.length() % RSA.chunkSize); if (Encryption.debug) System.out.println("toFill " + toFill); for (int i = toFill; (0 < i) && (i < RSA.chunkSize); i--){ plainBlock.append("#"); // padding } if (Encryption.debug) System.out.println("addMuck " + plainBlock); } /** * Converts a part of the plainBlock into the plainArray. Called by * RSA.encrypt. * * @param position Where RSA's encryption is in the plainBlock */ public void unpackPart(int position){ plainBlock.getChars(position, position + RSA.chunkSize, plainArray, 0); // keep recycling the array -- no offset if (Encryption.debug) { System.out.print("unpackPart"); for (int i = 0; i < plainArray.length; i++){ System.out.print(" " + plainArray[i]); } System.out.println(); } } /** * Takes an index number for the position in the plainArray and returns the * ASCII numerical equivalent. Called by RSA.blockToNumber. * * @param i Index number in plainArray * @returns int ASCII value of the character from plainArray */ public int toASCIIValue(int i){ int value = (int)plainArray[i]; if (Encryption.debug) System.out.println("toASCIIValue " + value); return value; } /** * Adds most recent ciphertext to the StringBuffer which is holding the * ciphertext as it becomes available. Called by RSA.encrypt. * * @param addThis New ciphernumber passed from RSA.encrypt. */ public void concatenateToCipher(String addThis){ cipherBlock.append(addThis); if (Encryption.debug) System.out.println("concatenateToCipher " + cipherBlock); } /** * Takes user input from Encryption, checks its length, and stores it in * cipherBlock. Called by Encryption. * * @param input User input passed by Encryption * @throws WrongInputLengthException If user input is incorrect length */ public void initializeCipher(String input) throws WrongInputLengthException { // also check input length if (Encryption.debug) System.out.println("initialize cipher"); cipherBlock.insert(0, input); if (Encryption.debug) System.out.println("insert into cipherBlock"); // test input length if ((cipherBlock.length() % Key.semiprimeLength) != 0) { if (Encryption.debug) System.out.println("tested, failed length"); throw new WrongInputLengthException(); } if (Encryption.debug) System.out.println("end initialize cipher"); } /** * Converts part of the cipherBlock to the integer cipherSegment. Called * by RSA.decrypt. * * @param howMany ChunkSize used * @param position Where RSA.decrypt is in its decryption * @throws NumberFormatException If user has entered a non-integer. */ public void pullFromCipherBlock(int howMany, int position) throws NumberFormatException { // grabs a number of stuff from the cipherBlock and stores it. try{ cipherSegment = Integer.parseInt(cipherBlock.substring( position, position + howMany)); } catch (StringIndexOutOfBoundsException s) {// should never happen here if (Encryption.debug) System.out.println("overextended substring"); } if (Encryption.debug) System.out.println("cipherSegment " + cipherSegment); } /** * Converts a character value back to a character and stores it in * plainArray. Called by RSA.backToText. * * @param value Value of character * @param index Place in the plainArray */ public void toASCIIChar(int value, int index){ // if (Encryption.debug) System.out.println((char)value); plainArray[index] = (char)value; if (Encryption.debug) System.out.println("toASCIIChar " + plainArray[index]); } /** * Adds current contents of plainArray to the plainBlock, which is holding * the plaintext output. Called by RSA.decrypt. */ public void concatenateToPlain(){ for (int i = 0; i < plainArray.length; i++) { plainBlock.append(plainArray[i]); } if (Encryption.debug) System.out.println("concatenateToPlain " + plainBlock); } /** * Called by Encryption to clear variables before looping back over main. */ public void clearAll(){ // get rid of all contents of StringBuffers plainBlock.delete(0, plainBlock.length()); cipherBlock.delete(0, cipherBlock.length()); cipherSegment = 0; if (Encryption.debug) System.out. print("cleartext pb " + plainBlock + " cb " + cipherBlock + " cs " + cipherSegment + " plar "); for (int i = 0; i < plainArray.length; i++) { plainArray[i] = ' '; if (Encryption.debug) System.out.print(plainArray[i] + "."); } if (Encryption.debug) System.out.println(); } }