Random Substitution Cipher Help

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1970 Views - Last Post: 09 December 2011 - 09:43 PM Rate Topic: -----

#1 welshej  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 82
  • Joined: 18-November 11

Random Substitution Cipher Help

Posted 09 December 2011 - 06:45 PM

Hello all!

I'm writing some code to generate a random encryption key and use it to perform a substitution cipher on an array.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 25
at cipherM.shuffleArray(cipherM.java:375)
at cipherM.getRandomEncrypt(cipherM.java:354)
at cipherM.getEncrypt(cipherM.java:314)
at cipherM.subHandle(cipherM.java:29)
at cipherM.main(cipherM.java:16)


I'm trying to integrate a method to my previous substitution cipher code that generates a random encryption key (rather then the user needing to enter it in letter-by-letter). The method we're concerned with is getRandomEncrypt() and possibly the satellite methods (shuffleArray and randomInt).

import javax.swing.JOptionPane;
import java.io.*;

public class cipherM {
	final static int A = (int)'A';

	public static void main(String[] args) throws FileNotFoundException, IOException { 


		String humanIn = JOptionPane.showInputDialog("Please enter your message:");

		String choice = JOptionPane.showInputDialog("Would you like to use morse code (1) or substitution cipher (2)?");
		int x = Integer.parseInt(choice);

		if (x == 1) morseHandle(humanIn);
		if (x == 2) subHandle(humanIn);

	}

	public static void subHandle (String humanIn) throws FileNotFoundException, IOException {
		char[] arrayOfChar = new char [humanIn.length() + 1];
		for(int i = 0; i < humanIn.length(); i++) { // Fills the array with the letters from the string
			arrayOfChar = humanIn.toCharArray();
		}

		int len = arrayOfChar.length;
		char morse;

		String encryptKeyStr = getEncrypt();	
		char[] toWrite = new char [humanIn.length()];
		//Deals with regular input
		for (int w = 0; w < len; w++) { //Traverses character array, and passes them one at a time into textToMorse
			morse = subToNormal(arrayOfChar[w], encryptKeyStr);

			System.out.print(morse + " ");

			toWrite[w] = morse;
		}
		writeFile("/Users/12ewelsh/Desktop/secretMessage.txt", toWrite);
	}




	public static void morseHandle (String humanIn) throws FileNotFoundException, IOException {
		char[] arrayOfChar = new char [humanIn.length() + 1];
		arrayOfChar = humanIn.toCharArray();


		int len = arrayOfChar.length; // Creates variable for the length of the array 5 for "Hello"
		String morse = "";
		String toWrite = "";
		for (int w = 0; w < len; w++) { //Traverses character array, and passes them one at a time into textToMorse
			morse = textToMorse(arrayOfChar[w]); //Converts the array back into a string
			System.out.print(morse + " ");
			toWrite = toWrite + morse;
		}
		char[] toWriteCharArray = new char [humanIn.length()];
		toWriteCharArray = toWrite.toCharArray();
		writeFile("/Users/12ewelsh/Desktop/secretMessage.txt", toWriteCharArray);

	}


	public static String textToMorse(char textChar) { //Takes english and converts into morse code
		String morseChar = "";
		switch (textChar) { //Identifies the char and returns a string morseChar
		case 'a':
		case 'A':
			morseChar = ".-";
			break;
		case 'b':
		case 'B':	
			morseChar = "-...";
			break;
		case 'c':
		case 'C':	
			morseChar = "-.-/>.";
			break;
		case 'd':
		case 'D':
			morseChar = "-..";
			break;
		case 'e':
		case 'E':	
			morseChar = ".";
			break;
		case 'f':
		case 'F':
			morseChar = "..-.";
			break;
		case 'g':
		case 'G':	
			morseChar = "--.";
			break;
		case 'h':
		case 'H':	
			morseChar = "....";
			break;
		case 'i':
		case 'I':	
			morseChar = "..";
			break;
		case 'j':
		case 'J':	
			morseChar = ".---";
			break;
		case 'k':
		case 'K':	
			morseChar = "-.-/>";
			break;
		case 'l':
		case 'L':
			morseChar = ".-..";
			break;
		case 'm':
		case 'M':	
			morseChar = "--";
			break;
		case 'n':
		case 'N':	
			morseChar = "-.";
			break;
		case 'o':
		case 'O':	
			morseChar = "---";
			break;
		case 'p':
		case 'P':	
			morseChar = ".--.";
			break;
		case 'q':
		case 'Q':	
			morseChar = "--.-/>";
			break;
		case 'r':
		case 'R':	
			morseChar = ".-.";
			break;
		case 's':
		case 'S':	
			morseChar = "...";
			break;
		case 't':
		case 'T':	
			morseChar = "-";
			break;
		case 'u':
		case 'U':
			morseChar = "..-";
			break;
		case 'v':
		case 'V':
			morseChar = "...-";
			break;
		case 'w':
		case 'W':
			morseChar = ".--";
			break;
		case 'x':
		case 'X':	
			morseChar = "-..-";
			break;
		case 'y':
		case 'Y':
			morseChar = "-.-/>-";
			break;
		case 'z':
		case 'Z':
			morseChar = "--..";
			break;
		case ' ':
			morseChar = "     ";
			break;
		default:
			morseChar =" ";
			break;
		}
		return morseChar;
	}

	public static char subToNormal(char textChar, String encryptKeyStr) { //Takes english and the encrypt key and converts into morse code
		char subChar;
		char[] encryptKey = new char [25];

		for(int i = 0; i < encryptKeyStr.length(); i++) { // Fills the array with the letters from the string
			encryptKey = encryptKeyStr.toCharArray();
		}

		switch (textChar) { //Identifies the char and returns a string morseChar
		case 'a':
		case 'A':
			subChar = encryptKey[0];
			break;
		case 'b':
		case 'B':	
			subChar = encryptKey[1];
			break;
		case 'c':
		case 'C':	
			subChar = encryptKey[2];
			break;
		case 'd':
		case 'D':
			subChar = encryptKey[3];
			break;
		case 'e':
		case 'E':	
			subChar = encryptKey[4];
			break;
		case 'f':
		case 'F':
			subChar = encryptKey[5];
			break;
		case 'g':
		case 'G':	
			subChar = encryptKey[6];
			break;
		case 'h':
		case 'H':	
			subChar = encryptKey[7];
			break;
		case 'i':
		case 'I':	
			subChar = encryptKey[8];
			break;
		case 'j':
		case 'J':	
			subChar = encryptKey[9];
			break;
		case 'k':
		case 'K':	
			subChar = encryptKey[10];
			break;
		case 'l':
		case 'L':
			subChar = encryptKey[11];
			break;
		case 'm':
		case 'M':	
			subChar = encryptKey[12];
			break;
		case 'n':
		case 'N':	
			subChar = encryptKey[13];
			break;
		case 'o':
		case 'O':	
			subChar = encryptKey[14];
			break;
		case 'p':
		case 'P':	
			subChar = encryptKey[15];
			break;
		case 'q':
		case 'Q':	
			subChar = encryptKey[16];
			break;
		case 'r':
		case 'R':	
			subChar = encryptKey[17];
			break;
		case 's':
		case 'S':	
			subChar = encryptKey[18];
			break;
		case 't':
		case 'T':	
			subChar = encryptKey[19];
			break;
		case 'u':
		case 'U':
			subChar = encryptKey[20];
			break;
		case 'v':
		case 'V':
			subChar = encryptKey[21];
			break;
		case 'w':
		case 'W':
			subChar = encryptKey[22];
			break;
		case 'x':
		case 'X':	
			subChar = encryptKey[23];
			break;
		case 'y':
		case 'Y':
			subChar = encryptKey[24];
			break;
		case 'z':
		case 'Z':
			subChar = encryptKey[25];
			break;
		case ' ':
			subChar = ' ';
			break;
		default:
			subChar =' ';
			break;
		}
		return subChar; 
	}

