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;
}
}

New Topic/Question
Reply



MultiQuote




|