	public static String getEncrypt() {

		String encryptString;
		String prompt;

		prompt = JOptionPane.showInputDialog("Would you like to enter your own encryption key(1), or use a randomly generated one(2)?");
		int choice = Integer.parseInt(prompt);

		if (choice == 2) {
			String randomEncrypt = getRandomEncrypt();
			return randomEncrypt;
		} else {

			System.out.println("Please enter the characters you would like to use to substitute for each letter.");
			encryptString = JOptionPane.showInputDialog("A: ");
			encryptString = encryptString + JOptionPane.showInputDialog("B: ");
			encryptString = encryptString + JOptionPane.showInputDialog("C: ");
			encryptString = encryptString + JOptionPane.showInputDialog("D: ");
			encryptString = encryptString + JOptionPane.showInputDialog("E: ");
			encryptString = encryptString + JOptionPane.showInputDialog("F: ");
			encryptString = encryptString + JOptionPane.showInputDialog("G: ");
			encryptString = encryptString + JOptionPane.showInputDialog("H: ");
			encryptString = encryptString + JOptionPane.showInputDialog("I: ");
			encryptString = encryptString + JOptionPane.showInputDialog("J: ");
			encryptString = encryptString + JOptionPane.showInputDialog("K: ");
			encryptString = encryptString + JOptionPane.showInputDialog("L: ");
			encryptString = encryptString + JOptionPane.showInputDialog("M: ");
			encryptString = encryptString + JOptionPane.showInputDialog("N: ");
			encryptString = encryptString + JOptionPane.showInputDialog("O: ");
			encryptString = encryptString + JOptionPane.showInputDialog("P: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Q: ");
			encryptString = encryptString + JOptionPane.showInputDialog("R: ");
			encryptString = encryptString + JOptionPane.showInputDialog("S: ");
			encryptString = encryptString + JOptionPane.showInputDialog("T: ");
			encryptString = encryptString + JOptionPane.showInputDialog("U: ");
			encryptString = encryptString + JOptionPane.showInputDialog("V: ");
			encryptString = encryptString + JOptionPane.showInputDialog("W: ");
			encryptString = encryptString + JOptionPane.showInputDialog("X: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Y: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Z: ");
			return encryptString;
		}
	}

	private static String getRandomEncrypt() {
		int[] arr = new int [25];
		for (int i = 1; i<=25; i++) {
			arr[i-1] = i; // Generate an array from 1-MAX
		}
		shuffleArray(arr);

		char[] randomEncryptArray = new char [25];

		for (int i = 0; i < 25; i++) {
			randomEncryptArray[i] = (char)(arr [i]+A);
		}
		String randomEncryptString = randomEncryptArray.toString();
		return randomEncryptString;
	}


	private static void shuffleArray (int a[]) { 
		/* Shuffles an array by going through one element at a time, and replacing each
		 * element with another random element of the array. */
		int temp;
		int rndNum = 0;

		for (int i = 0; i < 25; i++) {
			rndNum = randomInt(1,25); // Gives rndNum a random value
			temp = a[i]; //Stores the array value in a temporary storage variable
			a[i] = a[rndNum]; //Gives the ith position the value of the randomly selected element
			a[rndNum] = temp; //Sets the randomly selected element to the value of a[i].
		}
	}


	public static void writeFile (String filename, char[] array) throws IOException, FileNotFoundException {
		BufferedWriter outputWriter = new BufferedWriter(new FileWriter(filename));
		int len = array.length;
		//outputWriter.write(String.valueOf(nextInt));
		for (int i = 0; i < len; i++) {

			char nextChar = array[i];

			outputWriter.write(nextChar);
			outputWriter.flush();

		}

		outputWriter.close(); 
	}

	public static int[] readFile (String filename) throws IOException, FileNotFoundException {
		String line;
		BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));		
		int[] a = new int[100];

		for (int i = 0; ( line = inputReader.readLine()) != null; i++) { //Reading each line, parsing it

			int intValue = Integer.parseInt(line);
			a[i] = intValue; 
		}
		return a;
	}


	private static int randomInt(int low, int high) {
		int randReturn = 0;
		randReturn = (int) (high * Math.random()) + 1;
		return randReturn;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Random Substitution Cipher Help

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • Joined: 05-May 05

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 06:59 PM

rndNum = randomInt(1,25); // Gives rndNum a random value
374	            temp = a[i]; //Stores the array value in a temporary storage variable
375	            a[i] = a[rndNum]; //Gives the ith position the value of the randomly selected element
376	            a[rndNum] = temp; 



a[rndNum] is throwing the exception, so rndNum must be 25, which means randomInt(int,int) returns 25, when it should return at most 24.

private static int randomInt(int low, int high) {
412	        int randReturn = 0;
413	        randReturn = (int) (high * Math.random()) + 1;
414	        return randReturn;
415	    }



low isn't even used in the method. I use the Random class, so I'll just use that as an example. Random.nextInt(high) will return an integer between 0 (inclusive) and high (exclusive).

/**
* Generate a random number between the specified bounds.
* @param low The lower bound inclusive
* @param high The upper bound exclusive
*/
private static int randomInt(int low, int high) {
Random rand = new Random();
return low + rand.nextInt(high-low);
}


This post has been edited by blackcompe: 09 December 2011 - 07:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 07:12 PM

char[] arrayOfChar = new char [humanIn.length() + 1];

why + 1 ???
Was This Post Helpful? 0
  • +
  • -

#4 welshej  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 82
  • Joined: 18-November 11

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 07:13 PM

import javax.swing.JOptionPane;
import java.io.*;

public class cipherM {
	final static int A = (int)'A';

	public static void main(String[] args) throws FileNotFoundException, IOException { 


		String humanIn = JOptionPane.showInputDialog("Please enter your message:");

		String choice = JOptionPane.showInputDialog("Would you like to use morse code (1) or substitution cipher (2)?");
		int x = Integer.parseInt(choice);

		if (x == 1) morseHandle(humanIn);
		if (x == 2) subHandle(humanIn);

	}

	public static void subHandle (String humanIn) throws FileNotFoundException, IOException {
		char[] arrayOfChar = new char [humanIn.length() + 1];
		for(int i = 0; i < humanIn.length(); i++) { // Fills the array with the letters from the string
			arrayOfChar = humanIn.toCharArray();
		}

		int len = arrayOfChar.length;
		char morse;

		String encryptKeyStr = getEncrypt();	
		char[] toWrite = new char [humanIn.length()];
		//Deals with regular input
		for (int w = 0; w < len; w++) { //Traverses character array, and passes them one at a time into textToMorse
			morse = subToNormal(arrayOfChar[w], encryptKeyStr);

			System.out.print(morse + " ");

			toWrite[w] = morse;
		}
		writeFile("/Users/12ewelsh/Desktop/secretMessage.txt", toWrite);
	}




	public static void morseHandle (String humanIn) throws FileNotFoundException, IOException {
		char[] arrayOfChar = new char [humanIn.length() + 1];
		arrayOfChar = humanIn.toCharArray();


		int len = arrayOfChar.length; // Creates variable for the length of the array 5 for "Hello"
		String morse = "";
		String toWrite = "";
		for (int w = 0; w < len; w++) { //Traverses character array, and passes them one at a time into textToMorse
			morse = textToMorse(arrayOfChar[w]); //Converts the array back into a string
			System.out.print(morse + " ");
			toWrite = toWrite + morse;
		}
		char[] toWriteCharArray = new char [humanIn.length()];
		toWriteCharArray = toWrite.toCharArray();
		writeFile("/Users/12ewelsh/Desktop/secretMessage.txt", toWriteCharArray);

	}


	public static String textToMorse(char textChar) { //Takes english and converts into morse code
		String morseChar = "";
		switch (textChar) { //Identifies the char and returns a string morseChar
		case 'a':
		case 'A':
			morseChar = ".-";
			break;
		case 'b':
		case 'B':	
			morseChar = "-...";
			break;
		case 'c':
		case 'C':	
			morseChar = "-.-/>.";
			break;
		case 'd':
		case 'D':
			morseChar = "-..";
			break;
		case 'e':
		case 'E':	
			morseChar = ".";
			break;
		case 'f':
		case 'F':
			morseChar = "..-.";
			break;
		case 'g':
		case 'G':	
			morseChar = "--.";
			break;
		case 'h':
		case 'H':	
			morseChar = "....";
			break;
		case 'i':
		case 'I':	
			morseChar = "..";
			break;
		case 'j':
		case 'J':	
			morseChar = ".---";
			break;
		case 'k':
		case 'K':	
			morseChar = "-.-/>";
			break;
		case 'l':
		case 'L':
			morseChar = ".-..";
			break;
		case 'm':
		case 'M':	
			morseChar = "--";
			break;
		case 'n':
		case 'N':	
			morseChar = "-.";
			break;
		case 'o':
		case 'O':	
			morseChar = "---";
			break;
		case 'p':
		case 'P':	
			morseChar = ".--.";
			break;
		case 'q':
		case 'Q':	
			morseChar = "--.-/>";
			break;
		case 'r':
		case 'R':	
			morseChar = ".-.";
			break;
		case 's':
		case 'S':	
			morseChar = "...";
			break;
		case 't':
		case 'T':	
			morseChar = "-";
			break;
		case 'u':
		case 'U':
			morseChar = "..-";
			break;
		case 'v':
		case 'V':
			morseChar = "...-";
			break;
		case 'w':
		case 'W':
			morseChar = ".--";
			break;
		case 'x':
		case 'X':	
			morseChar = "-..-";
			break;
		case 'y':
		case 'Y':
			morseChar = "-.-/>-";
			break;
		case 'z':
		case 'Z':
			morseChar = "--..";
			break;
		case ' ':
			morseChar = "     ";
			break;
		default:
			morseChar =" ";
			break;
		}
		return morseChar;
	}

	public static char subToNormal(char textChar, String encryptKeyStr) { //Takes english and the encrypt key and converts into morse code
		char subChar;
		char[] encryptKey = new char [25];

		for(int i = 0; i < encryptKeyStr.length(); i++) { // Fills the array with the letters from the string
			encryptKey = encryptKeyStr.toCharArray();
		}

		switch (textChar) { //Identifies the char and returns a string morseChar
		case 'a':
		case 'A':
			subChar = encryptKey[0];
			break;
		case 'b':
		case 'B':	
			subChar = encryptKey[1];
			break;
		case 'c':
		case 'C':	
			subChar = encryptKey[2];
			break;
		case 'd':
		case 'D':
			subChar = encryptKey[3];
			break;
		case 'e':
		case 'E':	
			subChar = encryptKey[4];
			break;
		case 'f':
		case 'F':
			subChar = encryptKey[5];
			break;
		case 'g':
		case 'G':	
			subChar = encryptKey[6];
			break;
		case 'h':
		case 'H':	
			subChar = encryptKey[7];
			break;
		case 'i':
		case 'I':	
			subChar = encryptKey[8];
			break;
		case 'j':
		case 'J':	
			subChar = encryptKey[9];
			break;
		case 'k':
		case 'K':	
			subChar = encryptKey[10];
			break;
		case 'l':
		case 'L':
			subChar = encryptKey[11];
			break;
		case 'm':
		case 'M':	
			subChar = encryptKey[12];
			break;
		case 'n':
		case 'N':	
			subChar = encryptKey[13];
			break;
		case 'o':
		case 'O':	
			subChar = encryptKey[14];
			break;
		case 'p':
		case 'P':	
			subChar = encryptKey[15];
			break;
		case 'q':
		case 'Q':	
			subChar = encryptKey[16];
			break;
		case 'r':
		case 'R':	
			subChar = encryptKey[17];
			break;
		case 's':
		case 'S':	
			subChar = encryptKey[18];
			break;
		case 't':
		case 'T':	
			subChar = encryptKey[19];
			break;
		case 'u':
		case 'U':
			subChar = encryptKey[20];
			break;
		case 'v':
		case 'V':
			subChar = encryptKey[21];
			break;
		case 'w':
		case 'W':
			subChar = encryptKey[22];
			break;
		case 'x':
		case 'X':	
			subChar = encryptKey[23];
			break;
		case 'y':
		case 'Y':
			subChar = encryptKey[24];
			break;
		case 'z':
		case 'Z':
			subChar = encryptKey[25];
			break;
		case ' ':
			subChar = ' ';
			break;
		default:
			subChar =' ';
			break;
		}
		return subChar; 
	}

	public static String getEncrypt() {

		String encryptString;
		String prompt;

		prompt = JOptionPane.showInputDialog("Would you like to enter your own encryption key(1), or use a randomly generated one(2)?");
		int choice = Integer.parseInt(prompt);

		if (choice == 2) {
			String randomEncrypt = getRandomEncrypt();
			return randomEncrypt;
		} else {

			System.out.println("Please enter the characters you would like to use to substitute for each letter.");
			encryptString = JOptionPane.showInputDialog("A: ");
			encryptString = encryptString + JOptionPane.showInputDialog("B: ");
			encryptString = encryptString + JOptionPane.showInputDialog("C: ");
			encryptString = encryptString + JOptionPane.showInputDialog("D: ");
			encryptString = encryptString + JOptionPane.showInputDialog("E: ");
			encryptString = encryptString + JOptionPane.showInputDialog("F: ");
			encryptString = encryptString + JOptionPane.showInputDialog("G: ");
			encryptString = encryptString + JOptionPane.showInputDialog("H: ");
			encryptString = encryptString + JOptionPane.showInputDialog("I: ");
			encryptString = encryptString + JOptionPane.showInputDialog("J: ");
			encryptString = encryptString + JOptionPane.showInputDialog("K: ");
			encryptString = encryptString + JOptionPane.showInputDialog("L: ");
			encryptString = encryptString + JOptionPane.showInputDialog("M: ");
			encryptString = encryptString + JOptionPane.showInputDialog("N: ");
			encryptString = encryptString + JOptionPane.showInputDialog("O: ");
			encryptString = encryptString + JOptionPane.showInputDialog("P: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Q: ");
			encryptString = encryptString + JOptionPane.showInputDialog("R: ");
			encryptString = encryptString + JOptionPane.showInputDialog("S: ");
			encryptString = encryptString + JOptionPane.showInputDialog("T: ");
			encryptString = encryptString + JOptionPane.showInputDialog("U: ");
			encryptString = encryptString + JOptionPane.showInputDialog("V: ");
			encryptString = encryptString + JOptionPane.showInputDialog("W: ");
			encryptString = encryptString + JOptionPane.showInputDialog("X: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Y: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Z: ");
			return encryptString;
		}
	}

	private static String getRandomEncrypt() {
		int[] arr = new int [25];
		for (int i = 1; i<=25; i++) {
			arr[i-1] = i; // Generate an array from 1-MAX
		}
		shuffleArray(arr);

		char[] randomEncryptArray = new char [25];

		for (int i = 0; i < 25; i++) {
			randomEncryptArray[i] = (char)(arr [i]+A);
		}
		String randomEncryptString = randomEncryptArray.toString();
		return randomEncryptString;
	}


	private static void shuffleArray (int a[]) { 
		/* Shuffles an array by going through one element at a time, and replacing each
		 * element with another random element of the array. */
		int temp;
		int rndNum = 0;

		for (int i = 0; i < 25; i++) {
			rndNum = randomInt(25); // Gives rndNum a random value
			temp = a[i]; //Stores the array value in a temporary storage variable
			a[i] = a[rndNum]; //Gives the ith position the value of the randomly selected element
			a[rndNum] = temp; //Sets the randomly selected element to the value of a[i].
		}
	}


	public static void writeFile (String filename, char[] array) throws IOException, FileNotFoundException {
		BufferedWriter outputWriter = new BufferedWriter(new FileWriter(filename));
		int len = array.length;
		//outputWriter.write(String.valueOf(nextInt));
		for (int i = 0; i < len; i++) {

			char nextChar = array[i];

			outputWriter.write(nextChar);
			outputWriter.flush();

		}

		outputWriter.close(); 
	}

	public static int[] readFile (String filename) throws IOException, FileNotFoundException {
		String line;
		BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));		
		int[] a = new int[100];

		for (int i = 0; ( line = inputReader.readLine()) != null; i++) { //Reading each line, parsing it

			int intValue = Integer.parseInt(line);
			a[i] = intValue; 
		}
		return a;
	}


	private static int randomInt(int high) {
		int randReturn = 0;
		randReturn = (int) (high * Math.random());
		return randReturn;
	}
}

Was This Post Helpful? -1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 07:16 PM

and

char[] digit = str.toCharArray();

exists
Was This Post Helpful? 0
  • +
  • -

#6 welshej  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 82
  • Joined: 18-November 11

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 07:18 PM

And more updates:
import javax.swing.JOptionPane;
import java.io.*;

public class cipherM {
	final static int A = (int)'A';

	public static void main(String[] args) throws FileNotFoundException, IOException { 


		String humanIn = JOptionPane.showInputDialog("Please enter your message:");

		String choice = JOptionPane.showInputDialog("Would you like to use morse code (1) or substitution cipher (2)?");
		int x = Integer.parseInt(choice);

		if (x == 1) morseHandle(humanIn);
		if (x == 2) subHandle(humanIn);

	}

	public static void subHandle (String humanIn) throws FileNotFoundException, IOException {
		char[] arrayOfChar = new char [humanIn.length()];
		for(int i = 0; i < humanIn.length(); i++) { // Fills the array with the letters from the string
			arrayOfChar = humanIn.toCharArray();
		}

		int len = arrayOfChar.length;
		char morse;

		String encryptKeyStr = getEncrypt();	
		char[] toWrite = new char [humanIn.length()];
		//Deals with regular input
		for (int w = 0; w < len; w++) { //Traverses character array, and passes them one at a time into textToMorse
			morse = subToNormal(arrayOfChar[w], encryptKeyStr);

			System.out.print(morse + " ");

			toWrite[w] = morse;
		}
		writeFile("/Users/12ewelsh/Desktop/secretMessage.txt", toWrite);
	}




	public static void morseHandle (String humanIn) throws FileNotFoundException, IOException {
		char[] arrayOfChar = new char [humanIn.length()];
		arrayOfChar = humanIn.toCharArray();


		int len = arrayOfChar.length; // Creates variable for the length of the array 5 for "Hello"
		String morse = "";
		String toWrite = "";
		for (int w = 0; w < len; w++) { //Traverses character array, and passes them one at a time into textToMorse
			morse = textToMorse(arrayOfChar[w]); //Converts the array back into a string
			System.out.print(morse + " ");
			toWrite = toWrite + morse;
		}
		char[] toWriteCharArray = new char [humanIn.length()];
		toWriteCharArray = toWrite.toCharArray();
		writeFile("/Users/12ewelsh/Desktop/secretMessage.txt", toWriteCharArray);

	}


	public static String textToMorse(char textChar) { //Takes english and converts into morse code
		String morseChar = "";
		switch (textChar) { //Identifies the char and returns a string morseChar
		case 'a':
		case 'A':
			morseChar = ".-";
			break;
		case 'b':
		case 'B':	
			morseChar = "-...";
			break;
		case 'c':
		case 'C':	
			morseChar = "-.-/>.";
			break;
		case 'd':
		case 'D':
			morseChar = "-..";
			break;
		case 'e':
		case 'E':	
			morseChar = ".";
			break;
		case 'f':
		case 'F':
			morseChar = "..-.";
			break;
		case 'g':
		case 'G':	
			morseChar = "--.";
			break;
		case 'h':
		case 'H':	
			morseChar = "....";
			break;
		case 'i':
		case 'I':	
			morseChar = "..";
			break;
		case 'j':
		case 'J':	
			morseChar = ".---";
			break;
		case 'k':
		case 'K':	
			morseChar = "-.-/>";
			break;
		case 'l':
		case 'L':
			morseChar = ".-..";
			break;
		case 'm':
		case 'M':	
			morseChar = "--";
			break;
		case 'n':
		case 'N':	
			morseChar = "-.";
			break;
		case 'o':
		case 'O':	
			morseChar = "---";
			break;
		case 'p':
		case 'P':	
			morseChar = ".--.";
			break;
		case 'q':
		case 'Q':	
			morseChar = "--.-/>";
			break;
		case 'r':
		case 'R':	
			morseChar = ".-.";
			break;
		case 's':
		case 'S':	
			morseChar = "...";
			break;
		case 't':
		case 'T':	
			morseChar = "-";
			break;
		case 'u':
		case 'U':
			morseChar = "..-";
			break;
		case 'v':
		case 'V':
			morseChar = "...-";
			break;
		case 'w':
		case 'W':
			morseChar = ".--";
			break;
		case 'x':
		case 'X':	
			morseChar = "-..-";
			break;
		case 'y':
		case 'Y':
			morseChar = "-.-/>-";
			break;
		case 'z':
		case 'Z':
			morseChar = "--..";
			break;
		case ' ':
			morseChar = "     ";
			break;
		default:
			morseChar =" ";
			break;
		}
		return morseChar;
	}

	public static char subToNormal(char textChar, String encryptKeyStr) { //Takes english and the encrypt key and converts into morse code
		char subChar;
		char[] encryptKey = new char [25];

		for(int i = 0; i < encryptKeyStr.length(); i++) { // Fills the array with the letters from the string
			encryptKey = encryptKeyStr.toCharArray();
		}

		switch (textChar) { //Identifies the char and returns a string morseChar
		case 'a':
		case 'A':
			subChar = encryptKey[0];
			break;
		case 'b':
		case 'B':	
			subChar = encryptKey[1];
			break;
		case 'c':
		case 'C':	
			subChar = encryptKey[2];
			break;
		case 'd':
		case 'D':
			subChar = encryptKey[3];
			break;
		case 'e':
		case 'E':	
			subChar = encryptKey[4];
			break;
		case 'f':
		case 'F':
			subChar = encryptKey[5];
			break;
		case 'g':
		case 'G':	
			subChar = encryptKey[6];
			break;
		case 'h':
		case 'H':	
			subChar = encryptKey[7];
			break;
		case 'i':
		case 'I':	
			subChar = encryptKey[8];
			break;
		case 'j':
		case 'J':	
			subChar = encryptKey[9];
			break;
		case 'k':
		case 'K':	
			subChar = encryptKey[10];
			break;
		case 'l':
		case 'L':
			subChar = encryptKey[11];
			break;
		case 'm':
		case 'M':	
			subChar = encryptKey[12];
			break;
		case 'n':
		case 'N':	
			subChar = encryptKey[13];
			break;
		case 'o':
		case 'O':	
			subChar = encryptKey[14];
			break;
		case 'p':
		case 'P':	
			subChar = encryptKey[15];
			break;
		case 'q':
		case 'Q':	
			subChar = encryptKey[16];
			break;
		case 'r':
		case 'R':	
			subChar = encryptKey[17];
			break;
		case 's':
		case 'S':	
			subChar = encryptKey[18];
			break;
		case 't':
		case 'T':	
			subChar = encryptKey[19];
			break;
		case 'u':
		case 'U':
			subChar = encryptKey[20];
			break;
		case 'v':
		case 'V':
			subChar = encryptKey[21];
			break;
		case 'w':
		case 'W':
			subChar = encryptKey[22];
			break;
		case 'x':
		case 'X':	
			subChar = encryptKey[23];
			break;
		case 'y':
		case 'Y':
			subChar = encryptKey[24];
			break;
		case 'z':
		case 'Z':
			subChar = encryptKey[25];
			break;
		case ' ':
			subChar = ' ';
			break;
		default:
			subChar =' ';
			break;
		}
		return subChar; 
	}

	public static String getEncrypt() {

		String encryptString;
		String prompt;

		prompt = JOptionPane.showInputDialog("Would you like to enter your own encryption key(1), or use a randomly generated one(2)?");
		int choice = Integer.parseInt(prompt);

		if (choice == 2) {
			String randomEncrypt = getRandomEncrypt();
			return randomEncrypt;
		} else {

			System.out.println("Please enter the characters you would like to use to substitute for each letter.");
			encryptString = JOptionPane.showInputDialog("A: ");
			encryptString = encryptString + JOptionPane.showInputDialog("B: ");
			encryptString = encryptString + JOptionPane.showInputDialog("C: ");
			encryptString = encryptString + JOptionPane.showInputDialog("D: ");
			encryptString = encryptString + JOptionPane.showInputDialog("E: ");
			encryptString = encryptString + JOptionPane.showInputDialog("F: ");
			encryptString = encryptString + JOptionPane.showInputDialog("G: ");
			encryptString = encryptString + JOptionPane.showInputDialog("H: ");
			encryptString = encryptString + JOptionPane.showInputDialog("I: ");
			encryptString = encryptString + JOptionPane.showInputDialog("J: ");
			encryptString = encryptString + JOptionPane.showInputDialog("K: ");
			encryptString = encryptString + JOptionPane.showInputDialog("L: ");
			encryptString = encryptString + JOptionPane.showInputDialog("M: ");
			encryptString = encryptString + JOptionPane.showInputDialog("N: ");
			encryptString = encryptString + JOptionPane.showInputDialog("O: ");
			encryptString = encryptString + JOptionPane.showInputDialog("P: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Q: ");
			encryptString = encryptString + JOptionPane.showInputDialog("R: ");
			encryptString = encryptString + JOptionPane.showInputDialog("S: ");
			encryptString = encryptString + JOptionPane.showInputDialog("T: ");
			encryptString = encryptString + JOptionPane.showInputDialog("U: ");
			encryptString = encryptString + JOptionPane.showInputDialog("V: ");
			encryptString = encryptString + JOptionPane.showInputDialog("W: ");
			encryptString = encryptString + JOptionPane.showInputDialog("X: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Y: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Z: ");
			return encryptString;
		}
	}

	private static String getRandomEncrypt() {
		int[] arr = new int [25];
		for (int i = 1; i<=25; i++) {
			arr[i-1] = i; // Generate an array from 1-MAX
		}
		shuffleArray(arr);

		char[] randomEncryptArray = new char [25];

		for (int i = 0; i < 25; i++) {
			randomEncryptArray[i] = (char)(arr [i]+A);
		}
		String randomEncryptString = randomEncryptArray.toString();
		return randomEncryptString;
	}


	private static void shuffleArray (int a[]) { 
		/* Shuffles an array by going through one element at a time, and replacing each
		 * element with another random element of the array. */
		int temp;
		int rndNum = 0;

		for (int i = 0; i < 25; i++) {
			rndNum = randomInt(25); // Gives rndNum a random value
			temp = a[i]; //Stores the array value in a temporary storage variable
			a[i] = a[rndNum]; //Gives the ith position the value of the randomly selected element
			a[rndNum] = temp; //Sets the randomly selected element to the value of a[i].
		}
	}


	public static void writeFile (String filename, char[] array) throws IOException, FileNotFoundException {
		BufferedWriter outputWriter = new BufferedWriter(new FileWriter(filename));
		int len = array.length;
		//outputWriter.write(String.valueOf(nextInt));
		for (int i = 0; i < len; i++) {

			char nextChar = array[i];

			outputWriter.write(nextChar);
			outputWriter.flush();

		}

		outputWriter.close(); 
	}

	public static int[] readFile (String filename) throws IOException, FileNotFoundException {
		String line;
		BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));		
		int[] a = new int[100];

		for (int i = 0; ( line = inputReader.readLine()) != null; i++) { //Reading each line, parsing it

			int intValue = Integer.parseInt(line);
			a[i] = intValue; 
		}
		return a;
	}


	private static int randomInt(int high) {
		int randReturn = 0;
		randReturn = (int) (high * Math.random());
		return randReturn;
	}
}


import javax.swing.JOptionPane;
import java.io.*;

public class cipherM {
	final static int A = (int)'A';

	public static void main(String[] args) throws FileNotFoundException, IOException { 


		String humanIn = JOptionPane.showInputDialog("Please enter your message:");

		String choice = JOptionPane.showInputDialog("Would you like to use morse code (1) or substitution cipher (2)?");
		int x = Integer.parseInt(choice);

		if (x == 1) morseHandle(humanIn);
		if (x == 2) subHandle(humanIn);

	}

	public static void subHandle (String humanIn) throws FileNotFoundException, IOException {
		char[] arrayOfChar = new char [humanIn.length()];
		for(int i = 0; i < humanIn.length(); i++) { // Fills the array with the letters from the string
			arrayOfChar = humanIn.toCharArray();
		}

		int len = arrayOfChar.length;
		char morse;

		String encryptKeyStr = getEncrypt();	
		char[] toWrite = new char [humanIn.length()];
		//Deals with regular input
		for (int w = 0; w < len; w++) { //Traverses character array, and passes them one at a time into textToMorse
			morse = subToNormal(arrayOfChar[w], encryptKeyStr);

			System.out.print(morse + " ");

			toWrite[w] = morse;
		}
		writeFile("/Users/12ewelsh/Desktop/secretMessage.txt", toWrite);
	}




	public static void morseHandle (String humanIn) throws FileNotFoundException, IOException {
		char[] arrayOfChar = new char [humanIn.length()];
		arrayOfChar = humanIn.toCharArray();


		int len = arrayOfChar.length; // Creates variable for the length of the array 5 for "Hello"
		String morse = "";
		String toWrite = "";
		for (int w = 0; w < len; w++) { //Traverses character array, and passes them one at a time into textToMorse
			morse = textToMorse(arrayOfChar[w]); //Converts the array back into a string
			System.out.print(morse + " ");
			toWrite = toWrite + morse;
		}
		char[] toWriteCharArray = new char [humanIn.length()];
		toWriteCharArray = toWrite.toCharArray();
		writeFile("/Users/12ewelsh/Desktop/secretMessage.txt", toWriteCharArray);

	}


	public static String textToMorse(char textChar) { //Takes english and converts into morse code
		String morseChar = "";
		switch (textChar) { //Identifies the char and returns a string morseChar
		case 'a':
		case 'A':
			morseChar = ".-";
			break;
		case 'b':
		case 'B':	
			morseChar = "-...";
			break;
		case 'c':
		case 'C':	
			morseChar = "-.-/>.";
			break;
		case 'd':
		case 'D':
			morseChar = "-..";
			break;
		case 'e':
		case 'E':	
			morseChar = ".";
			break;
		case 'f':
		case 'F':
			morseChar = "..-.";
			break;
		case 'g':
		case 'G':	
			morseChar = "--.";
			break;
		case 'h':
		case 'H':	
			morseChar = "....";
			break;
		case 'i':
		case 'I':	
			morseChar = "..";
			break;
		case 'j':
		case 'J':	
			morseChar = ".---";
			break;
		case 'k':
		case 'K':	
			morseChar = "-.-/>";
			break;
		case 'l':
		case 'L':
			morseChar = ".-..";
			break;
		case 'm':
		case 'M':	
			morseChar = "--";
			break;
		case 'n':
		case 'N':	
			morseChar = "-.";
			break;
		case 'o':
		case 'O':	
			morseChar = "---";
			break;
		case 'p':
		case 'P':	
			morseChar = ".--.";
			break;
		case 'q':
		case 'Q':	
			morseChar = "--.-/>";
			break;
		case 'r':
		case 'R':	
			morseChar = ".-.";
			break;
		case 's':
		case 'S':	
			morseChar = "...";
			break;
		case 't':
		case 'T':	
			morseChar = "-";
			break;
		case 'u':
		case 'U':
			morseChar = "..-";
			break;
		case 'v':
		case 'V':
			morseChar = "...-";
			break;
		case 'w':
		case 'W':
			morseChar = ".--";
			break;
		case 'x':
		case 'X':	
			morseChar = "-..-";
			break;
		case 'y':
		case 'Y':
			morseChar = "-.-/>-";
			break;
		case 'z':
		case 'Z':
			morseChar = "--..";
			break;
		case ' ':
			morseChar = "     ";
			break;
		default:
			morseChar =" ";
			break;
		}
		return morseChar;
	}

	public static char subToNormal(char textChar, String encryptKeyStr) { //Takes english and the encrypt key and converts into morse code
		char subChar;
		char[] encryptKey = new char [25];

		for(int i = 0; i < encryptKeyStr.length(); i++) { // Fills the array with the letters from the string
			encryptKey = encryptKeyStr.toCharArray();
		}

		switch (textChar) { //Identifies the char and returns a string morseChar
		case 'a':
		case 'A':
			subChar = encryptKey[0];
			break;
		case 'b':
		case 'B':	
			subChar = encryptKey[1];
			break;
		case 'c':
		case 'C':	
			subChar = encryptKey[2];
			break;
		case 'd':
		case 'D':
			subChar = encryptKey[3];
			break;
		case 'e':
		case 'E':	
			subChar = encryptKey[4];
			break;
		case 'f':
		case 'F':
			subChar = encryptKey[5];
			break;
		case 'g':
		case 'G':	
			subChar = encryptKey[6];
			break;
		case 'h':
		case 'H':	
			subChar = encryptKey[7];
			break;
		case 'i':
		case 'I':	
			subChar = encryptKey[8];
			break;
		case 'j':
		case 'J':	
			subChar = encryptKey[9];
			break;
		case 'k':
		case 'K':	
			subChar = encryptKey[10];
			break;
		case 'l':
		case 'L':
			subChar = encryptKey[11];
			break;
		case 'm':
		case 'M':	
			subChar = encryptKey[12];
			break;
		case 'n':
		case 'N':	
			subChar = encryptKey[13];
			break;
		case 'o':
		case 'O':	
			subChar = encryptKey[14];
			break;
		case 'p':
		case 'P':	
			subChar = encryptKey[15];
			break;
		case 'q':
		case 'Q':	
			subChar = encryptKey[16];
			break;
		case 'r':
		case 'R':	
			subChar = encryptKey[17];
			break;
		case 's':
		case 'S':	
			subChar = encryptKey[18];
			break;
		case 't':
		case 'T':	
			subChar = encryptKey[19];
			break;
		case 'u':
		case 'U':
			subChar = encryptKey[20];
			break;
		case 'v':
		case 'V':
			subChar = encryptKey[21];
			break;
		case 'w':
		case 'W':
			subChar = encryptKey[22];
			break;
		case 'x':
		case 'X':	
			subChar = encryptKey[23];
			break;
		case 'y':
		case 'Y':
			subChar = encryptKey[24];
			break;
		case 'z':
		case 'Z':
			subChar = encryptKey[25];
			break;
		case ' ':
			subChar = ' ';
			break;
		default:
			subChar =' ';
			break;
		}
		return subChar; 
	}

	public static String getEncrypt() {

		String encryptString;
		String prompt;

		prompt = JOptionPane.showInputDialog("Would you like to enter your own encryption key(1), or use a randomly generated one(2)?");
		int choice = Integer.parseInt(prompt);

		if (choice == 2) {
			String randomEncrypt = getRandomEncrypt();
			return randomEncrypt;
		} else {

			System.out.println("Please enter the characters you would like to use to substitute for each letter.");
			encryptString = JOptionPane.showInputDialog("A: ");
			encryptString = encryptString + JOptionPane.showInputDialog("B: ");
			encryptString = encryptString + JOptionPane.showInputDialog("C: ");
			encryptString = encryptString + JOptionPane.showInputDialog("D: ");
			encryptString = encryptString + JOptionPane.showInputDialog("E: ");
			encryptString = encryptString + JOptionPane.showInputDialog("F: ");
			encryptString = encryptString + JOptionPane.showInputDialog("G: ");
			encryptString = encryptString + JOptionPane.showInputDialog("H: ");
			encryptString = encryptString + JOptionPane.showInputDialog("I: ");
			encryptString = encryptString + JOptionPane.showInputDialog("J: ");
			encryptString = encryptString + JOptionPane.showInputDialog("K: ");
			encryptString = encryptString + JOptionPane.showInputDialog("L: ");
			encryptString = encryptString + JOptionPane.showInputDialog("M: ");
			encryptString = encryptString + JOptionPane.showInputDialog("N: ");
			encryptString = encryptString + JOptionPane.showInputDialog("O: ");
			encryptString = encryptString + JOptionPane.showInputDialog("P: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Q: ");
			encryptString = encryptString + JOptionPane.showInputDialog("R: ");
			encryptString = encryptString + JOptionPane.showInputDialog("S: ");
			encryptString = encryptString + JOptionPane.showInputDialog("T: ");
			encryptString = encryptString + JOptionPane.showInputDialog("U: ");
			encryptString = encryptString + JOptionPane.showInputDialog("V: ");
			encryptString = encryptString + JOptionPane.showInputDialog("W: ");
			encryptString = encryptString + JOptionPane.showInputDialog("X: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Y: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Z: ");
			return encryptString;
		}
	}

	private static String getRandomEncrypt() {
		int[] arr = new int [25];
		for (int i = 1; i<=25; i++) {
			arr[i-1] = i; // Generate an array from 1-MAX
		}
		shuffleArray(arr);

		char[] randomEncryptArray = new char [25];

		for (int i = 0; i < 25; i++) {
			randomEncryptArray[i] = (char)(arr [i]+A);
		}
		String randomEncryptString = randomEncryptArray.toString();
		return randomEncryptString;
	}


	private static void shuffleArray (int a[]) { 
		/* Shuffles an array by going through one element at a time, and replacing each
		 * element with another random element of the array. */
		int temp;
		int rndNum = 0;

		for (int i = 0; i < 25; i++) {
			rndNum = randomInt(25); // Gives rndNum a random value
			temp = a[i]; //Stores the array value in a temporary storage variable
			a[i] = a[rndNum]; //Gives the ith position the value of the randomly selected element
			a[rndNum] = temp; //Sets the randomly selected element to the value of a[i].
		}
	}


	public static void writeFile (String filename, char[] array) throws IOException, FileNotFoundException {
		BufferedWriter outputWriter = new BufferedWriter(new FileWriter(filename));
		int len = array.length;
		//outputWriter.write(String.valueOf(nextInt));
		for (int i = 0; i < len; i++) {

			char nextChar = array[i];

			outputWriter.write(nextChar);
			outputWriter.flush();

		}

		outputWriter.close(); 
	}

	public static int[] readFile (String filename) throws IOException, FileNotFoundException {
		String line;
		BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));		
		int[] a = new int[100];

		for (int i = 0; ( line = inputReader.readLine()) != null; i++) { //Reading each line, parsing it

			int intValue = Integer.parseInt(line);
			a[i] = intValue; 
		}
		return a;
	}


	private static int randomInt(int high) {
		int randReturn = 0;
		randReturn = (int) (high * Math.random());
		return randReturn;
	}
}

This post has been edited by modi123_1: 09 December 2011 - 08:13 PM
Reason for edit:: fixed botched code tags...

Was This Post Helpful? -1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 07:32 PM

don't tell me that after 25 posts you don't know :code:

And may be you should learn how to use arrays befor trying to write such horrible code
Was This Post Helpful? 0
  • +
  • -

#8 welshej  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 82
  • Joined: 18-November 11

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 07:50 PM

View Postpbl, on 09 December 2011 - 07:32 PM, said:

don't tell me that after 25 posts you don't know :code:

And may be you should learn how to use arrays befor trying to write such horrible code


If you took any time whatsoever to read the rest of the post, you'd see that I have the [ code ] version directly beneath that. It appears that I accidently pasted it twice... Nevertheless, I'm not sure why you're acting so hurtful. I'm sorry if my code offends you, or doesn't meet your standards, but I posted it for the purpose of suggestion - I only started programming a few months ago. Rather then insulting my code for being 'horrible' could you tell me how I could improve it? I'm open to criticism as long as it's constructive.

Here's the code in [ code ] again, if it helps:

import javax.swing.JOptionPane;
import java.io.*;

public class cipherM {
	final static int A = (int)'A';

	public static void main(String[] args) throws FileNotFoundException, IOException { 


		String humanIn = JOptionPane.showInputDialog("Please enter your message:");

		String choice = JOptionPane.showInputDialog("Would you like to use morse code (1) or substitution cipher (2)?");
		int x = Integer.parseInt(choice);

		if (x == 1) morseHandle(humanIn);
		if (x == 2) subHandle(humanIn);

	}

	public static void subHandle (String humanIn) throws FileNotFoundException, IOException {
		char[] arrayOfChar = new char [humanIn.length()];
		for(int i = 0; i < humanIn.length(); i++) { // Fills the array with the letters from the string
			arrayOfChar = humanIn.toCharArray();
		}

		int len = arrayOfChar.length;
		char morse;

		String encryptKeyStr = getEncrypt();	
		char[] toWrite = new char [humanIn.length()];
		//Deals with regular input
		for (int w = 0; w < len; w++) { //Traverses character array, and passes them one at a time into textToMorse
			morse = subToNormal(arrayOfChar[w], encryptKeyStr);

			System.out.print(morse + " ");

			toWrite[w] = morse;
		}
		writeFile("/Users/12ewelsh/Desktop/secretMessage.txt", toWrite);
	}




	public static void morseHandle (String humanIn) throws FileNotFoundException, IOException {
		char[] arrayOfChar = new char [humanIn.length()];
		arrayOfChar = humanIn.toCharArray();


		int len = arrayOfChar.length; // Creates variable for the length of the array 5 for "Hello"
		String morse = "";
		String toWrite = "";
		for (int w = 0; w < len; w++) { //Traverses character array, and passes them one at a time into textToMorse
			morse = textToMorse(arrayOfChar[w]); //Converts the array back into a string
			System.out.print(morse + " ");
			toWrite = toWrite + morse;
		}
		char[] toWriteCharArray = new char [humanIn.length()];
		toWriteCharArray = toWrite.toCharArray();
		writeFile("/Users/12ewelsh/Desktop/secretMessage.txt", toWriteCharArray);

	}


	public static String textToMorse(char textChar) { //Takes english and converts into morse code
		String morseChar = "";
		switch (textChar) { //Identifies the char and returns a string morseChar
		case 'a':
		case 'A':
			morseChar = ".-";
			break;
		case 'b':
		case 'B':	
			morseChar = "-...";
			break;
		case 'c':
		case 'C':	
			morseChar = "-.-/>.";
			break;
		case 'd':
		case 'D':
			morseChar = "-..";
			break;
		case 'e':
		case 'E':	
			morseChar = ".";
			break;
		case 'f':
		case 'F':
			morseChar = "..-.";
			break;
		case 'g':
		case 'G':	
			morseChar = "--.";
			break;
		case 'h':
		case 'H':	
			morseChar = "....";
			break;
		case 'i':
		case 'I':	
			morseChar = "..";
			break;
		case 'j':
		case 'J':	
			morseChar = ".---";
			break;
		case 'k':
		case 'K':	
			morseChar = "-.-/>";
			break;
		case 'l':
		case 'L':
			morseChar = ".-..";
			break;
		case 'm':
		case 'M':	
			morseChar = "--";
			break;
		case 'n':
		case 'N':	
			morseChar = "-.";
			break;
		case 'o':
		case 'O':	
			morseChar = "---";
			break;
		case 'p':
		case 'P':	
			morseChar = ".--.";
			break;
		case 'q':
		case 'Q':	
			morseChar = "--.-/>";
			break;
		case 'r':
		case 'R':	
			morseChar = ".-.";
			break;
		case 's':
		case 'S':	
			morseChar = "...";
			break;
		case 't':
		case 'T':	
			morseChar = "-";
			break;
		case 'u':
		case 'U':
			morseChar = "..-";
			break;
		case 'v':
		case 'V':
			morseChar = "...-";
			break;
		case 'w':
		case 'W':
			morseChar = ".--";
			break;
		case 'x':
		case 'X':	
			morseChar = "-..-";
			break;
		case 'y':
		case 'Y':
			morseChar = "-.-/>-";
			break;
		case 'z':
		case 'Z':
			morseChar = "--..";
			break;
		case ' ':
			morseChar = "     ";
			break;
		default:
			morseChar =" ";
			break;
		}
		return morseChar;
	}

	public static char subToNormal(char textChar, String encryptKeyStr) { //Takes english and the encrypt key and converts into morse code
		char subChar;
		char[] encryptKey = new char [25];

		for(int i = 0; i < encryptKeyStr.length(); i++) { // Fills the array with the letters from the string
			encryptKey = encryptKeyStr.toCharArray();
		}

		switch (textChar) { //Identifies the char and returns a string morseChar
		case 'a':
		case 'A':
			subChar = encryptKey[0];
			break;
		case 'b':
		case 'B':	
			subChar = encryptKey[1];
			break;
		case 'c':
		case 'C':	
			subChar = encryptKey[2];
			break;
		case 'd':
		case 'D':
			subChar = encryptKey[3];
			break;
		case 'e':
		case 'E':	
			subChar = encryptKey[4];
			break;
		case 'f':
		case 'F':
			subChar = encryptKey[5];
			break;
		case 'g':
		case 'G':	
			subChar = encryptKey[6];
			break;
		case 'h':
		case 'H':	
			subChar = encryptKey[7];
			break;
		case 'i':
		case 'I':	
			subChar = encryptKey[8];
			break;
		case 'j':
		case 'J':	
			subChar = encryptKey[9];
			break;
		case 'k':
		case 'K':	
			subChar = encryptKey[10];
			break;
		case 'l':
		case 'L':
			subChar = encryptKey[11];
			break;
		case 'm':
		case 'M':	
			subChar = encryptKey[12];
			break;
		case 'n':
		case 'N':	
			subChar = encryptKey[13];
			break;
		case 'o':
		case 'O':	
			subChar = encryptKey[14];
			break;
		case 'p':
		case 'P':	
			subChar = encryptKey[15];
			break;
		case 'q':
		case 'Q':	
			subChar = encryptKey[16];
			break;
		case 'r':
		case 'R':	
			subChar = encryptKey[17];
			break;
		case 's':
		case 'S':	
			subChar = encryptKey[18];
			break;
		case 't':
		case 'T':	
			subChar = encryptKey[19];
			break;
		case 'u':
		case 'U':
			subChar = encryptKey[20];
			break;
		case 'v':
		case 'V':
			subChar = encryptKey[21];
			break;
		case 'w':
		case 'W':
			subChar = encryptKey[22];
			break;
		case 'x':
		case 'X':	
			subChar = encryptKey[23];
			break;
		case 'y':
		case 'Y':
			subChar = encryptKey[24];
			break;
		case 'z':
		case 'Z':
			subChar = encryptKey[25];
			break;
		case ' ':
			subChar = ' ';
			break;
		default:
			subChar =' ';
			break;
		}
		return subChar; 
	}

	public static String getEncrypt() {

		String encryptString;
		String prompt;

		prompt = JOptionPane.showInputDialog("Would you like to enter your own encryption key(1), or use a randomly generated one(2)?");
		int choice = Integer.parseInt(prompt);

		if (choice == 2) {
			String randomEncrypt = getRandomEncrypt();
			return randomEncrypt;
		} else {

			System.out.println("Please enter the characters you would like to use to substitute for each letter.");
			encryptString = JOptionPane.showInputDialog("A: ");
			encryptString = encryptString + JOptionPane.showInputDialog("B: ");
			encryptString = encryptString + JOptionPane.showInputDialog("C: ");
			encryptString = encryptString + JOptionPane.showInputDialog("D: ");
			encryptString = encryptString + JOptionPane.showInputDialog("E: ");
			encryptString = encryptString + JOptionPane.showInputDialog("F: ");
			encryptString = encryptString + JOptionPane.showInputDialog("G: ");
			encryptString = encryptString + JOptionPane.showInputDialog("H: ");
			encryptString = encryptString + JOptionPane.showInputDialog("I: ");
			encryptString = encryptString + JOptionPane.showInputDialog("J: ");
			encryptString = encryptString + JOptionPane.showInputDialog("K: ");
			encryptString = encryptString + JOptionPane.showInputDialog("L: ");
			encryptString = encryptString + JOptionPane.showInputDialog("M: ");
			encryptString = encryptString + JOptionPane.showInputDialog("N: ");
			encryptString = encryptString + JOptionPane.showInputDialog("O: ");
			encryptString = encryptString + JOptionPane.showInputDialog("P: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Q: ");
			encryptString = encryptString + JOptionPane.showInputDialog("R: ");
			encryptString = encryptString + JOptionPane.showInputDialog("S: ");
			encryptString = encryptString + JOptionPane.showInputDialog("T: ");
			encryptString = encryptString + JOptionPane.showInputDialog("U: ");
			encryptString = encryptString + JOptionPane.showInputDialog("V: ");
			encryptString = encryptString + JOptionPane.showInputDialog("W: ");
			encryptString = encryptString + JOptionPane.showInputDialog("X: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Y: ");
			encryptString = encryptString + JOptionPane.showInputDialog("Z: ");
			return encryptString;
		}
	}

	private static String getRandomEncrypt() {
		int[] arr = new int [25];
		for (int i = 1; i<=25; i++) {
			arr[i-1] = i; // Generate an array from 1-MAX
		}
		shuffleArray(arr);

		char[] randomEncryptArray = new char [25];

		for (int i = 0; i < 25; i++) {
			randomEncryptArray[i] = (char)(arr [i]+A);
		}
		String randomEncryptString = randomEncryptArray.toString();
		return randomEncryptString;
	}


	private static void shuffleArray (int a[]) { 
		/* Shuffles an array by going through one element at a time, and replacing each
		 * element with another random element of the array. */
		int temp;
		int rndNum = 0;

		for (int i = 0; i < 25; i++) {
			rndNum = randomInt(25); // Gives rndNum a random value
			temp = a[i]; //Stores the array value in a temporary storage variable
			a[i] = a[rndNum]; //Gives the ith position the value of the randomly selected element
			a[rndNum] = temp; //Sets the randomly selected element to the value of a[i].
		}
	}


	public static void writeFile (String filename, char[] array) throws IOException, FileNotFoundException {
		BufferedWriter outputWriter = new BufferedWriter(new FileWriter(filename));
		int len = array.length;
		//outputWriter.write(String.valueOf(nextInt));
		for (int i = 0; i < len; i++) {

			char nextChar = array[i];

			outputWriter.write(nextChar);
			outputWriter.flush();

		}

		outputWriter.close(); 
	}

	public static int[] readFile (String filename) throws IOException, FileNotFoundException {
		String line;
		BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));		
		int[] a = new int[100];

		for (int i = 0; ( line = inputReader.readLine()) != null; i++) { //Reading each line, parsing it

			int intValue = Integer.parseInt(line);
			a[i] = intValue; 
		}
		return a;
	}


	private static int randomInt(int high) {
		int randReturn = 0;
		randReturn = (int) (high * Math.random());
		return randReturn;
	}
}

This post has been edited by modi123_1: 09 December 2011 - 08:16 PM
Reason for edit:: fixed botched code tags

Was This Post Helpful? -1
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 08:00 PM

OK, forget he politically correct reply..

Your code is an horrible piece of crap...
You should seriously look to find a job outside the IT business, saling hot dogs on a stand out of a corner of a street seems more appropriate to you...
You denigrate the quality of the code shown in this forum by displaying such an horror...
And if you do not accept criticisms too bad for you but you will never progress in your professional assigment...
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 08:00 PM

*Edited: duplicated post narrowed down

This post has been edited by pbl: 09 December 2011 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#11 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 08:06 PM

You should definitely create an array to store the mores code sequence strings.

In a couple of post above, I noticed you still have not used the low value for your random number generator. Try this out, I have tested it thoroughly. I also added a swap at the beginning in case the parameters were backwards.
private int randomNumber(int min, int max) {
	if (min > max) {
		int temp = min;
		min = max;
		max = temp;
	}
	return min + (int) (Math.random() * ((max - min) + 1));
}


Was This Post Helpful? 1
  • +
  • -

#12 welshej  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 82
  • Joined: 18-November 11

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 08:14 PM

View Postx68zeppelin80x, on 09 December 2011 - 08:06 PM, said:

You should definitely create an array to store the mores code sequence strings.

In a couple of post above, I noticed you still have not used the low value for your random number generator. Try this out, I have tested it thoroughly. I also added a swap at the beginning in case the parameters were backwards.
private int randomNumber(int min, int max) {
	if (min > max) {
		int temp = min;
		min = max;
		max = temp;
	}
	return min + (int) (Math.random() * ((max - min) + 1));
}



Thanks! I've added it. However, I'm still getting an error with the message 'Hello':

6 3 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
	at cipherM.subToNormal(cipherM.java:237)
	at cipherM.subHandle(cipherM.java:33)
	at cipherM.main(cipherM.java:16)

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 08:26 PM

View Postx68zeppelin80x, on 09 December 2011 - 11:06 PM, said:

You should definitely create an array to store the mores code sequence strings.

In a couple of post above, I noticed you still have not used the low value for your random number generator. Try this out, I have tested it thoroughly. I also added a swap at the beginning in case the parameters were backwards.
private int randomNumber(int min, int max) {
	if (min > max) {
		int temp = min;
		min = max;
		max = temp;
	}
	return min + (int) (Math.random() * ((max - min) + 1));
}


If you want to impress your teacher do
private int randomNumber(int min, int max) {
	if (min > max) {
		max ^= min;
		min ^= max;
		max ^= min;
	}
	return min + (int) (Math.random() * ((max - min) + 1));
}


whhy creating a useles temp variable :)

This post has been edited by pbl: 09 December 2011 - 08:27 PM

Was This Post Helpful? 2
  • +
  • -

#14 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 129
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 09:10 PM

I created this to help you get a grasp on English plain-text to Morse Code translation. I should make this a tutorial LOL :) Enjoy.

TestMorseCode.java: This is the driver class, it prompts the user to enter an English plain-text phrase, and it converts it into Morse Code by scanning the phrase and looking up each character in the dictionary.
import java.util.ArrayList;
import java.util.Scanner;

public class TestMorseCode {
	MorseDictionary dictionary = new MorseDictionary();
	ArrayList<MorseCode> code = dictionary.getDictionary();

	public TestMorseCode() {
		System.out.println("This progrm will convert English plain-text to Morse Code.");
		System.out.print("Input: ");
		String input = new Scanner(System.in).nextLine();
		input = input.toUpperCase();
		System.out.println("Output: " + decode(input));
	}

	public String decode(String input) {
		String output = "";
		for (int i = 0; i < input.length(); i++) {
			for (int d = 0; d < code.size(); d++) {
				if (input.charAt(i) == code.get(d).getCharacter()) {
					output += code.get(d).getSequence();
				}
			}
		}
		return output;
	}

	public static void main(String[] args) {
		new TestMorseCode();
	}

}



MorseDictionary.java: This class creates a dictionary that stores each Morse Code sequence.
import java.util.ArrayList;
public class MorseDictionary {

	private ArrayList<MorseCode> dict;

	public MorseDictionary() {
		dict = new ArrayList<MorseCode>();
		dict.add(new MorseCode(' ', "+"));
		dict.add(new MorseCode('0', "-----"));
		dict.add(new MorseCode('1', ".----"));
		dict.add(new MorseCode('2', "..---"));
		dict.add(new MorseCode('3', "...--"));
		dict.add(new MorseCode('4', "....-"));
		dict.add(new MorseCode('5', "....."));
		dict.add(new MorseCode('6', "-...."));
		dict.add(new MorseCode('7', "--..."));
		dict.add(new MorseCode('8', "---.."));
		dict.add(new MorseCode('9', "----."));
		dict.add(new MorseCode('A', ".-"));
		dict.add(new MorseCode('B', "-..."));
		dict.add(new MorseCode('C', "-.-/>."));
		dict.add(new MorseCode('D', "-.."));
		dict.add(new MorseCode('E', "."));
		dict.add(new MorseCode('F', "..-."));
		dict.add(new MorseCode('G', "--."));
		dict.add(new MorseCode('H', "...."));
		dict.add(new MorseCode('I', ".."));
		dict.add(new MorseCode('J', ".---"));
		dict.add(new MorseCode('K', "-.-/>"));
		dict.add(new MorseCode('L', ".-.."));
		dict.add(new MorseCode('M', "--"));
		dict.add(new MorseCode('N', "-."));
		dict.add(new MorseCode('O', "---"));
		dict.add(new MorseCode('P', ".--."));
		dict.add(new MorseCode('Q', "--.-/>"));
		dict.add(new MorseCode('R', ".-."));
		dict.add(new MorseCode('S', "..."));
		dict.add(new MorseCode('T', "-"));
		dict.add(new MorseCode('U', "..-"));
		dict.add(new MorseCode('V', "...-"));
		dict.add(new MorseCode('W', ".--"));
		dict.add(new MorseCode('X', "-..-"));
		dict.add(new MorseCode('Y', "-.-/>-"));
		dict.add(new MorseCode('Z', "--.."));
	}

	public ArrayList<MorseCode> getDictionary() {
		return dict;
	}
}



MorseCode.java: This class maps a character to a sequence, so that you can cross reference.
I guess you could use a Map, but this is part of the learning experience :)
public class MorseCode {
	char character;
	String sequence;

	public MorseCode(char character, String sequence) {
		this.character = character;
		this.sequence = " " + sequence + " ";
	}

	public char getCharacter() {
		return character;
	}

	public String getSequence() {
		return sequence;
	}
}


This post has been edited by x68zeppelin80x: 09 December 2011 - 09:11 PM

Was This Post Helpful? 1
  • +
  • -

#15 welshej  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 82
  • Joined: 18-November 11

Re: Random Substitution Cipher Help

Posted 09 December 2011 - 09:14 PM

Brilliant!

I'm still a little confused as to what MorseCode.java does. By the way, did you really write that whole thing just for me? I feel honored!
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2